Reputation: 1226
I have a doubt of how the spring boot JDBC template works. I have read the documentation , but could not understand clearly :( When does the template opens connection , when does it gets closed . How does the transactions are handled . Does it gets opened and closed for every query execution ?
Upvotes: 0
Views: 365
Reputation: 7097
When does the template opens connection, when does it gets closed
For building JdbcTemplate you should specify the JDBC DataSource to obtain connections from:
public JdbcTemplate(DataSource dataSource)
Or:
public JdbcTemplate()
JdbcAccessor.setDataSource(javax.sql.DataSource)
Conclusively, JdbcTemplate
works with this DataSource
.
DataSource
, depending on the implementation, may return new standard Connection
objects that are not pooled or Connection
objects that participate in connection pooling which can be an be recycled.
JdbcTemplate
has pooled connections and releases them back to DataSource.
How does the transactions are handled
JdbcTemplate
relies on database transactions.
If you want to operate transactions on service layer/business logic you need transaction management.
The simplest way is to annotate services with @Transactional
or use org.springframework.transaction.support.TransactionTemplate
.
Upvotes: 2