Announcement Announcement Module
Collapse
No announcement yet.
Spring 3.1 with Multiple Transaction Managers Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3.1 with Multiple Transaction Managers

    I'm building an application that needs CRUD operations on two separate databases. The transactions are applied to one database or the other (never both...so no need for JTA is my understanding).

    The problem: My server (JBoss AS7) starts up fine. The application reads from both datasources, say DS1 and DS2, BUT it can only manipulate data from DS1. I can see sequences (Oracle 11g) being updated on DS2 but no table updates. There are no errors/exceptions thrown. I suspect one of my transaction managers isn't committing.

    Below is a list of technologies used and configuration settings...

    Tech Stack
    • JBoss AS7
    • Oracle 11g
    • Spring 3.1
    • JPA 2
    • Hibernate 4.1

    persistence-ds1.xml
    Code:
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
      <persistence-unit name="pu1">
        <class>com.somepackage.EntityA</class>  
        <class>com.somepackage.EntityB</class>  
        <class>com.somepackage.EntityC</class>
        <validation-mode>CALLBACK</validation-mode>
        <properties>
          <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultNamingStrategy" />
          <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
          <property name="hibernate.hbm2ddl.auto" value="validate" /> 
        </properties>
      </persistence-unit>
    </persistence>

    persistence-ds2.xml
    Code:
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
      <persistence-unit name="pu2">
        <class>com.somepackage.EntityD</class>  
        <class>com.somepackage.EntityE</class>  
        <class>com.somepackage.EntityF</class>
        <validation-mode>CALLBACK</validation-mode>
        <properties>
          <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultNamingStrategy" />
          <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
          <property name="hibernate.hbm2ddl.auto" value="validate" /> 
        </properties>
      </persistence-unit>
    </persistence>
    applicationContext.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:jee="http://www.springframework.org/schema/jee"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
                          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                          http://www.springframework.org/schema/tx
                          http://www.springframework.org/schema/tx/spring-tx.xsd
                          http://www.springframework.org/schema/jee         
                          http://www.springframework.org/schema/jee/spring-jee.xsd">
    
      <jee:jndi-lookup id="ds1" jndi-name="java:jboss/datasources/DS1"
        expected-type="javax.sql.DataSource" />
      <jee:jndi-lookup id="ds2" jndi-name="java:jboss/datasources/DS2"
        expected-type="javax.sql.DataSource" />     
    
      <bean id="em1" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <property name="entityManagerFactory" ref="emf1" />
        <property name="persistenceUnitName" value="pu1" />
      </bean>
      <bean id="em2" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <property name="entityManagerFactory" ref="emf2" />
        <property name="persistenceUnitName" value="pu2" />
      </bean>
    
      <bean id="emf1" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence-ds1.xml"/>
        <property name="dataSource" ref="ds1" />
        <property name="jpaVendorAdapter">
          <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
          </bean>
        </property>
        <property name="jpaDialect">
          <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
      </bean>
      <bean id="emf2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence-ds2.xml"/>
        <property name="dataSource" ref="ds2" />
        <property name="jpaVendorAdapter">
          <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
          </bean>
        </property>
        <property name="jpaDialect">
          <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
      </bean>
    
      <tx:annotation-driven transaction-manager="txm1" />
      <tx:annotation-driven transaction-manager="txm2" />
    
      <bean id="txm1" class="org.springframework.orm.jpa.JpaTransactionManager">
        <qualifier value="txMgr1"/>
        <property name="entityManagerFactory" ref="emf1" />
        <property name="jpaDialect">
          <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
      </bean>
      <bean id="txm2" class="org.springframework.orm.jpa.JpaTransactionManager">
        <qualifier value="txMgr2"/>
        <property name="entityManagerFactory" ref="emf2" />
        <property name="jpaDialect">
          <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
        </property>
      </bean>   
    </beans>
    In my DAOs, I reference the transaction managers at the class-level as follows.

    Code:
    @Transactional("txm1")
    public class DAO1 { ... }
    Code:
    @Transactional("txm2")
    public class DAO2 { ... }
Working...
X