Hi,
I am trying to test the Propagation.REQUIRED and Propagation.REQUIRES_NEW settings.
Here is the service I have
Objective is to make sure Propagation settings are working correctly.Code:package service; import java.util.logging.Logger; import model.BusData; import model.Container; import model.ExceptionLog; import model.Status; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import dataaccess.TestDAO; public class ServiceToTestTxnAnnotations { static private final Logger logger = Logger.getLogger(ServiceToTestTxnAnnotations.class.getName()); private TestDAO testDAO; @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public void runTest(Status status, BusData data, ExceptionLog log, boolean rollbackTest) throws Exception { createStatus(status); testDAO.createBusData(data); if(rollbackTest) { logger.fine("Rolling Back Transaction"); createExceptionLog(log); throw new Exception("Throwing Exception"); } } @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class) public void createStatus(Status status) { testDAO.createStatus(status); } public void createBusData(BusData data) { testDAO.createBusData(data); } @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class) public void createExceptionLog(ExceptionLog log) { testDAO.createExceptionLog(log); } public Container retrieve() { Status status = null; BusData data = null; ExceptionLog log = null; try { status = testDAO.retrieveStatus(); data = testDAO.retrieveBusData(); log = testDAO.retrieveExceptionLog(); } catch (Exception e) { e.printStackTrace(); } Container container = new Container(status, data, log); return container; } public TestDAO getTestDAO() { return testDAO; } public void setTestDAO(TestDAO testDAO) { this.testDAO = testDAO; } }
Here is my applicationContext.xml
According to the code written above, if an exception test is done, the createStatus and creaetExceptionLog methods should have been committed.Code:<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/UADM_DS"/> </bean> <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <constructor-arg><ref bean="dataSource"/></constructor-arg> </bean> <tx:annotation-driven transaction-manager="txManager"/> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
If no exception test, then createStatus and createBusData should have been committed. If affect createStatus and createExceptionLog should have been in their own transactions.
But when I tested this, everything happens in one transaction.
If exception test nothing is getting committed.
If no exception test createStatus and createBusData are getting committed.
Any help in identifying the issue is appreciated.


Reply With Quote
