Two Phase Lock (2PC) Configuration with Atomicos

I am creating a two-phase commit (2PC) sample application. I used the bits of code used here from the Internet. I use Spring, Hibernate and Atomikos with MySQL as the backend. I use two databases and intentionally make a call to the second database without checking if the first call to the database fails. Unfortunately this does not work. Can someone point me to some links with some sample code?

Below is my configuration:
Hibernate Session factories:

<bean id="sessionFactory1" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource1"/>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.connection.isolation">3</prop>    
            <prop key="hibernate.current_session_context_class">jta</prop>    
            <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>    
            <prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop> 
            <prop key="hibernate.connection.release_mode">on_close</prop>
        </props>
    </property>

    <property name="mappingResources">
        <list>
            <value>/hibernate/Stock.hbm.xml</value>
        </list>
    </property>
</bean>

<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
        <ref bean="dataSource2"/>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.connection.isolation">3</prop>    
            <prop key="hibernate.current_session_context_class">jta</prop>    
            <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>    
            <prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop> 
            <prop key="hibernate.connection.release_mode">on_close</prop>
        </props>
    </property>

    <property name="mappingResources">
        <list>
            <value>/hibernate/Stock1.hbm.xml</value>
        </list>
    </property>
</bean>

DataSource configuration:

<bean id="dataSource1" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">        
    <!-- set an arbitrary but unique name for the datasource -->       
    <property name="uniqueResourceName"><value>XADBMS1</value></property>        
    <!-- set the underlying driver class to use, in this example case we use MySql  -->       
    <property name="xaDataSourceClassName">          
        <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>       
    </property>       
    <property name="xaProperties">           
    <!--   set the driver-specific XADataSource properties  (check your driver docs for more info)           -->                 
        <props>                         
            <prop key="user">${jdbc.username}</prop>                         
            <prop key="password">${jdbc.password}</prop>                         
            <prop key="URL" >${jdbc.url1}</prop>                 
        </props>      
    </property>           
    <!-- how many connections in the pool? -->       
    <property name="poolSize" value="3"/>    
</bean>

<bean id="dataSource2" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">        
    <!-- set an arbitrary but unique name for the datasource -->       
    <property name="uniqueResourceName"><value>XADBMS2</value></property>        
    <!-- set the underlying driver class to use, in this example case we use MySql  -->       
    <property name="xaDataSourceClassName">          
        <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value>       
    </property>       
    <property name="xaProperties">           
    <!--   set the driver-specific XADataSource properties  (check your driver docs for more info)           -->                 
        <props>                         
            <prop key="user">${jdbc.username}</prop>                         
            <prop key="password">${jdbc.password}</prop>                         
            <prop key="URL" >${jdbc.url2}</prop>                 
        </props>      
    </property>           
    <!-- how many connections in the pool? -->       
    <property name="poolSize" value="3"/>    
</bean>

JTA Spring Configuration:

 <bean id="AtomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">     
<!-- when close is called, should we force transactions to terminate or not?     -->   
   <property name="forceShutdown" value="false" /> 
</bean>  
<!--Also use Atomikos UserTransactionImp, needed to configure Spring   --> 
<bean id="AtomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">     
    <property name="transactionTimeout" value="300" /> 
</bean>

<!--Configure the Spring framework to use JTA transactions from Atomikos  --> 
<bean id="JtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">    
    <property name="transactionManager" ref="AtomikosTransactionManager" />    
    <property name="userTransaction" ref="AtomikosUserTransaction" /> 
</bean>  

I have two DAOImpl into which I insert the two sessionFactories described above. The following is a call from Java code:

public static void main( String[] args )
{
    ApplicationContext appContext = 
            new ClassPathXmlApplicationContext("spring/config/appContext.xml");

    StockBo stockBo = (StockBo)appContext.getBean("stockBo1");
    StockBo stockBo2 = (StockBo)appContext.getBean("stockBo2");

    /** insert **/
    Stock stock = new Stock();
    stock.setStockCode("7668");
    stock.setStockName("HAIO");
    stockBo.save(stock);

    Stock stock1 = new Stock();
    //stock1.setStockCode("1668"); **Commented to fail the second db insert**
    stock1.setStockName("AAIO");
    stockBo2.save(stock1);
}

Any pointers would be very helpful.

thank

+3
1

, , DAO , , :

final StockBo stockBo = (StockBo)appContext.getBean("stockBo1");
final StockBo stockBo2 = (StockBo)appContext.getBean("stockBo2");
TransactionTemplate tx = new TransactionTemplate(appContext.getBean(PlatformTransactionManager.class);

tx.execute(new TransactionCallback<Void>() {
    public Void doInTransaction(TransactionStatus ts) {
        /** insert **/
        Stock stock = new Stock();
        stock.setStockCode("7668");
        stock.setStockName("HAIO");
        stockBo.save(stock);

        Stock stock1 = new Stock();
        //stock1.setStockCode("1668"); **Commented to fail the second db insert**
        stock1.setStockName("AAIO");
        stockBo2.save(stock1);

       return null;
    }
});

, , , Hibernate Spring:

<prop key="hibernate.current_session_context_class">jta</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop> 
<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup</prop>
<prop key="hibernate.connection.release_mode">on_close</prop>  

. :

+3

All Articles