use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.
the class TransactionInterceptorTests method serializableWithCompositeSource.
@Test
public void serializableWithCompositeSource() throws Exception {
NameMatchTransactionAttributeSource tas1 = new NameMatchTransactionAttributeSource();
Properties props = new Properties();
props.setProperty("methodName", "PROPAGATION_REQUIRED");
tas1.setProperties(props);
NameMatchTransactionAttributeSource tas2 = new NameMatchTransactionAttributeSource();
props = new Properties();
props.setProperty("otherMethodName", "PROPAGATION_REQUIRES_NEW");
tas2.setProperties(props);
TransactionInterceptor ti = new TransactionInterceptor();
ti.setTransactionAttributeSources(new TransactionAttributeSource[] { tas1, tas2 });
PlatformTransactionManager ptm = new SerializableTransactionManager();
ti.setTransactionManager(ptm);
ti = (TransactionInterceptor) SerializationTestUtils.serializeAndDeserialize(ti);
assertTrue(ti.getTransactionManager() instanceof SerializableTransactionManager);
assertTrue(ti.getTransactionAttributeSource() instanceof CompositeTransactionAttributeSource);
CompositeTransactionAttributeSource ctas = (CompositeTransactionAttributeSource) ti.getTransactionAttributeSource();
assertTrue(ctas.getTransactionAttributeSources()[0] instanceof NameMatchTransactionAttributeSource);
assertTrue(ctas.getTransactionAttributeSources()[1] instanceof NameMatchTransactionAttributeSource);
}
use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.
the class TransactionInterceptorTests method determineTransactionManagerWithQualifierAndDefault.
@Test
public void determineTransactionManagerWithQualifierAndDefault() {
BeanFactory beanFactory = mock(BeanFactory.class);
PlatformTransactionManager transactionManager = mock(PlatformTransactionManager.class);
TransactionInterceptor ti = transactionInterceptorWithTransactionManager(transactionManager, beanFactory);
PlatformTransactionManager fooTransactionManager = associateTransactionManager(beanFactory, "fooTransactionManager");
DefaultTransactionAttribute attribute = new DefaultTransactionAttribute();
attribute.setQualifier("fooTransactionManager");
assertSame(fooTransactionManager, ti.determineTransactionManager(attribute));
}
use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.
the class AbstractTransactionAspectTests method noTransaction.
@Test
public void noTransaction() throws Exception {
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
TestBean tb = new TestBean();
TransactionAttributeSource tas = new MapTransactionAttributeSource();
// All the methods in this class use the advised() template method
// to obtain a transaction object, configured with the given PlatformTransactionManager
// and transaction attribute source
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
checkTransactionStatus(false);
itb.getName();
checkTransactionStatus(false);
// expect no calls
verifyZeroInteractions(ptm);
}
use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.
the class AbstractTransactionAspectTests method doTestRollbackOnException.
/**
* Check that the given exception thrown by the target can produce the
* desired behavior with the appropriate transaction attribute.
* @param ex exception to be thrown by the target
* @param shouldRollback whether this should cause a transaction rollback
*/
@SuppressWarnings("serial")
protected void doTestRollbackOnException(final Exception ex, final boolean shouldRollback, boolean rollbackException) throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute() {
@Override
public boolean rollbackOn(Throwable t) {
assertTrue(t == ex);
return shouldRollback;
}
};
Method m = exceptionalMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
TransactionStatus status = mock(TransactionStatus.class);
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
// Gets additional call(s) from TransactionControl
given(ptm.getTransaction(txatt)).willReturn(status);
TransactionSystemException tex = new TransactionSystemException("system exception");
if (rollbackException) {
if (shouldRollback) {
willThrow(tex).given(ptm).rollback(status);
} else {
willThrow(tex).given(ptm).commit(status);
}
}
TestBean tb = new TestBean();
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
try {
itb.exceptional(ex);
fail("Should have thrown exception");
} catch (Throwable t) {
if (rollbackException) {
assertEquals("Caught wrong exception", tex, t);
} else {
assertEquals("Caught wrong exception", ex, t);
}
}
if (!rollbackException) {
if (shouldRollback) {
verify(ptm).rollback(status);
} else {
verify(ptm).commit(status);
}
}
}
use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.
the class AbstractTransactionAspectTests method enclosingTransactionWithNonTransactionMethodOnAdvisedInside.
@Test
public void enclosingTransactionWithNonTransactionMethodOnAdvisedInside() throws Throwable {
TransactionAttribute txatt = new DefaultTransactionAttribute();
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(exceptionalMethod, txatt);
TransactionStatus status = mock(TransactionStatus.class);
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
// Expect a transaction
given(ptm.getTransaction(txatt)).willReturn(status);
final String spouseName = "innerName";
TestBean outer = new TestBean() {
@Override
public void exceptional(Throwable t) throws Throwable {
TransactionInfo ti = TransactionAspectSupport.currentTransactionInfo();
assertTrue(ti.hasTransaction());
assertEquals(spouseName, getSpouse().getName());
}
};
TestBean inner = new TestBean() {
@Override
public String getName() {
// Assert that we're in the inner proxy
TransactionInfo ti = TransactionAspectSupport.currentTransactionInfo();
assertFalse(ti.hasTransaction());
return spouseName;
}
};
ITestBean outerProxy = (ITestBean) advised(outer, ptm, tas);
ITestBean innerProxy = (ITestBean) advised(inner, ptm, tas);
outer.setSpouse(innerProxy);
checkTransactionStatus(false);
// Will invoke inner.getName, which is non-transactional
outerProxy.exceptional(null);
checkTransactionStatus(false);
verify(ptm).commit(status);
}
Aggregations