use of cn.taketoday.transaction.PlatformTransactionManager in project today-infrastructure by TAKETODAY.
the class TransactionTemplate method execute.
@Override
@Nullable
public <T> T execute(TransactionCallback<T> action) throws TransactionException {
PlatformTransactionManager transactionManager = getTransactionManager();
Assert.state(transactionManager != null, "No PlatformTransactionManager set");
if (transactionManager instanceof CallbackPreferringPlatformTransactionManager) {
return ((CallbackPreferringPlatformTransactionManager) transactionManager).execute(this, action);
} else {
TransactionStatus status = transactionManager.getTransaction(this);
T result;
try {
result = action.doInTransaction(status);
} catch (RuntimeException | Error ex) {
// Transactional code threw application exception -> rollback
rollbackOnException(transactionManager, status, ex);
throw ex;
} catch (Throwable ex) {
// Transactional code threw unexpected exception -> rollback
rollbackOnException(transactionManager, status, ex);
throw new UndeclaredThrowableException(ex, "TransactionCallback threw undeclared checked exception");
}
transactionManager.commit(status);
return result;
}
}
use of cn.taketoday.transaction.PlatformTransactionManager in project today-infrastructure by TAKETODAY.
the class AbstractTransactionAspectTests method transactionShouldSucceed.
/**
* Check that a transaction is created and committed.
*/
@Test
public void transactionShouldSucceed() throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute();
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(getNameMethod, txatt);
TransactionStatus status = mock(TransactionStatus.class);
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
// expect a transaction
given(ptm.getTransaction(txatt)).willReturn(status);
TestBean tb = new TestBean();
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
checkTransactionStatus(false);
itb.getName();
checkTransactionStatus(false);
verify(ptm).commit(status);
}
use of cn.taketoday.transaction.PlatformTransactionManager in project today-infrastructure by TAKETODAY.
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) {
assertThat(t == ex).isTrue();
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) {
assertThat(t).as("Caught wrong exception").isEqualTo(tex);
} else {
assertThat(t).as("Caught wrong exception").isEqualTo(ex);
}
}
if (!rollbackException) {
if (shouldRollback) {
verify(ptm).rollback(status);
} else {
verify(ptm).commit(status);
}
}
}
use of cn.taketoday.transaction.PlatformTransactionManager in project today-infrastructure by TAKETODAY.
the class AbstractTransactionAspectTests method twoTransactionsShouldSucceed.
/**
* Check that two transactions are created and committed.
*/
@Test
public void twoTransactionsShouldSucceed() throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute();
MapTransactionAttributeSource tas1 = new MapTransactionAttributeSource();
tas1.register(getNameMethod, txatt);
MapTransactionAttributeSource tas2 = new MapTransactionAttributeSource();
tas2.register(setNameMethod, txatt);
TransactionStatus status = mock(TransactionStatus.class);
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
// expect a transaction
given(ptm.getTransaction(txatt)).willReturn(status);
TestBean tb = new TestBean();
ITestBean itb = (ITestBean) advised(tb, ptm, new TransactionAttributeSource[] { tas1, tas2 });
checkTransactionStatus(false);
itb.getName();
checkTransactionStatus(false);
itb.setName("myName");
checkTransactionStatus(false);
verify(ptm, times(2)).commit(status);
}
use of cn.taketoday.transaction.PlatformTransactionManager in project today-infrastructure by TAKETODAY.
the class AbstractTransactionAspectTests method transactionShouldSucceedWithNotNew.
/**
* Check that a transaction is created and committed.
*/
@Test
public void transactionShouldSucceedWithNotNew() throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute();
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(getNameMethod, txatt);
TransactionStatus status = mock(TransactionStatus.class);
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
// expect a transaction
given(ptm.getTransaction(txatt)).willReturn(status);
TestBean tb = new TestBean();
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
checkTransactionStatus(false);
// verification!?
itb.getName();
checkTransactionStatus(false);
verify(ptm).commit(status);
}
Aggregations