Announcement Announcement Module
Collapse
No announcement yet.
Trying to avoid two phase commit in a two datasource j2ee application. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trying to avoid two phase commit in a two datasource j2ee application.

    Here it is:

    Code:
    @Controller
    public class DoTestImpl implements DoTest {
    	private Service service1;         //With dataSource1
    	private Service service2;         //With dataSource2
    	private SomeDao someDao;  //With dataSource1
    	
    	@Transactional
    	@RequestMapping
    	public String doTest(HttpServletRequest request, HttpServletResponse response){
    		someDao.insert("3");
    	
    		try {
    			service1.doIt("1");
    			service2.doIt("2");
    		} catch (Exception e) {...}
    		
    		someDao.insert("5");
    		return "whatever";
    	}
    ...
    }
    
    public class ServiceImpl implements Service {
    	private SomeDao someDao;
    	
    	@Transactional(propagation=Propagation.REQUIRES_NEW, rollbackFor=Exception.class)
    	public void doIt(String number) throws Exception{
    		someDao.insert(number);
    	}
    }
    
    ...
            <tx:jta-transaction-manager/>	
    	<!-- 
    	<bean id="transactionManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager"/>
    	 -->
    	<tx:annotation-driven/>
    				            
    	<!-- DataSource with PDO Access.-->
    	<bean id="datasource1"  class="org.springframework.jndi.JndiObjectFactoryBean">
    		<property name="jndiName" value="java:comp/env/jdbc/datasource1"/>
    	</bean>
    	
    	<!-- DataSource with IntegII Access.-->
    	<bean id="datasource2" class="org.springframework.jndi.JndiObjectFactoryBean">
    		<property name="jndiName" value="java:comp/env/jdbc/datasource2"/>
    	</bean>
    	
    	<bean name="/test" class="com.ibm.brmt.mes.test.DoTestImpl">
    	    <property name="service1" ref="service1"/>
    	    <property name="service2" ref="service2"/>
    	     <property name="someDao" ref="someDao1"/>
    	</bean>
    	
    	<bean id="service1" class="com.ibm.brmt.mes.test.ServiceImpl">
    	    <property name="someDao" ref="someDao1"/>
    	</bean>
    	
    	<bean id="service2" class="com.ibm.brmt.mes.test.ServiceImpl">
    	    <property name="someDao" ref="someDao2"/>
    	</bean>
    	
     	<bean id="someDao1" class="com.ibm.brmt.integii.dao.CarrierAssignDaoImpl">
    		<property name="dataSource" ref="datasource1"/>
    	</bean>
    	
    	 <bean id="someDao2" class="com.ibm.brmt.integii.dao.CarrierAssignDaoImpl">
    		<property name="dataSource" ref="datasource2"/>
    	</bean>
    ...
    This works fine, but if this occurs

    Code:
    public class ServiceImpl implements Service {
    	private SomeDao someDao;
    	
    	@Transactional(propagation=Propagation.REQUIRES_NEW, rollbackFor=Exception.class)
    	public void doIt(String number) throws Exception{
    		someDao.insert(number);
                    throw new Exception();
    	}
    }
    I get :

    Code:
    ...
    Caused by: com.ibm.ws.Transaction.IllegalResourceIn2PCTransactionException: Illegal attempt to enlist multiple 1PC XAResources
    	at com.ibm.ws.tx.jta.RegisteredResources.enlistResource(RegisteredResources.java:864)
    	at com.ibm.ws.tx.jta.TransactionImpl.enlistResource(TransactionImpl.java:1781)
    	at com.ibm.ws.tx.jta.TranManagerSet.enlistOnePhase(TranManagerSet.java:612)
    	at com.ibm.ejs.j2c.LocalTransactionWrapper.enlist(LocalTransactionWrapper.java:593)
    	... 60 more
    Any idea why I get that?
    Seems like the rollbacking of the Service Transaction even if declared with propagation=Propagation.REQUIRES_NEW gets tangled with the initial transaction.

    I've added the log provided by 'org.springframework.transaction' in a zip file. Attachment
    Thanks
    Attached Files
    Last edited by Olivier Quirion; Mar 8th, 2012, 10:20 AM. Reason: I'va added the log of the transaction.

  • #2
    Problem solved!!

    Here's the solution for anybody who would ran into the same problem:

    In the web.xml file i've changed the datasource 'Sharing scope' property from 'Unshareable' to 'Shareable'
    Code:
    ...
    	<resource-ref id="ResourceRef_1324319635636">
    		<res-ref-name>jdbc/datasource1</res-ref-name>
    		<res-type>javax.sql.DataSource</res-type>
    		<res-auth>Container</res-auth>
    		<res-sharing-scope>Shareable</res-sharing-scope>
    	</resource-ref>
    	<resource-ref id="ResourceRef_1324319689579">
    		<res-ref-name>jdbc/datasource2</res-ref-name>
    		<res-type>javax.sql.DataSource</res-type>
    		<res-auth>Container</res-auth>
    		<res-sharing-scope>Shareable</res-sharing-scope>
    	</resource-ref>
    ...
    This IBM article help me understand the difference between the 'global and local transaction containment' and the influence of the 'Sharing scope' property.
    http://www.ibm.com/developerworks/we...0404_tang.html

    Best of luck if you get this problem, hope this help!!

    Comment


    • #3
      This post should be attach to Spring Project/Data since it talks about thread management, is there a way to change it?

      Comment

      Working...
      X