Spring, two phase commit

Discussions

General J2EE: Spring, two phase commit

  1. Spring, two phase commit (7 messages)

    Have anyone worked on two phase commit. I got two dao(s) working on two different databases and I got one service class that works with these two dao(s), how will i handle the transaction using spring ??

    any ideas??

    -ravi
  2. You will need to use the JTA support that Spring provides - you are running in an app-server right?

    Trevor
  3. Yes Trevor, I am running on weblogic. I got two datasource configured and doing a jndi lookup in my spring config xml. I am new to JTA. Let me google and see what I can find..........

    any help regarding implementation.............

    -ravi
  4. after searching for a while, I found this example on http://www.hibernate.org/110.html
    <!--
    <bean id="myDataSource1" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName">
        <value>jdbc/myds1</value>
      </property>
    </bean>

    <bean id="myDataSource2" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName">
        <value>jdbc/myds2</value>
      </property>
    </bean>

    <bean id="mySessionFactory1" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
      <property name="mappingResources">
        <list>
          <value>product.hbm.xml</value>
        </list>
      </property>
      <property name="hibernateProperties">
        <props>
          <prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
        </props>
      </property>
      <property name="dataSource">
        <ref bean="myDataSource1"/>
      </property>
    </bean>

    <bean id="mySessionFactory2" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
      <property name="mappingResources">
        <list>
          <value>inventory.hbm.xml</value>
        </list>
      </property>
      <property name="hibernateProperties">
        <props>
          <prop key="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect</prop>
        </props>
      </property>
      <property name="dataSource">
        <ref bean="myDataSource2"/>
      </property>
    </bean>

    <bean id="myTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>

    <bean id="myProductDao" class="product.ProductDaoImpl">
      <property name="sessionFactory">
        <ref bean="mySessionFactory1"/>
       </property>
    </bean>

    <bean id="myInventoryDao" class="product.InventoryDaoImpl">
      <property name="sessionFactory">
        <ref bean="mySessionFactory2"/>
      </property>
    </bean>

    <bean id="myProductServiceTarget" class="product.ProductServiceImpl">
      <property name="productDao">
        <ref bean="myProductDao"/>
      </property>
      <property name="inventoryDao">
        <ref bean="myInventoryDao"/>
       </property>
    </bean>

    <bean id="myProductService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <property name="transactionManager">
        <ref bean="myTransactionManager"/>
      </property>
      <property name="target">
        <ref bean="myProductServiceTarget"/>
      </property>
      <property name="transactionAttributes">
        <props>
          <prop key="increasePrice*">PROPAGATION_REQUIRED</prop>
          <prop key="someOtherBusinessMethod">PROPAGATION_MANDATORY</prop>
        </props>
      </property>
    </bean>
    -->
  5. Hi Ravindranath,
       Were you able to make some progress on this? A good example always helps.

       The Spring guys (Jurgen and Rod and co) like to make note of the fact that you can transparently change your transaction management strategy. I.e. you can develop outside the container using Spring's local transactions, and then move to JTA with a simple configuration change. But this is only relevant for a single transactional database. If you need two-phase commit/distributed transactions, you will need to use JTA from the off - this will limit your testing to the container, but that is the nature of your application I guess.

    Trevor
  6. Trevor, I am still working on this one, I need to run few tests and will get back with some sample code and results.

    -ravi
  7. I am sorry for such question at this Forum, but would it be possible to execute 2-phase commit in Spring running on Tomcat only? - Michael
  8. I am sorry for such question at this Forum, but would it be possible to execute 2-phase commit in Spring running on Tomcat only?

    - Michael
    Yes, see http://www.atomikos.com/products.html for a standalone JTA that can be embedded in Tomcat and integrated with Spring. Guy