JDBC, Transactions, Spring Data JPA
July 29, 2021

M03 Q06 When does the JDBC template acquire (and release) a connection, for every method called or once per template? Why?

The connection lifecycle in JDBC Template depends on transactions being involved or not.

If JDBC Template is used without transaction, then the connection is acquired and released for every method call. Reason for this strategy, is to minimize amount of time when resource (connection) has to be held.

If JDBC Template is used together with transaction, then DataSourceUtils which is using TransactionSynchronizationManager will reuse connection between method calls as long as transaction is not committed or rolled back. Reason for this strategy is that connection cannot be closed when transaction is in progress, since closing connection would also rollback any changes made.

JDBC Template uses getConnection() method from DataSource class through DataSourceUtils class. If DataSource is plain JDBC Connection source, then connection is actually opened/closed, however if Connection Pool, like DBCP or C3P0 is used, then connection is not being opened/closed, however it is acquired or released from/to the pool.