use of cn.taketoday.transaction.CannotCreateTransactionException in project today-infrastructure by TAKETODAY.
the class AbstractTransactionAspectTests method cannotCreateTransaction.
/**
* Simulate a transaction infrastructure failure.
* Shouldn't invoke target method.
*/
@Test
public void cannotCreateTransaction() throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute();
Method m = getNameMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
// Expect a transaction
CannotCreateTransactionException ex = new CannotCreateTransactionException("foobar", null);
given(ptm.getTransaction(txatt)).willThrow(ex);
TestBean tb = new TestBean() {
@Override
public String getName() {
throw new UnsupportedOperationException("Shouldn't have invoked target method when couldn't create transaction for transactional method");
}
};
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
try {
itb.getName();
fail("Shouldn't have invoked method");
} catch (CannotCreateTransactionException thrown) {
assertThat(thrown == ex).isTrue();
}
}
use of cn.taketoday.transaction.CannotCreateTransactionException in project today-infrastructure by TAKETODAY.
the class DataSourceTransactionManager method doBegin.
@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction;
Connection con = null;
try {
if (!txObject.hasConnectionHolder() || txObject.getConnectionHolder().isSynchronizedWithTransaction()) {
Connection newCon = obtainDataSource().getConnection();
if (logger.isDebugEnabled()) {
logger.debug("Acquired Connection [{}] for JDBC transaction", newCon);
}
txObject.setConnectionHolder(new ConnectionHolder(newCon), true);
}
ConnectionHolder connectionHolder = txObject.getConnectionHolder();
connectionHolder.setSynchronizedWithTransaction(true);
con = connectionHolder.getConnection();
Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
txObject.setPreviousIsolationLevel(previousIsolationLevel);
txObject.setReadOnly(definition.isReadOnly());
// configured the connection pool to set it already).
if (con.getAutoCommit()) {
txObject.setMustRestoreAutoCommit(true);
if (logger.isDebugEnabled()) {
logger.debug("Switching JDBC Connection [{}] to manual commit", con);
}
con.setAutoCommit(false);
}
prepareTransactionalConnection(con, definition);
connectionHolder.setTransactionActive(true);
int timeout = determineTimeout(definition);
if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
connectionHolder.setTimeoutInSeconds(timeout);
}
// Bind the connection holder to the thread.
if (txObject.isNewConnectionHolder()) {
TransactionSynchronizationManager.bindResource(obtainDataSource(), connectionHolder);
}
} catch (Throwable ex) {
if (txObject.isNewConnectionHolder()) {
DataSourceUtils.releaseConnection(con, obtainDataSource());
txObject.setConnectionHolder(null, false);
}
throw new CannotCreateTransactionException("Could not open JDBC Connection for transaction", ex);
}
}
use of cn.taketoday.transaction.CannotCreateTransactionException in project today-framework by TAKETODAY.
the class AbstractTransactionAspectTests method cannotCreateTransaction.
/**
* Simulate a transaction infrastructure failure.
* Shouldn't invoke target method.
*/
@Test
public void cannotCreateTransaction() throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute();
Method m = getNameMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
// Expect a transaction
CannotCreateTransactionException ex = new CannotCreateTransactionException("foobar", null);
given(ptm.getTransaction(txatt)).willThrow(ex);
TestBean tb = new TestBean() {
@Override
public String getName() {
throw new UnsupportedOperationException("Shouldn't have invoked target method when couldn't create transaction for transactional method");
}
};
ITestBean itb = (ITestBean) advised(tb, ptm, tas);
try {
itb.getName();
fail("Shouldn't have invoked method");
} catch (CannotCreateTransactionException thrown) {
assertThat(thrown == ex).isTrue();
}
}
use of cn.taketoday.transaction.CannotCreateTransactionException in project today-framework by TAKETODAY.
the class DataSourceTransactionManager method doBegin.
@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction;
Connection con = null;
try {
if (!txObject.hasConnectionHolder() || txObject.getConnectionHolder().isSynchronizedWithTransaction()) {
Connection newCon = obtainDataSource().getConnection();
if (logger.isDebugEnabled()) {
logger.debug("Acquired Connection [{}] for JDBC transaction", newCon);
}
txObject.setConnectionHolder(new ConnectionHolder(newCon), true);
}
ConnectionHolder connectionHolder = txObject.getConnectionHolder();
connectionHolder.setSynchronizedWithTransaction(true);
con = connectionHolder.getConnection();
Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition);
txObject.setPreviousIsolationLevel(previousIsolationLevel);
txObject.setReadOnly(definition.isReadOnly());
// configured the connection pool to set it already).
if (con.getAutoCommit()) {
txObject.setMustRestoreAutoCommit(true);
if (logger.isDebugEnabled()) {
logger.debug("Switching JDBC Connection [{}] to manual commit", con);
}
con.setAutoCommit(false);
}
prepareTransactionalConnection(con, definition);
connectionHolder.setTransactionActive(true);
int timeout = determineTimeout(definition);
if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
connectionHolder.setTimeoutInSeconds(timeout);
}
// Bind the connection holder to the thread.
if (txObject.isNewConnectionHolder()) {
TransactionSynchronizationManager.bindResource(obtainDataSource(), connectionHolder);
}
} catch (Throwable ex) {
if (txObject.isNewConnectionHolder()) {
DataSourceUtils.releaseConnection(con, obtainDataSource());
txObject.setConnectionHolder(null, false);
}
throw new CannotCreateTransactionException("Could not open JDBC Connection for transaction", ex);
}
}
use of cn.taketoday.transaction.CannotCreateTransactionException in project today-framework by TAKETODAY.
the class AbstractReactiveTransactionAspectTests method cannotCreateTransaction.
/**
* Simulate a transaction infrastructure failure.
* Shouldn't invoke target method.
*/
@Test
public void cannotCreateTransaction() throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute();
Method m = getNameMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);
// Expect a transaction
CannotCreateTransactionException ex = new CannotCreateTransactionException("foobar", null);
given(rtm.getReactiveTransaction(txatt)).willThrow(ex);
DefaultTestBean tb = new DefaultTestBean() {
@Override
public Mono<String> getName() {
throw new UnsupportedOperationException("Shouldn't have invoked target method when couldn't create transaction for transactional method");
}
};
TestBean itb = (TestBean) advised(tb, rtm, tas);
itb.getName().as(StepVerifier::create).expectError(CannotCreateTransactionException.class).verify();
}
Aggregations