Think first What does JdbcTemplate take off your hands, and what is still your job when you query a database with it?
Why it matters: Questions ask which JdbcTemplate method or callback fits a task, and which exception a failed data access call throws.
JdbcTemplate opens and closes JDBC objects such as connections for you, which helps you avoid mistakes such as leaving a connection unclosed. It also turns every SQLException into an unchecked DataAccessException.
Reported, thanks
Step 2 of 10
Term
Meaning
JdbcTemplate
The central class of the JDBC core package; it creates and releases JDBC connections and related objects.
RowCallbackHandler
A callback interface that pulls values out of each row of a ResultSet.
DataAccessException
The root of Spring's data access exception hierarchy; it wraps the original exception.
Exception translator
The component that converts a JDBC SQLException into a Spring data access exception.
Reported, thanks
Step 3 of 10
Create it inside the DAO, or inject it as a bean. A DAO can instantiate the template directly. It can also receive one configured in the Spring container as a bean reference. Once configured, the template is thread-safe, so one shared instance can serve many DAOs.
Exam trap You do not need one JdbcTemplate per DAO or per thread. A configured instance is thread-safe and can be shared.
Reported, thanks
Step 4 of 10
Connection handling, transactions and SQL logging come built in. The template creates and releases what it uses, which helps you avoid errors such as a forgotten connection close. It joins Spring transaction support for both JTA and JDBC transactions. It logs every statement at DEBUG level under its class name.
Reported, thanks
Step 5 of 10
Each kind of SQL work has its own template method. Match the method to what the statement does and what you need back.
Common JdbcTemplate methods and their use
Method
Use it for
Worth knowing
update(..)
Insert, update and delete statements
One method covers all three changes
execute(..)
Any SQL, often DDL statements
Runs arbitrary SQL
queryForObject(..)
A single value of a given Java class
A bad type conversion throws InvalidDataAccessApiUsageException
queryForList(..)
Rows as a list of maps
Each map is keyed by column name
Reported, thanks
Step 6 of 10
Callbacks decide how each row of a result set is read. A RowCallbackHandler extracts values from every row of a ResultSet. For a JavaBean class, BeanPropertyRowMapper.newInstance builds a row mapper from the class you pass in. These callback interfaces live in org.springframework.jdbc.core with the template.
Reported, thanks
Step 7 of 10
JdbcTemplate never lets a raw SQLException escape. It translates JDBC exceptions into the hierarchy in org.springframework.dao, rooted at DataAccessException. The original exception stays wrapped inside, so no detail is lost. All translated exceptions are unchecked.
Exam trap Translated exceptions are unchecked. You are not forced to catch them; catch only the ones you can recover from and let the rest propagate.
Reported, thanks
Step 8 of 10
Since 6.0, the default is SQLExceptionSubclassTranslator. It checks SQLException subclasses from JDBC 4 and falls back to SQLState checks through SQLStateSQLExceptionTranslator. For your own translator, call setExceptionTranslator on the template. Use that template for all the affected data access.
Reported, thanks
Step 9 of 10
Mark DAOs and repositories with @Repository. It is the best way to guarantee exception translation. PersistenceExceptionTranslationPostProcessor finds every PersistenceExceptionTranslator and advises each @Repository bean, so thrown exceptions get translated.
Reported, thanks
Step 10 of 10
JdbcTemplate creates and releases JDBC connections, which helps you avoid forgetting to close a connection.
A configured JdbcTemplate is thread-safe and can be shared by many DAOs.
Use update(..) for insert, update and delete, and execute(..) for DDL.
JdbcTemplate and JdbcTransactionManager do not propagate SQLException; translated exceptions are unchecked.
Think first When does a @Transactional method start, join or roll back a transaction, and how do tests change that?
Why it matters: Questions show a transactional method or test and ask which propagation, rollback rule or default applies.
Spring wraps a transactional bean in an AOP proxy that runs transactions around method calls. Propagation decides whether a call joins a running transaction or starts its own. Rollback rules decide which exceptions undo the work.
Reported, thanks
Step 2 of 10
Term
Meaning
Transactional proxy
An AOP proxy that uses a TransactionInterceptor and a transaction manager around method calls.
PlatformTransactionManager
The manager imperative transactions need; reactive transactions use ReactiveTransactionManager instead.
Propagation
How a method's transaction relates to one already running; the default is REQUIRED.
Rollback rule
A setting that marks the transaction for rollback when a matching exception type is thrown.
Rollback-only
A mark that forces rollback; an outer caller that still commits gets UnexpectedRollbackException.
Reported, thanks
Step 3 of 10
AOP proxies apply transactions from XML or annotation metadata. The proxy pairs a TransactionInterceptor with a transaction manager around each call. In Java configuration, @EnableTransactionManagement switches this on. Methods returning Publisher or Kotlin Flow get reactive management; all other return types, void included, get imperative management.
Exam trap In proxy mode, the default, a call from one method to another in the same object skips the proxy. That self-invocation runs with no transaction, even if the called method has @Transactional.
Reported, thanks
Step 4 of 10
A method-level @Transactional beats the class-level settings. The most derived location wins. The XML tx:advice defaults are isolation DEFAULT and a read-write transaction. Its timeout is the one the underlying system uses, or no timeout where that system lacks them. Read-only transactions suit code that only reads data and can help performance, for example with Hibernate.
Reported, thanks
Step 5 of 10
REQUIRES_NEW always starts its own physical transaction.REQUIRED, the default, joins an outer transaction. A joining scope takes the outer scope's settings and silently ignores its own isolation, timeout or read-only flag. Spring offers every propagation option known from EJB CMT.
Three propagation settings compared
Feature
REQUIRED
REQUIRES_NEW
NESTED
Physical transaction
Joins the outer one and takes on its settings
An independent one for each affected scope
A single one, with savepoints to roll back to
Default propagation
Yes
No
No
Uses the isolation attribute
Yes
Yes
No
Exam trap When an inner scope silently marks the shared transaction rollback-only, the outer commit does not go through. The outer caller gets an UnexpectedRollbackException.
Reported, thanks
Step 6 of 10
By default, unchecked exceptions roll back and checked ones do not. Any RuntimeException or Error triggers rollback. Since Spring 6.2, rollbackOn=ALL_EXCEPTIONS on @EnableTransactionManagement makes checked exceptions roll back too. Spring's own TransactionException is unchecked, because it extends RuntimeException.
Exam trap A checked exception does not roll back by default. Since Spring 6.2, rollbackOn=ALL_EXCEPTIONS can change that default for all exceptions.
Reported, thanks
Step 7 of 10
A type-based rollback rule also matches subclasses. A thrown exception matches when it is the configured type or a subclass of it. If several rules match, Spring applies the strongest one. In XML, rollback-for and no-rollback-for each take a comma-delimited list of exceptions.
Reported, thanks
Step 8 of 10
A thread-bound transaction does not reach new threads.@Transactional usually relies on a PlatformTransactionManager binding the transaction to the current thread. Threads started inside the method do not share it. getTransaction() returns a TransactionStatus for a new or existing transaction. JtaTransactionManager uses the container's global infrastructure, so it needs no DataSource.
Reported, thanks
Step 9 of 10
Transactional test methods roll back by default. The TestContext Framework rolls back even when no @Rollback is declared. A method-level @Rollback can override a class-level @Rollback or @Commit. An @AfterTransaction void method runs once the test's transaction ends, and it need not be public.
Exam trap Leaving out @Rollback does not make a test commit. Rollback is already the default for transactional integration tests.
Reported, thanks
Step 10 of 10
@Transactional works through an AOP proxy, so self-invocation gets no transaction in proxy mode.
The default propagation is REQUIRED; REQUIRES_NEW always uses an independent physical transaction.
By default, RuntimeException and Error cause rollback, while checked exceptions do not.
Rollback rules match the configured exception type and its subclasses; the strongest match wins.
Transactional test methods roll back by default, even without @Rollback.
Think first In a Spring Boot JPA application, what does Boot set up for you, and what do you still declare yourself?
Why it matters: Questions show a JPA setup, repository or data test and ask what Spring Boot configures or how a query is built.
Add the Data JPA starter and Spring Boot brings Hibernate, a connection pool and entity scanning. You declare repository interfaces, and Spring Data builds queries from their method names. In tests, @DataJpaTest sets up entities and repositories for you.
Reported, thanks
Step 2 of 11
Term
Meaning
Data JPA starter
spring-boot-starter-data-jpa, which brings in Hibernate and the HikariCP connection pool.
Entity scanning
Finds @Entity, @Embeddable and @MappedSuperclass classes, so no persistence.xml is needed.
Derived query
A JPA query Spring Data creates automatically from a repository method's name.
@DataJpaTest
A test annotation that finds @Entity classes and sets up the Spring Data JPA repositories.
TestEntityManager
A test-specific alternative to the standard JPA EntityManager.
Reported, thanks
Step 3 of 11
The Data JPA starter adds Hibernate and HikariCP. Hibernate is the JPA implementation it includes. The spring-boot-starter-jdbc starter also pulls in HikariCP. @SpringBootApplication stands for @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan together, so auto-configuration is on.
Reported, thanks
Step 4 of 11
Without spring.datasource.url, Boot tries an embedded database. Boot can set up embedded H2, HSQL and Derby on its own, with no connection URL. You can instead point at a JNDI DataSource with spring.datasource.jndi-name.
Exam trap Boot does not create JPA databases automatically for every database by default. That default covers only embedded H2, HSQL or Derby.
Reported, thanks
Step 5 of 11
Entity scanning replaces persistence.xml. Boot finds entities by scanning, not from an XML list. Two annotations change where it looks, one for entities and one for repositories.
How Boot locates entities and repositories
Feature
Entities
Repositories
What is found
Classes with @Entity, @Embeddable or @MappedSuperclass
Interfaces that usually extend Repository or CrudRepository
Default search
Entity scanning, with no persistence.xml
The auto-configuration packages
Annotation to change the location
@EntityScan
@EnableJpaRepositories
Reported, thanks
Step 6 of 11
A Spring Data JPA repository is an interface you declare. Spring Data creates JPA queries automatically from its method names. The example shows a paged findAll and a finder whose name spells out the query.
Example: Declares a city repository with a paged findAll method and a finder derived from its name.
public interface CityRepository
extends Repository<City, Long> {
Page<City> findAll(Pageable pageable);
City findByNameAndStateAllIgnoringCase(
String name, String state);
}
Reported, thanks
Step 7 of 11
Annotate a method with @Query for complex queries. Method names work well for simple finders, and @Query covers the rest. The JPA and MongoDB modules of Spring Data use one common repository infrastructure.
Exam trap Not every store builds finders from names the way JPA does. Cassandra repository support is more limited and needs @Query finder methods.
Reported, thanks
Step 8 of 11
Repositories bootstrap in default, deferred or lazy mode. Set spring.data.jpa.repositories.bootstrap-mode to deferred or lazy to change the mode. In a web app, Boot registers OpenEntityManagerInViewInterceptor by default, allowing lazy loading in views. Setting spring.jpa.open-in-view to false stops it.
Exam trap Open EntityManager in View is already on by default in web apps. You turn it off, not on.
Reported, thanks
Step 9 of 11
@DataJpaTest scans entities and configures repositories. It also sets up an embedded database when one is on the classpath. It logs SQL by default, and its showSql attribute turns that off. To include @ConfigurationProperties beans, add @EnableConfigurationProperties.
Exam trap@DataJpaTest does not scan regular @Component beans, even though it does scan @Entity classes.
Reported, thanks
Step 10 of 11
Each data JPA test rolls back when it finishes. They are transactional by default. To turn that off for a whole class, use Propagation.NOT_SUPPORTED. Tests can inject a TestEntityManager, built for tests. Add @AutoConfigureTestEntityManager to get one in other Spring tests.
Example: Disables transaction management for every test in a data JPA test class.
@DataJpaTest
@Transactional(propagation = Propagation.NOT_SUPPORTED)
class MyNonTransactionalTests {
}
Reported, thanks
Step 11 of 11
The Data JPA starter brings in Hibernate and the HikariCP connection pool.
By default, Boot creates JPA databases automatically only for embedded H2, HSQL or Derby.
Spring Data derives JPA queries from repository method names; use @Query for complex ones.
@EntityScan moves entity scanning, and @EnableJpaRepositories moves repository scanning.
By default, every data JPA test runs in a transaction that rolls back afterward.
Each topic's Check tested it on its own; this mixes them.
Mixed quiz
Take the Domain 2 quiz
10 questions mixed from every topic in this domain, each answer explained. Mixing topics is harder than one at a time, and it is what the exam does. No account needed; with a free account your answers also build your estimated exam score.
Free to start: the study notes and plan, quizzes, flashcards and the first mock exam, no card needed. A free account keeps your place and saves your quiz scores.