Results 1 to 5 of 5

Thread: Propagation settings in @Transactional annotation not working

  1. #1
    Join Date
    Apr 2011
    Posts
    1

    Default 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 at 01:57 PM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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 Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Apr 2009
    Posts
    14

    Default

    Quote 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" ?

  4. #4
    Join Date
    Apr 2011
    Posts
    6

    Default

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

  5. #5
    Join Date
    Apr 2009
    Posts
    14

    Default

    Quote 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).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •