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
and it's default implementation class:Code:package com.spring.da.tx; public interface FooService { Foo getFoo(String fooName); Foo getFoo(String fooName, String barName); void noPropagation(); }
Then here is my context config: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(""); } }
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).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>
When I run this test:
no exception is thrown. But getFoo is marked as MANDATORY and is called from non transactional method. So why the exception is not thrown?Code:FooService fooService = (FooService)ctx.getBean("fooService"); fooService.noPropagation();


Reply With Quote