Announcement Announcement Module
Collapse
No announcement yet.
Integrating Spring Managed Transactions into Legacy Code? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Integrating Spring Managed Transactions into Legacy Code?

    We're trying to integrate some Spring managed code which uses declarative transactions and Hibernate into some legacy code which manages its own database connections and begins/commits transactions on said database connections. Is there any way to integrate the two so that the transactions can cross the boundaries between the legacy JDBC usage and the spring components? I.E.

    MyTrans trans;
    try {
    //my legacy JDBC code
    //calls to Spring managed components which do Hibernate stuff
    trans.commit(); // Commit both the JDBC trans and the Spring trans
    } catch (Exception e) {
    trans.rollback();
    }


    Could we, for example, pass the database connection to the Spring Managed Hibernate SessionFactory, implement a custom transaction strategy which simply flushes the Hibernate Session configured in Spring, and then commit the trans on the JDBC connection on the legacy side of the code?

  • #2
    You can achieve this in spring in a fairly elegant way. If both HB and your JDBC is working on the same code it makes sense to use only one TransactionManager and you can achieve this buy injecting the same datasource into the HBTM and your jdbc template - the jdbc template is aware of any transactions going on and it will participate in there.

    On the other hand if there are different databases then you would need to have 2 separate transactions and in that case using a JTA TM (like JOTM) make more sense.

    Comment


    • #3
      Thanks for your response. The, JDBC code is not Spring managed... so it isn't using JdbcTemplate. Is this still possible?

      Comment


      • #4
        For the non-Spring-managed JDBC code - try getting the connection from a TransactionAwareDataSourceProxy (see http://static.springframework.org/sp...api/index.html

        That way the code can participate in Spring managed transactions - as long as the legacy code does not do any transaction management on its own.

        Comment


        • #5
          Indeed TransactionAwareDataSourceProxy is the cleanest approach by far. If your code DOES a transactiona management of it's own then try using the JTA.

          Comment


          • #6
            I'm trying to do exactly the same. Existing code uses its own custom built transaction manager and persistence mechanism. I want to start using hibernate for new objects and need to ensure that both transactions are committed or rolled back in an integrated way.

            Can anyone point to an example of doing this using JTA please?

            Comment

            Working...
            X