简体   繁体   中英

Spring boot Jdbctemplate

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 ?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM