Results 1 to 6 of 6

Thread: Integrating Spring Managed Transactions into Legacy Code?

  1. #1
    Join Date
    Dec 2005
    Posts
    3

    Default 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. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Dec 2005
    Posts
    3

    Default

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

  4. #4
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    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.
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  5. #5
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Indeed TransactionAwareDataSourceProxy is the cleanest approach by far. If your code DOES a transactiona management of it's own then try using the JTA.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  6. #6
    Join Date
    Dec 2005
    Posts
    1

    Default

    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •