Announcement Announcement Module
Collapse
No announcement yet.
Transaction management with multiple data sources Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transaction management with multiple data sources

    I have service classes using multiple data sources and it depends on the arguments of the service method which data source is used each time.

    The service methods are as the one below:
    Code:
    public class MyServiceImpl implements MyService, Serializable{
    
    ...
    
      public MyObject updateObject(MyObject object){
    
          ObjectDAO objectDAO = (ObjectDAO) daoFactory
                  .getObject(object.getDB());
    
          return objectDAO.updateObject(object);
      }
    
    ...
    }
    I would need to have my service method inside a transaction, but I don't know how to do it.

    How can I specify a transaction manager that is not tied to one single data source. Ie. how to specify "myTransactionManager" below.

    Code:
    	<bean id="txProxyTemplate" abstract="true"
    		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager">
    			<ref bean="myTransactionManager"/>
    		</property>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
    				<prop key="update*">PROPAGATION_REQUIRED,-Throwable</prop>
    				<prop key="save*">PROPAGATION_REQUIRED,-Throwable</prop>
    				<prop key="add*">PROPAGATION_REQUIRED,-Throwable</prop>
    				<prop key="delete*">PROPAGATION_REQUIRED,-Throwable</prop>
    			</props>
    		</property>
    	</bean>
    
    
    <bean id="myService" parent="txProxyTemplate">
        <property name="target">
          <ref local="myServiceTarget" />
        </property>
      </bean>
      
      <bean id="myServiceTarget"
        class="org.app.my.service.impl.MyServiceImpl">
        <property name="daoFactory">
          <ref bean="daoFactory" />
        </property>
      </bean>
    
      <bean id="daoFactory"
        class="org.app.factory.MyDAOFactory">
        <constructor-arg>
          <map>
            <entry key="DB01">
              <ref bean="db01MyDAO" />
            </entry>
            <entry key="DB02">
              <ref bean="db02MyDAO" />
            </entry>
            <entry key="DB03">
              <ref bean="db03MyDAO" />
            </entry>
            <entry key="DB04">
              <ref bean="db04MyDAO" />
            </entry>
            <entry key="DB05">
              <ref bean="db05MyDAO" />
            </entry>
            <entry key="DB06">
              <ref bean="db06MyDAO" />
            </entry>
            <entry key="DB07">
              <ref bean="db07MyDAO" />
            </entry>
            <entry key="DB08">
              <ref bean="db08MyDAO" />
            </entry>
            <entry key="DB09">
              <ref bean="db09MyDAO" />
            </entry>
          </map>
        </constructor-arg>
    I have tried JtaTransactionManager as follows but I am getting a null pointer exeption when my service method is called. I am using Spring 1.1.4 and Hibernate 2. I would really have no need for JTA because only one data source is used in one transaction. But what is the best way to do this?

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

  • #2
    The JtaTM requires a UserTransaction or a real JTA TM to delegate to - read the javadocs. Why don't you use the HibernateTransactionManager if you don't need the JTA?
    Checkout the sample and reference documentation (the javadoc is also very good).

    Comment


    • #3
      Originally posted by costin
      The JtaTM requires a UserTransaction or a real JTA TM to delegate to - read the javadocs. Why don't you use the HibernateTransactionManager if you don't need the JTA?
      Checkout the sample and reference documentation (the javadoc is also very good).
      I am using OC4J and I think it should have a TM. I don't know how to configure it. I have tried to read the javadocs but either the information I need is not there or I don't understand it. The last line in my first message is what I have found in some examples but that is obviously not enough.

      I would use HibernateTransactionManager if I only knew how to configure it to use several data sources.

      For example, this works fine, but only with one of my data sources. If you know how to configure a transactionmanager that can have a list of datasources, please post a sample or a link to one.
      Code:
        <bean id="myTransactionManager"
           class="org.springframework.orm.hibernate.HibernateTransactionManager">
          <property name="dataSource"><ref bean="DB02" /></property>
          <property name="sessionFactory"><ref bean="db02SessionFactory" /></property>
        </bean>

      Comment


      • #4
        =)

        have you found your solution o how to use multiple hibernateTransactionmanager? can share?

        Comment

        Working...
        X