I am less than sure how you can formulate the question, so take a moment too tell my that i'm thinking completly wrong.
I wish to make use of the
JdbcTemplate and also the
TransactionTemplate. I oncoming of by initilizing my connection pool as datasource and also the produce a transaction manager like 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);
However I wish to create my TransactionTemplate and JdbcTemplate:
transactionTemplate = new TransactionTemplate(transactionManager); JdbcTemplate jdbc = new JdbcTemplate(transactionManager.getDataSource());
Now mulitple threads access
jdbc. Performs this code guarantee that everything completed in
doInTransaction uses exactly the same connection for those jdbc calls?
May be the connection in some way linked internally, since it appears to be if JdbcTemplate and TransactionTemplate can use no matter what connection they wanted. Is my code correct/save?
This will be fine. The critical part is the fact that
DataSourceTransactionManager are provided with similar
DataSource object, that you've done.
Performs this code guarantee that everything completed in doInTransaction uses exactly the same connection for those jdbc calls? May be the connection in some way linked internally, since it appears to be if JdbcTemplate and TransactionTemplate can use what every connection they wanted.
Internally, Spring uses complex transaction synchronization logic to make certain the transactions, connections and datasources are correctly synchronized (if you are interested, take a look at
TransactionSynchronizationManager, although be cautioned, it's fearsome).
As lengthy while you operate through the
JdbcTemplate APIs, it'll just work with no effort from you. Should you start by hand fetching connections in the
DataSource yourself, though, then all bets are off.