Announcement Announcement Module
Collapse
No announcement yet.
JtaTransactionManager NOT Rolling back the data. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JtaTransactionManager NOT Rolling back the data.

    Hello All,

    Currently I configured Spring's (org.springframework.transaction.jta.JtaTransactio nManager) in my TOMCAT server environment to achieve distributed TM. I am using "JOTM" as the transaction manager.

    Right now I configured 2 data sources (1 oracle 10g and 1 Mysql 5.1) and want to participate them in a global transaction.

    I am doing a very simple operation: Inserting new products in to the tables of both the data sources. The product_id is the primary key here and will be entered from a front-end JSP. In case a product already present in the Mysql database it throws an "com.mysql.jdbc.exceptions.MySQLIntegrityConst rain tViolationException".

    Before trying to insert in MySql I am trying to insert the data in Oracle DB (Data not present). In this case while inserting the data in to Mysql it throws the above exception but does not rollback the data in Oracle DB.

    As per my understanding it should rollback all the transaction in case there is a run time exception but it is not happening in the above case.

    I have configured the "JtaTransactionManager" in my TOMCAT env. as below.

    [code]
    <tx:annotation-driven transaction-manager="txManager" />

    <!-- <jee:jndi-lookup id="dataSource" jndi-name="jdbc/exsOracle" /> -->
    <bean id="jotm" class="org.springframework.transaction.jta.JotmFac toryBean"/>


    <bean id="txManager"
    class="org.springframework.transaction.jta.JtaTran sactionManager">
    <property name="userTransaction"><ref local="jotm"/></property>
    <!-- <property name="transactionManagerName" value="java:comp/UserTransaction" /> -->
    </bean>

    <bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSou rce" destroy-method="shutdown">
    <property name="dataSource">
    <bean class="org.enhydra.jdbc.standard.StandardXADataSou rce" destroy-method="shutdown">
    <property name="transactionManager" ref="jotm" />
    <property name="driverName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="${dburl}" />
    </bean>
    </property>
    <property name="user" value="uname"/>
    <property name="password" value="pass"/>
    </bean>

    <bean id="dataSourceMysql" class="org.enhydra.jdbc.pool.StandardXAPoolDataSou rce" destroy-method="shutdown">
    <property name="dataSource">
    <bean class="org.enhydra.jdbc.standard.StandardXADataSou rce" destroy-method="shutdown">
    <property name="transactionManager" ref="jotm" />
    <property name="driverName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="${mysqldburl}" />
    </bean>
    </property>
    <property name="user" value="unamemsql"/>
    <property name="password" value="passmsql"/>
    </bean>
    [code]

    My Service and DAO classes are written below.

    Code:
    Service:SicServiceImpl
    import org.springframework.transaction.annotation.Transactional;
    
    import com.spr.dao.SicDao;
    import com.spr.domain.Sic;
    
    @Transactional
    public class SicServiceImpl implements SicService{
    
    	private SicDao sicDao;
    
    	/**
    	 * @param sicDao the sicDao to set
    	 */
    	public void setSicDao(SicDao sicDao) {
    		this.sicDao = sicDao;
    	}
    	
    	public boolean createSic(Sic sic){
    		boolean flag = sicDao.createSic(sic);
    		System.err.println("flag:::::::::::"+flag);
    		return flag;
    	}
    	
    	
    }
    
    
    DAO:SicDaoImpl
    
    import javax.sql.DataSource;
    
    //import org.springframework.dao.DataAccessException;
    import org.springframework.jdbc.core.JdbcTemplate;
    
    import com.spr.domain.Sic;
    
    public class SicDaoImpl implements SicDao{
    
    	
    	private JdbcTemplate jdbcTemplate;
    	private JdbcTemplate jdbctemplateMysql;
    
    	/**
    	 * 
    	 * @param datasourceExs
    	 */
    	public void setDatasourceExs(DataSource datasourceExs) {
    		this.jdbcTemplate = new JdbcTemplate(datasourceExs);
    	}
    	
    	public void setDatasourceExsMysql(DataSource datasourceExsMysql) {
    		this.jdbctemplateMysql = new JdbcTemplate(datasourceExsMysql);
    	}
    	
    	
    	
    	/**
    	 * @param sic
    	 */
    	public boolean createSic(Sic sic){
    		boolean flag = createSicInOracle(sic);
    		boolean flag1 = createSicInMySql(sic);
    		//return (flag);
    		return (flag && flag1);
    	}
    	
    	public boolean createSicInOracle(Sic sic) {
    		boolean isInsertSuccess = false;
    		int recordInsertedTisoSic = 0;
    		StringBuffer insertQueryTisoSic = new StringBuffer( " insert into " );
    		insertQueryTisoSic.append( "tiso_sic" );
    		insertQueryTisoSic.append( "(" );
    		insertQueryTisoSic.append( "SIC_CD" );
    		insertQueryTisoSic.append( "," );
    		insertQueryTisoSic.append( "SIC_DS" );
    		insertQueryTisoSic.append( "," );
    		insertQueryTisoSic.append( "TRIAGE_SIC_TYPE_CD" );
    		insertQueryTisoSic.append( ")" );
    		insertQueryTisoSic.append( " values (?,?,?)" );
    		recordInsertedTisoSic = jdbcTemplate.update( insertQueryTisoSic.toString(), new Object[]{
    			sic.getSicCode(), sic.getSicDesc(),sic.getSicTriageType()} );
    		if( recordInsertedTisoSic > 0 ){
    			isInsertSuccess = true;
    		}else{
    			isInsertSuccess = false;
    		}
    		
    		return isInsertSuccess;
    	}
    	
    
    	public boolean createSicInMySql(Sic sic){
    		
    		boolean isInsertSuccess = false;
    		int recordInsertedTisoSic = 0;
    		StringBuffer insertQueryTisoSic = new StringBuffer( " insert into " );
    		insertQueryTisoSic.append( "sic" );
    		insertQueryTisoSic.append( "(" );
    		insertQueryTisoSic.append( "SIC_CD" );
    		insertQueryTisoSic.append( "," );
    		insertQueryTisoSic.append( "SIC_DS" );
    		insertQueryTisoSic.append( "," );
    		insertQueryTisoSic.append( "TRIAGE_SIC_TYPE_CD" );
    		insertQueryTisoSic.append( ")" );
    		insertQueryTisoSic.append( " values (?,?,?)" );
    		recordInsertedTisoSic = jdbctemplateMysql.update( insertQueryTisoSic.toString(), new Object[]{
    			sic.getSicCode(), sic.getSicDesc(),sic.getSicTriageType()} );
    		if( recordInsertedTisoSic > 0 ){
    			isInsertSuccess = true;
    		}else{
    			isInsertSuccess = false;
    		}
    		
    		return isInsertSuccess;
    
    	}
    }

    Can any one say where is it going wrong?
    Does any one had ever faced similar situation with the tomcat environment before?

    Please share your thoughts.

    Many Thanks!!!

    Manoj.
Working...
X