Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDBCTemplate with TransactionTemplate and Connection Pool, which datasource to use

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?

like image 380
Franz Kafka Avatar asked Aug 28 '11 11:08

Franz Kafka


People also ask

Does JdbcTemplate use connection pooling?

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.

How do you use TransactionTemplate?

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.

How to manage transaction in Spring JDBC?

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.

What is Datasourcetransactionmanager in spring?

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.


1 Answers

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.

like image 111
skaffman Avatar answered Sep 25 '22 15:09

skaffman