use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.
the class AbstractTransactionAspectTests method enclosingTransactionWithNestedTransactionOnAdvisedInside.
@Test
public void enclosingTransactionWithNestedTransactionOnAdvisedInside() throws Throwable {
final TransactionAttribute outerTxatt = new DefaultTransactionAttribute();
final TransactionAttribute innerTxatt = new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_NESTED);
Method outerMethod = exceptionalMethod;
Method innerMethod = getNameMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(outerMethod, outerTxatt);
tas.register(innerMethod, innerTxatt);
TransactionStatus outerStatus = mock(TransactionStatus.class);
TransactionStatus innerStatus = mock(TransactionStatus.class);
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
// Expect a transaction
given(ptm.getTransaction(outerTxatt)).willReturn(outerStatus);
given(ptm.getTransaction(innerTxatt)).willReturn(innerStatus);
final String spouseName = "innerName";
TestBean outer = new TestBean() {
@Override
public void exceptional(Throwable t) throws Throwable {
TransactionInfo ti = TransactionAspectSupport.currentTransactionInfo();
assertTrue(ti.hasTransaction());
assertEquals(outerTxatt, ti.getTransactionAttribute());
assertEquals(spouseName, getSpouse().getName());
}
};
TestBean inner = new TestBean() {
@Override
public String getName() {
// Assert that we're in the inner proxy
TransactionInfo ti = TransactionAspectSupport.currentTransactionInfo();
// Has nested transaction
assertTrue(ti.hasTransaction());
assertEquals(innerTxatt, ti.getTransactionAttribute());
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(innerStatus);
verify(ptm).commit(outerStatus);
}
use of org.springframework.transaction.PlatformTransactionManager in project spring-framework by spring-projects.
the class AbstractTransactionAspectTests method programmaticRollback.
/**
* Test that TransactionStatus.setRollbackOnly works.
*/
@Test
public void programmaticRollback() throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute();
Method m = getNameMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
TransactionStatus status = mock(TransactionStatus.class);
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
given(ptm.getTransaction(txatt)).willReturn(status);
final String name = "jenny";
TestBean tb = new TestBean() {
@Override
public String getName() {
TransactionStatus txStatus = TransactionInterceptor.currentTransactionStatus();
txStatus.setRollbackOnly();
return name;
}
};
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
// verification!?
assertTrue(name.equals(itb.getName()));
verify(ptm).commit(status);
}
use of org.springframework.transaction.PlatformTransactionManager in project spring-boot by spring-projects.
the class TransactionAutoConfigurationTests method singleTransactionManager.
@Test
public void singleTransactionManager() {
load(DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class);
PlatformTransactionManager transactionManager = this.context.getBean(PlatformTransactionManager.class);
TransactionTemplate transactionTemplate = this.context.getBean(TransactionTemplate.class);
assertThat(transactionTemplate.getTransactionManager()).isSameAs(transactionManager);
}
use of org.springframework.transaction.PlatformTransactionManager in project grails-core by grails.
the class ChainedTransactionManager method getTransaction.
/*
* (non-Javadoc)
* @see org.springframework.transaction.PlatformTransactionManager#getTransaction(org.springframework.transaction.TransactionDefinition)
*/
public MultiTransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
MultiTransactionStatus mts = new MultiTransactionStatus(transactionManagers.get(0));
if (!synchronizationManager.isSynchronizationActive() && canCreateTransaction(definition)) {
synchronizationManager.initSynchronization();
mts.setNewSynchonization();
}
try {
for (PlatformTransactionManager transactionManager : transactionManagers) {
mts.registerTransactionManager(definition, transactionManager);
}
} catch (Exception ex) {
Map<PlatformTransactionManager, TransactionStatus> transactionStatuses = mts.getTransactionStatuses();
for (PlatformTransactionManager transactionManager : transactionManagers) {
try {
if (transactionStatuses.get(transactionManager) != null) {
transactionManager.rollback(transactionStatuses.get(transactionManager));
}
} catch (Exception ex2) {
LOGGER.warn("Rollback exception (" + transactionManager + ") " + ex2.getMessage(), ex2);
}
}
if (mts.isNewSynchonization()) {
synchronizationManager.clearSynchronization();
}
throw new CannotCreateTransactionException(ex.getMessage(), ex);
}
return mts;
}
use of org.springframework.transaction.PlatformTransactionManager in project grails-core by grails.
the class ChainedTransactionManager method rollback.
/*
* (non-Javadoc)
* @see org.springframework.transaction.PlatformTransactionManager#rollback(org.springframework.transaction.TransactionStatus)
*/
public void rollback(TransactionStatus status) throws TransactionException {
Exception rollbackException = null;
PlatformTransactionManager rollbackExceptionTransactionManager = null;
MultiTransactionStatus multiTransactionStatus = (MultiTransactionStatus) status;
for (PlatformTransactionManager transactionManager : reverse(transactionManagers)) {
try {
multiTransactionStatus.rollback(transactionManager);
} catch (Exception ex) {
if (rollbackException == null) {
rollbackException = ex;
rollbackExceptionTransactionManager = transactionManager;
} else {
LOGGER.warn("Rollback exception (" + transactionManager + ") " + ex.getMessage(), ex);
}
}
}
if (multiTransactionStatus.isNewSynchonization()) {
synchronizationManager.clearSynchronization();
}
if (rollbackException != null) {
throw new UnexpectedRollbackException("Rollback exception, originated at (" + rollbackExceptionTransactionManager + ") " + rollbackException.getMessage(), rollbackException);
}
}
Aggregations