Announcement Announcement Module
Collapse
No announcement yet.
JdbcTemplate transaction on 2 datasource Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JdbcTemplate transaction on 2 datasource

    Hello,

    I have 2 database and want to do update on both or none at all.

    I tried using transactions but my code throws
    Code:
    java.lang.IllegalStateException: Cannot deactivate transaction synchronization - not active
    Here is the code
    Code:
                    TransactionDefinition def = new DefaultTransactionDefinition();
    		
    		TransactionStatus status1 = transactionManager1.getTransaction(def);
    		TransactionStatus status2 = transactionManager2.getTransaction(def);
    
    		try {
    			String sql1 = "SOME QUERY";
    			jdbcTemplate1.update(sql1, someParams.....);
    
    			String sql2 = "SOME QUERY";
    			jdbcTemplate2.update(sql2, someParams....);
    
    			transactionManager1.commit(status1);
    			transactionManager2.commit(status2);
    		} catch (DataAccessException e) {
    			transactionManager1.rollback(status1);
    			transactionManager2.rollback(status2);
    			throw e;
    		}

    It works fine when I have one transaction manager and one datasource but I need to update on 2 different database through one transaction.


    Appreciate if someone could help me on this.

  • #2
    Have you configured JTA? in case your transaction is spawning multiple database you need to use JTA. This needs to be configured in Spring.

    Comment


    • #3
      My app is running on tomcat 6.0.
      Should I use Atomikos to get JTA support?

      Comment


      • #4
        I have never used Atomikos, from the availaible documentation it seems like it works with Tomcat.
        You will also need to change your code to use a single JTA transaction manager.

        Comment


        • #5
          I tried to use Atomikos but I'm having problems to make it work.

          Can you propose any other solution with Spring and Tomcat?

          Link to some example would be very usefull.

          Thanks in advance.

          Comment

          Working...
          X