use of javax.transaction.TransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndExistingWithBeginException.
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExistingWithBeginException() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
TransactionManager tm = mock(TransactionManager.class);
Transaction tx = mock(Transaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
given(tm.suspend()).willReturn(tx);
willThrow(new SystemException()).given(ut).begin();
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 CannotCreateTransactionException");
} catch (CannotCreateTransactionException ex) {
// expected
}
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
verify(tm).resume(tx);
}
use of javax.transaction.TransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingTransactionAndJtaSynchronization.
@Test
public void jtaTransactionManagerWithExistingTransactionAndJtaSynchronization() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
TransactionManager tm = mock(TransactionManager.class);
MockJtaTransaction tx = new MockJtaTransaction();
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
given(tm.getTransaction()).willReturn(tx);
final TransactionSynchronization synch = mock(TransactionSynchronization.class);
JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
TransactionTemplate tt = new TransactionTemplate(ptm);
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
TransactionSynchronizationManager.registerSynchronization(synch);
status.setRollbackOnly();
}
});
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
assertNotNull(tx.getSynchronization());
tx.getSynchronization().beforeCompletion();
tx.getSynchronization().afterCompletion(Status.STATUS_ROLLEDBACK);
verify(ut).setRollbackOnly();
verify(synch).beforeCompletion();
verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
}
use of javax.transaction.TransactionManager in project spring-framework by spring-projects.
the class JtaTransactionManagerSerializationTests method serializable.
@Test
public void serializable() throws Exception {
UserTransaction ut1 = mock(UserTransaction.class);
UserTransaction ut2 = mock(UserTransaction.class);
TransactionManager tm = mock(TransactionManager.class);
JtaTransactionManager jtam = new JtaTransactionManager();
jtam.setUserTransaction(ut1);
jtam.setTransactionManager(tm);
jtam.setRollbackOnCommitFailure(true);
jtam.afterPropertiesSet();
SimpleNamingContextBuilder jndiEnv = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
jndiEnv.bind(JtaTransactionManager.DEFAULT_USER_TRANSACTION_NAME, ut2);
JtaTransactionManager serializedJtatm = (JtaTransactionManager) SerializationTestUtils.serializeAndDeserialize(jtam);
// should do client-side lookup
assertNotNull("Logger must survive serialization", serializedJtatm.logger);
assertTrue("UserTransaction looked up on client", serializedJtatm.getUserTransaction() == ut2);
assertNull("TransactionManager didn't survive", serializedJtatm.getTransactionManager());
assertEquals(true, serializedJtatm.isRollbackOnCommitFailure());
}
use of javax.transaction.TransactionManager in project blueprints by tinkerpop.
the class Neo4jGraph method rollback.
public void rollback() {
if (null == tx.get()) {
return;
}
GraphDatabaseAPI graphDatabaseAPI = (GraphDatabaseAPI) getRawGraph();
TransactionManager transactionManager = graphDatabaseAPI.getTxManager();
try {
javax.transaction.Transaction t = transactionManager.getTransaction();
if (t == null || t.getStatus() == Status.STATUS_ROLLEDBACK) {
return;
}
tx.get().failure();
} catch (SystemException e) {
throw new RuntimeException(e);
} finally {
tx.get().finish();
tx.remove();
}
}
use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.
the class JTASessionContext method currentSession.
@Override
public Session currentSession() throws HibernateException {
final JtaPlatform jtaPlatform = factory().getServiceRegistry().getService(JtaPlatform.class);
final TransactionManager transactionManager = jtaPlatform.retrieveTransactionManager();
if (transactionManager == null) {
throw new HibernateException("No TransactionManagerLookup specified");
}
Transaction txn;
try {
txn = transactionManager.getTransaction();
if (txn == null) {
throw new HibernateException("Unable to locate current JTA transaction");
}
if (!JtaStatusHelper.isActive(txn.getStatus())) {
// entries cleaned up (aside from spawning threads).
throw new HibernateException("Current transaction is not in progress");
}
} catch (HibernateException e) {
throw e;
} catch (Throwable t) {
throw new HibernateException("Problem locating/validating JTA transaction", t);
}
final Object txnIdentifier = jtaPlatform.getTransactionIdentifier(txn);
Session currentSession = currentSessionMap.get(txnIdentifier);
if (currentSession == null) {
currentSession = buildOrObtainSession();
try {
txn.registerSynchronization(buildCleanupSynch(txnIdentifier));
} catch (Throwable t) {
try {
currentSession.close();
} catch (Throwable ignore) {
LOG.debug("Unable to release generated current-session on failed synch registration", ignore);
}
throw new HibernateException("Unable to register cleanup Synchronization with TransactionManager");
}
currentSessionMap.put(txnIdentifier, currentSession);
} else {
validateExistingSession(currentSession);
}
return currentSession;
}
Aggregations