I'm not quite sure how to formulate the question, so feel free to tell me that I am thinking completly wrong.
I want to use the JdbcTemplate
and the TransactionTemplate
. I start of by initilizing my connection pool as datasource and the create a transaction manager as a datasource aswell?
BoneCPConfig connectionPoolConfig = new BoneCPConfig();
connectionPoolConfig.setJdbcUrl(...);
connectionPoolConfig.setUsername(...);
connectionPoolConfig.setPassword(...);
connectionPoolConfig.setMinConnectionsPerPartition(...);
connectionPoolConfig.setMaxConnectionsPerPartition(...);
dataSource = new BoneCPDataSource(connectionPoolConfig);
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
definition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
But now I want to create my TransactionTemplate and JdbcTemplate:
transactionTemplate = new TransactionTemplate(transactionManager);
JdbcTemplate jdbc = new JdbcTemplate(transactionManager.getDataSource());
Now mulitple threads access transactionTemplate
and jdbc
. Does this code guarantee that everything done in doInTransaction
uses the same connection for all jdbc calls?
Is the connection somehow linked internally, because it looks as if JdbcTemplate and TransactionTemplate could use what ever connection they wanted. Is my code correct/save?
It is a Java-based data access technology used to link Java databases. @Autowired JdbcTemplate jdbcTemplate; @Autowired private NamedParameterJdbcTemplate jdbcTemplate; We will configure DataSource and connection pooling present in the application.
Using TransactionTemplate TransactionTemplate provides a set of callback-based APIs to manage transactions manually. In order to use it, we should first initialize it with a PlatformTransactionManager. The PlatformTransactionManager helps the template to create, commit or roll back transactions.
Spring Transaction Management JDBC Example. We will create a simple Spring JDBC project where we will update multiple tables in a single transaction. The transaction should commit only when all the JDBC statements execute successfully otherwise it should rollback to avoid data inconsistency.
This class is capable of working in any environment with any JDBC driver, as long as the setup uses a javax. sql. DataSource as its Connection factory mechanism. Binds a JDBC Connection from the specified DataSource to the current thread, potentially allowing for one thread-bound Connection per DataSource.
This should all be fine. The critical part is that JdbcTemplate
and DataSourceTransactionManager
are supplied with the same DataSource
object, which you have done.
Does this code guarantee that everything done in doInTransaction uses the same connection for all jdbc calls? Is the connection somehow linked internally, because it looks as if JdbcTemplate and TransactionTemplate could use what every connection they wanted.
Internally, Spring uses complex transaction synchronization logic to make sure that the transactions, connections and datasources are all properly synchronized (if you're interested, have a look at TransactionSynchronizationManager
, although be warned, it's fearsome).
As long as you operate via the TransactionTemplate
and JdbcTemplate
APIs, it'll just work without any effort on your part. If you start manually fetching connections from the DataSource
yourself, though, then all bets are off.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With