use of javax.transaction.SystemException in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndExistingWithSuspendException.
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExistingWithSuspendException() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
TransactionManager tm = mock(TransactionManager.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
willThrow(new SystemException()).given(tm).suspend();
JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
try {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
}
});
fail("Should have thrown TransactionSystemException");
} catch (TransactionSystemException ex) {
// expected
}
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
use of javax.transaction.SystemException in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithSystemExceptionOnCommit.
@Test
public void jtaTransactionManagerWithSystemExceptionOnCommit() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
willThrow(new SystemException("system exception")).given(ut).commit();
try {
JtaTransactionManager ptm = newJtaTransactionManager(ut);
TransactionTemplate tt = new TransactionTemplate(ptm);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
// something transactional
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCompletion(int status) {
assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_UNKNOWN);
}
});
}
});
fail("Should have thrown TransactionSystemException");
} catch (TransactionSystemException ex) {
// expected
}
verify(ut).begin();
}
use of javax.transaction.SystemException in project spring-framework by spring-projects.
the class SpringSessionContext method currentSession.
/**
* Retrieve the Spring-managed Session for the current thread, if any.
*/
@Override
@SuppressWarnings("deprecation")
public Session currentSession() throws HibernateException {
Object value = TransactionSynchronizationManager.getResource(this.sessionFactory);
if (value instanceof Session) {
return (Session) value;
} else if (value instanceof SessionHolder) {
SessionHolder sessionHolder = (SessionHolder) value;
Session session = sessionHolder.getSession();
if (!sessionHolder.isSynchronizedWithTransaction() && TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.registerSynchronization(new SpringSessionSynchronization(sessionHolder, this.sessionFactory, false));
sessionHolder.setSynchronizedWithTransaction(true);
// Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
// with FlushMode.MANUAL, which needs to allow flushing within the transaction.
FlushMode flushMode = SessionFactoryUtils.getFlushMode(session);
if (flushMode.equals(FlushMode.MANUAL) && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
session.setFlushMode(FlushMode.AUTO);
sessionHolder.setPreviousFlushMode(flushMode);
}
}
return session;
}
if (this.transactionManager != null) {
try {
if (this.transactionManager.getStatus() == Status.STATUS_ACTIVE) {
Session session = this.jtaSessionContext.currentSession();
if (TransactionSynchronizationManager.isSynchronizationActive()) {
TransactionSynchronizationManager.registerSynchronization(new SpringFlushSynchronization(session));
}
return session;
}
} catch (SystemException ex) {
throw new HibernateException("JTA TransactionManager found but status check failed", ex);
}
}
if (TransactionSynchronizationManager.isSynchronizationActive()) {
Session session = this.sessionFactory.openSession();
if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
session.setFlushMode(FlushMode.MANUAL);
}
SessionHolder sessionHolder = new SessionHolder(session);
TransactionSynchronizationManager.registerSynchronization(new SpringSessionSynchronization(sessionHolder, this.sessionFactory, true));
TransactionSynchronizationManager.bindResource(this.sessionFactory, sessionHolder);
sessionHolder.setSynchronizedWithTransaction(true);
return session;
} else {
throw new HibernateException("Could not obtain transaction-synchronized Session for current thread");
}
}
use of javax.transaction.SystemException in project wildfly by wildfly.
the class JpaTestSlsb method insertRecord.
public void insertRecord(boolean doRollback) throws SQLException {
Connection conn = null;
Statement statement = null;
try {
userTransaction.begin();
Assert.assertNotNull(dataSource);
conn = dataSource.getConnection();
statement = conn.createStatement();
statement.executeUpdate("INSERT INTO test_table(description, type) VALUES ('add in bean', '2')");
if (doRollback) {
throw new IllegalStateException("Rollback!");
}
userTransaction.commit();
} catch (Exception e) {
try {
userTransaction.rollback();
} catch (IllegalStateException | SecurityException | SystemException e1) {
log.warn(e1.getMessage());
}
} finally {
closeStatement(statement);
closeConnection(conn);
}
}
use of javax.transaction.SystemException in project wildfly by wildfly.
the class TransactionInflowMdb method onMessage.
public void onMessage(Message msg) {
String text = getText(msg);
log.tracef("%s.onMessage with message: %s[%s]", this.getClass().getSimpleName(), text, msg);
try {
Transaction tx = transactionManager.getTransaction();
if (tx == null || tx.getStatus() != Status.STATUS_ACTIVE) {
log.error("Test method called without an active transaction!");
throw new IllegalStateException("Test method called without an active transaction!");
}
} catch (SystemException e) {
log.error("Cannot get the current transaction!", e);
throw new RuntimeException("Cannot get the current transaction!", e);
}
enlistXAResource();
enlistXAResource();
checker.addMessage(text);
log.tracef("Message '%s' processed", text);
}
Aggregations