Announcement Announcement Module
Collapse
No announcement yet.
Propagation settings in @Transactional annotation not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Propagation settings in @Transactional annotation not working

    Hi,

    I am trying to test the Propagation.REQUIRED and Propagation.REQUIRES_NEW settings.

    Here is the service I have

    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;
    	}
    }
    Objective is to make sure Propagation settings are working correctly.

    Here is my applicationContext.xml

    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>
    According to the code written above, if an exception test is done, the createStatus and creaetExceptionLog methods should have been committed.
    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.
    Last edited by rpeketi; Apr 13th, 2011, 01:57 PM.

  • #2
    Please use the search as this question has been answered numerous times before.

    I suggest a read of the AOP chapter of the reference guide, especially the part where they explain proxies.

    Short answer internal method calls don't pass through the proxie and thus aren't getting adviced.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      Please use the search as this question has been answered numerous times before.

      I suggest a read of the AOP chapter of the reference guide, especially the part where they explain proxies.

      Short answer internal method calls don't pass through the proxie and thus aren't getting adviced.
      Marten, could you please check if your answer also applies to http://forum.springsource.org/showthread.php?t=107611 ?

      More specifically, I don't understand your last sentence: what do you mean by "internal method calls don't pass through the proxie and thus aren't getting adviced" ?

      Comment


      • #4
        Here's the section of the reference Marten's refering to: http://static.springsource.org/sprin...ng-aop-proxies

        Comment


        • #5
          Originally posted by jtougas View Post
          Here's the section of the reference Marten's refering to: http://static.springsource.org/sprin...ng-aop-proxies
          Oh, much clearer. Thanks a lot jtougas.

          So basically inner methods are called directly by the pojo class. They do not pass the proxied class and therefore any annotations are ignored (ie, the "advice" Marten referred).

          Comment

          Working...
          X