Results 1 to 2 of 2

Thread: Transaction propagation question

  1. #1

    Default Transaction propagation question

    Hi guys. Not sure if it's right place for question, but anyway.
    I can't understand how is the propagation mechanism works.
    So, I have an interface
    Code:
    package com.spring.da.tx;
    
    public interface FooService {
    	
    	Foo getFoo(String fooName);
    	
    	Foo getFoo(String fooName, String barName);
    	
    	void noPropagation();
    
    }
    and it's default implementation class:
    Code:
    package com.spring.da.tx;
    
    import org.springframework.transaction.interceptor.TransactionAspectSupport;
    
    public class DefaultFooService implements FooService {
    
    	public Foo getFoo(String fooName) {
    		return new Foo();
    	}
    
    	public Foo getFoo(String fooName, String barName) {
    		throw new UnsupportedOperationException();
    	}
    	
    	public void noPropagation() {
                getFoo("");
    	}
    
    }
    Then here is my context config:
    Code:
    <bean id="fooService" class="com.spring.da.tx.DefaultFooService"/>
        
        <tx:advice id="txAdvice" transaction-manager="txManager">
        	<tx:attributes>
        		<tx:method name="get*" read-only="true" rollback-for="UnsupportedOperationException" propagation="MANDATORY"/>
        	</tx:attributes>
        </tx:advice>
        
        <aop:config>
         	<aop:pointcut id="fooServiceOperation" expression="execution(* com.spring.da.tx.FooService.get*(..))"/>
        	<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation"/>
        </aop:config>
        
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="dataSource"/>
        </bean>
        
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        	<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        	<property name="url" value="jdbc:mysql://localhost:3306/"/>
        	<property name="username" value="root"/>
        	<property name="password" value="root"/>
        </bean>
    As you can see, only methods which are started with get should be in transaction. Even more, the propagation rule is MANDATORY, that means if no existing transaction is in progress, an exception will be thrown. In the DefaultFooService implementation class I have a non transactional method noPropagation() which calls the transactional (MANDATORY) method getFoo(String).

    When I run this test:
    Code:
    FooService fooService =  (FooService)ctx.getBean("fooService");
    		fooService.noPropagation();
    no exception is thrown. But getFoo is marked as MANDATORY and is called from non transactional method. So why the exception is not thrown?

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

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