Announcement Announcement Module
No announcement yet.
Integrating Spring Managed Transactions into Legacy Code? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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) {

    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.


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


      • #4
        For the non-Spring-managed JDBC code - try getting the connection from a TransactionAwareDataSourceProxy (see

        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.


        • #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.


          • #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?