Back Forum Reply New

Transaction can't rollback when throw exception

mons.dbcp.BasicDataSourcequot; destroy-method=quot;closequot; lazy-init=quot;truequot;gt;   lt;property name=quot;driverClassNamequot;gt;     lt;valuegt;oracle.jdbc.driver.OracleDriverlt;/valuegt;   lt;/propertygt;   lt;property name=quot;uclquot;gt;     lt;valuegt;jdbcracle:thinlocalhost:MYDBlt;/valuegt;   lt;/propertygt;   lt;property name=quot;usernamequot;gt;     lt;valuegt;ABCDlt;/valuegt;   lt;/propertygt;   lt;property name=quot;passwordquot;gt;     lt;valuegt;ABCD12lt;/valuegt;   lt;/propertygt;   lt;property name=quot;maxIdlequot;gt;     lt;valuegt;1lt;/valuegt;   lt;/propertygt;   lt;property name=quot;maxActivequot;gt;     lt;valuegt;8lt;/valuegt;   lt;/propertygt;   lt;property name=quot;initialSizequot;gt;     lt;valuegt;1lt;/valuegt;   lt;/propertygt;   lt;property name=quot;poolPreparedStatementsquot;gt;     lt;valuegt;truelt;/valuegt;   lt;/propertygt;   lt;property name=quot;validationQueryquot;gt;     lt;valuegt;select * from duallt;/valuegt;   lt;/propertygt; lt;/beangt; lt;bean id=quot;SessionFactoryquot; class=quot;org..orm.hibernate3.LocalSessionFactoryBeanquot; lazy-init=quot;truequot;gt;   lt;property name=quot;dataSourcequot;gt;     lt;ref bean=quot;DataSourcequot; /gt;   lt;/propertygt;   lt;property name=quot;hibernatePropertiesquot;gt;     lt;propsgt;       lt;prop key=quot;hibernate.dialectquot;gt;org.hibernate.dialect.Oracle9Dialectlt;/propgt;       lt;prop key=quot;hibernate.jdbc.fetch_sizequot;gt;50lt;/propgt;       lt;prop key=quot;hibernate.jdbc.batch_sizequot;gt;50lt;/propgt;     lt;/propsgt;   lt;/propertygt;   lt;property name=quot;mappingResourcesquot;gt;     lt;listgt;       lt;valuegt;Job.hbm.xmllt;/valuegt;     lt;/listgt;   lt;/propertygt; lt;/beangt; lt;bean id=quot;JobDAOquot; class=quot;com.test.dao.JobDAOquot;gt;   lt;property name=quot;sessionFactoryquot;gt;     lt;ref bean=quot;SessionFactoryquot; /gt;   lt;/propertygt; lt;/beangt; lt;bean id=quot;Testerquot; class=quot;com.test.dao.Testerquot;gt;   lt;property name=quot;jobDaoquot;gt;     lt;ref bean=quot;JobDAOquot; /gt;   lt;/propertygt; lt;/beangt; lt;bean id=quot;SpringTxManagerquot; class=quot;org..aop.framework.ProxyFactoryBeanquot;gt;   lt;property name=quot;proxyTargetClassquot;gt;     lt;valuegt;truelt;/valuegt;   lt;/propertygt;   lt;property name=quot;singletonquot;gt;     lt;valuegt;falselt;/valuegt;   lt;/propertygt;   lt;property name=quot;targetNamequot;gt;     lt;valuegt;Testerlt;/valuegt;   lt;/propertygt;   lt;property name=quot;interceptorNamesquot;gt;     lt;listgt;       lt;valuegt;transactionInterceptorlt;/valuegt;     lt;/listgt;   lt;/propertygt; lt;/beangt; lt;bean id=quot;transactionInterceptorquot; class=quot;org..transaction.interceptor.TransactionInterceptorquot;gt;   lt;property name=quot;transactionAttributesquot;gt;     lt;propsgt;       lt;prop key=quot;execquot;gtROPAGATION_REQUIRED,-java.lang.Exceptionlt;/propgt;     lt;/propsgt;   lt;/propertygt;   lt;property name=quot;transactionManagerquot;gt;     lt;ref bean=quot;transactionManagerquot; /gt;   lt;/propertygt; lt;/beangt; lt;bean id=quot;transactionManagerquot; class=quot;org..orm.hibernate3.HibernateTransactionManagerquot;gt;   lt;property name=quot;sessionFactoryquot;gt;     lt;ref bean=quot;SessionFactoryquot; /gt;   lt;/propertygt; lt;/beangt;
lt;/beansgt;

Code:
public class Tester
{   private JobDAO jobDao;   public void exec()   {       Job job = new Job();       job.setComment(quot;testquot;);       job.setDate(new Date());       this.jobDao.save( job );       this.throwException();   }      private void throwException()   {       throw new RuntimeException(quot;MyExceptionquot;);   }      public JobDAO getJobDao()   {       return this.jobDao;   }      public void setJobInfoDao( JobDAO jobDao )   {       this.jobDao = jobDao;   }
}How do you execute the Test.exec() method?

Dear all
I found the problem.
I should Get SpringTxManager bean instead of Tester bean defined in my Spring configuration to execute the programe.

As per the Spring's web-documentation , I am using 'Programmatic transaction management' using 'TransactionTemplate' to handle my transaction context.
However my transaction does not rollback on executing a Business exception despite calling the  setRollbackOnly () .

The java code is as follows:-

Code:

// this code uses inner class
transactionTemplate.execute(new TransactionCallbackWithoutResult() {

protected void   doInTransactionWithoutResult
(TransactionStatus status)
{       try {    //transaction 1    userDAO.createUser(createUser);    // transaction 2
validateUser(createUser);        
} catch (BusinessException ex) {          status.setRollbackOnly();          throw ex;       }   }
});

Code in the Application-Context.xml is as follows:-

lt;beansgt;   lt;bean id=quot;userServicequot;  class=quot;com.service.UserServiceImplquot;gt;       lt;constructor-arg ref  =quot;transactionManagerquot;/gt;lt;/beangt;
   lt;bean id=quot;transactionManagerquot; class=quot;org..jdbc.datasource.DataSourceTransactionManagerquot;gt;    lt;property name=quot;dataSourcequot; ref=quot;dataSourcequot;/gt;  lt;/beangt;   lt;bean id=quot;dataSourcequot; class=quot;org..jndi.JndiObjectFactoryBeanquot;gt;       lt;property name=quot;jndiNamequot;gt;lt;valuegt;java:comp/env/jdbc/mysql_M2FAUserDBlt;/valuegt;       lt;/propertygt;   lt;/beangt;
Am i using the correct code for  DatasourceTransactionManager ?

Any inputs will be highly valuable ?

regards
dhiraj

Is your mysql database transactional?

dejanp,         Thanks for your reply . I am using mysql 5.0.3 database but i am not sure if its non-transactional.
I assume that all standard databases (including Mysql 5.0.3) support transactional behaviour  .regards,
Dhiraj

Standard databases most certainly, but mysql is hardly a standard database. Afaik, you have to explicitly create your tables as quot;innodbquot; to make them transactional.
¥
Back Forum Reply New