Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How exactly JdbcTemplate with TransactionManager works together?

As far as I understood DataSourceTransactionManager binds a JDBC connection from the specified DataSource to the current thread, allowing for one thread-bound Connection per DataSource. If it's a pool of connections, it will take one of the available connections.

After this if I use JdbcTemplate inside a transaction, it will capture a connection binded by DataSourceTransactionManager. Do I understand the mechanism correctly? A there any requirements for making transaction manager bean definition (i.e. singleton)?

like image 566
Alexey Kalmykov Avatar asked Feb 12 '10 09:02

Alexey Kalmykov


People also ask

Can we use JdbcTemplate with transactional annotation?

It loops through the list of people and, for each person, inserts that person into the BOOKINGS table by using the JdbcTemplate . This method is tagged with @Transactional , meaning that any failure causes the entire operation to roll back to its previous state and to re-throw the original exception.

Does JdbcTemplate use connection pooling?

Spring Example JDBC Database Connection PoolJdbcTemplate requires a DataSource which is javax. sql. DataSource implementation and you can get this directly using spring bean configuration or by using JNDI if you are using the J2EE web server or application server for managing Connection Pool.

Does JdbcTemplate close connection?

In short yes it does close the connection.

Is it possible to define multiple transaction manager in Spring?

You can have more than one database connection.


1 Answers

You've pretty much described how it works. Spring's transaction synchronization logic has the rather fearsome responsibility for keeping transactions synchronized across JDBC, Hibernate, JPA, etc, and the end result is pretty seamless.

Transaction managers must be singletons, yes. Any one transaction is managed by only one transaction manager, and if you have several of them lying around, you're in for a world of pain.

like image 92
skaffman Avatar answered Sep 29 '22 01:09

skaffman