3

I am working on an application wherein I have to connect to different database depending upon an customer id that is passed from the client side. The schema for all the databases is same. It is a kind of multi-tenant application. Since I don't know how many customers will be there, I cannot use xml configuration to statically create the datasources, hence I have to create datasources manually.

We are using Spring JdbcTemplate for connecting to the databases and the connection parameter comes from another database which holds the configuration for the application. I am able to connect to the databases properly, but the method calls are not happening in a transaction. Following is the code snippet which does the database connectivity for only one database and I was going to extend it for multiple databases:

BasicDataSource datasource = new BasicDataSource();
// set database connection params
....
// create jdbcTemplate, 
jdbcTemplate = new JdbcTemplate(datasource);
// create transaction managers
PlatformTransactionManager txManager = new DataSourceTransactionManager(datasource);

My idea is to create the transaction manager manually and somehow bind it in the spring container so that all the methods/classes with @Transactional annotation can use this transaction manager. I am not able to figure how do I bind the txManager, so that all the methods/classes with @Transactional will use this transaction manager. I am not sure whether this is the right way and should I be creating a transaction manager for every datasource, since, I don't want the transaction to span multiple databases, but I want that every service method call should be in a transaction. Note: All my service classes have @Component and @Transactional annotation.

Am I solving the problem in the correct way?

jayendrap
  • 553
  • 1
  • 4
  • 10
  • Possible duplicate http://stackoverflow.com/questions/12641666/springs-jdbctemplate-and-transactions – Pradeep Kr Kaushal Jan 21 '14 at 15:36
  • @PradeepKrKaushal, thanks for your comment, but I had seen that question before posting. Using TransactionTemplate is not an efficient option for me because I lots of methods in my serivce classes and all of them will have to be wrapped using TransactionTemplate. I was looking for a solution which will be similar to the one mentioned in the 3rd paragraph of the accepted answer, but couldn't find how to do it. – jayendrap Jan 21 '14 at 16:00

2 Answers2

0

I think the following answer provides what you are looking for. Otherwise, you are going to need a global transaction manager. A global transaction manager will come bundled with a JEE container, or can be provided standalone by a trnsaction manager such as Atomikos. The Spring documentation covers both

Community
  • 1
  • 1
Romski
  • 1,912
  • 1
  • 12
  • 27
0

You can use the spring's AbstractRoutingDatasource.

Please check the below link. The AbstractRoutingDatasource is explained.Here database is changed dynamically based on language selected.

Spring AbstractRoutingDatasource

Sivaranjani D
  • 512
  • 4
  • 16