Results 1 to 4 of 4

Thread: Multiple transaction issue

  1. #1
    Join Date
    Jul 2012
    Posts
    2

    Default Multiple transaction issue

    Hi, I am working on a project where we are doing some enhancements to the existing legacy system. The legacy system uses JDBC connection. Now we built some enhancements on top of it with Spring 3.1 and use hibernate JPA for persistence. We have a situation to do some inserts and updates with JDBC connection [existing code] and do some inserts and updates with the enhanced code using hibernate JPA. As the system is opening up new connections, one for the legacy and other for the enhanced system we are unable to control the transactions...we use LocalContainerEntityManagerBean and JPATransactionManager. Is there a way to control both the transactions? Your assistance is much appreciated.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    I suggest a read of the reference guide which has a section on integrating with legacy systems.

    In short you must only use the JpaTransactionManager and have it also control the jdbc transactions (which it is perfectly capable of!), simply inject the datasource into the JpaTransactionManager. Next you need to wrap your datasource in a transactationawaredatasourceproxy and have the legacy stuff use that as the datasource.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jul 2012
    Posts
    2

    Default

    Thank you Marten.
    I read the reference guide but my situation seems to be quite different. May be my interpretation is wrong.

    I don't want to change the legacy code which obtains connection using code:

    Code:
    Driver dbDriver = (Driver) Class.forName(sDriver).newInstance(); 
    Connection conn = dbDriver .connect(url, info);
    Legacy does its stuff with the above connection [stuff-A] and then I use spring to do my enhacement stuffs [stuff-B] which uses its own connection with the custom datasource supplied in xml.
    Spring code:

    Code:
    <bean id="myJPAEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
         <property name="persistenceXmlLocation" value="classpath:com/myProject/data/persistence.xml" />
         <property name="persistenceUnitName" value="default" />  
         <property name="dataSource" ref="myCustomDataSource" />
         
         <property name="jpaVendorAdapter">
              <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    	<property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" />
    	<property name="showSql" value="true" />
              </bean>
         </property>
    </bean>
    
    <tx:annotation-driven transaction-manager="jpaTransactionManager" />
    <bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
               p:entityManagerFactory-ref="myJPAEntityManagerFactory"  p:dataSource-ref="myCustomDataSource" />
    As there are two differenct connections I feel they are bound to different transactions and if stuff-B fails then stuff-A is not rolled back.
    Appreciate your help.
    Last edited by Elamurugan; Jul 5th, 2012 at 06:55 PM. Reason: Making it readable

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Well if that is the code you are facing you are on your own... That is a way of obtaining a connection which you shouldn't use (since about 2002 ).

    You might be able to write a proxy Driver which delegates to the Spring stuff (I assume you are controllering the construction of those objects in the same spring application!) and returns the thread bound connection (you could still delegate parts to the TransactionAwareDataSourceProxy).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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