@Transactional supports qualifier value for choosing between multiple transaction managers

This commit is contained in:
Juergen Hoeller
2009-05-08 23:13:43 +00:00
parent dc83107d66
commit d34c4a2cf0
13 changed files with 208 additions and 75 deletions

View File

@@ -22,6 +22,7 @@ import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.AopUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.CallCountingTransactionManager;
import org.springframework.transaction.support.TransactionSynchronizationManager;
/**
@@ -32,16 +33,34 @@ public class AnnotationDrivenTests extends TestCase {
public void testWithProxyTargetClass() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("annotationDrivenProxyTargetClassTests.xml", getClass());
TransactionalService service = (TransactionalService) context.getBean("service");
CallCountingTransactionManager tm1 = context.getBean("transactionManager1", CallCountingTransactionManager.class);
CallCountingTransactionManager tm2 = context.getBean("transactionManager2", CallCountingTransactionManager.class);
TransactionalService service = context.getBean("service", TransactionalService.class);
assertTrue(AopUtils.isCglibProxy(service));
service.setBeanName("someName");
service.setSomething("someName");
assertEquals(1, tm1.commits);
assertEquals(0, tm2.commits);
service.doSomething();
assertEquals(1, tm1.commits);
assertEquals(1, tm2.commits);
service.setSomething("someName");
assertEquals(2, tm1.commits);
assertEquals(1, tm2.commits);
service.doSomething();
assertEquals(2, tm1.commits);
assertEquals(2, tm2.commits);
}
public static class TransactionCheckingInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
if (methodInvocation.getMethod().getName().equals("setSomething")) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
}
else {
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
return methodInvocation.proceed();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,12 +21,16 @@ import org.springframework.transaction.annotation.Transactional;
/**
* @author Rob Harrop
* @author Juergen Hoeller
*/
@Transactional
public class TransactionalService implements BeanNameAware {
public class TransactionalService {
public void setBeanName(String name) {
// just for testing :)
@Transactional("synch")
public void setSomething(String name) {
}
@Transactional("noSynch")
public void doSomething() {
}
}

View File

@@ -2,7 +2,7 @@
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<tx:annotation-driven proxy-target-class="true" order="0"/>
@@ -12,8 +12,15 @@
<bean id="txCheckingInterceptor" class="org.springframework.transaction.config.AnnotationDrivenTests$TransactionCheckingInterceptor"/>
<bean id="transactionManager" class="org.springframework.transaction.CallCountingTransactionManager"/>
<bean id="transactionManager1" class="org.springframework.transaction.CallCountingTransactionManager">
<qualifier value="synch"/>
</bean>
<bean id="transactionManager2" class="org.springframework.transaction.CallCountingTransactionManager">
<property name="transactionSynchronizationName" value="SYNCHRONIZATION_NEVER"/>
<qualifier value="noSynch"/>
</bean>
<bean id="service" class="org.springframework.transaction.config.TransactionalService"/>
</beans>