use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.
the class AbstractNonFunctionalTest method withTx.
protected <T> T withTx(NodeEnvironment environment, SharedSessionContractImplementor session, Callable<T> callable) throws Exception {
if (jtaPlatform != null) {
TransactionManager tm = environment.getServiceRegistry().getService(JtaPlatform.class).retrieveTransactionManager();
return Caches.withinTx(tm, callable);
} else {
Transaction transaction = ((Session) session).beginTransaction();
boolean rollingBack = false;
try {
T retval = callable.call();
if (transaction.getStatus() == TransactionStatus.ACTIVE) {
transaction.commit();
} else {
rollingBack = true;
transaction.rollback();
}
return retval;
} catch (Exception e) {
if (!rollingBack) {
try {
transaction.rollback();
} catch (Exception suppressed) {
e.addSuppressed(suppressed);
}
}
throw e;
}
}
}
use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.
the class DdlInWildFlyUsingBmtAndEmfTest method testCreateThenDrop.
@Test
public void testCreateThenDrop() throws Exception {
URL persistenceXmlUrl = Thread.currentThread().getContextClassLoader().getResource(PERSISTENCE_XML_RESOURCE_NAME);
if (persistenceXmlUrl == null) {
persistenceXmlUrl = Thread.currentThread().getContextClassLoader().getResource('/' + PERSISTENCE_XML_RESOURCE_NAME);
}
assertNotNull(persistenceXmlUrl);
ParsedPersistenceXmlDescriptor persistenceUnit = PersistenceXmlParser.locateIndividualPersistenceUnit(persistenceXmlUrl);
// creating the EMF causes SchemaCreator to be run...
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(persistenceUnit, Collections.emptyMap()).build();
// closing the EMF causes the delayed SchemaDropper to be run...
// wrap in a transaction just to see if we can get this to fail in the way the WF report says;
// in my experience however this succeeds with or without the transaction
final TransactionManager tm = emf.unwrap(SessionFactoryImplementor.class).getServiceRegistry().getService(JtaPlatform.class).retrieveTransactionManager();
tm.begin();
Transaction txn = tm.getTransaction();
emf.close();
txn.commit();
}
use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.
the class BeforeCompletionFailureTest method testUniqueConstraintViolationDuringManagedFlush.
@Test
@TestForIssue(jiraKey = "HHH-9888")
public void testUniqueConstraintViolationDuringManagedFlush() throws Exception {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// set up test data
Session session = openSession();
session.getTransaction().begin();
session.save(newEntity(1));
session.getTransaction().commit();
session.close();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// do the test
final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
// begin the transaction ("CMT" style)
tm.begin();
session = openSession();
session.save(newEntity(2));
// which should lead to the UK violation
try {
tm.commit();
fail("Expecting a failure from JTA commit");
} catch (RollbackException expected) {
log.info("Test encountered expected JTA RollbackException; looking for nested JDBCException", expected);
boolean violationExceptionFound = false;
Throwable cause = expected;
while (cause != null) {
if (cause instanceof JDBCException) {
log.info("Found JDBCException, assuming related to UK violation", cause);
violationExceptionFound = true;
break;
}
cause = cause.getCause();
}
if (!violationExceptionFound) {
fail("Did not find JDBCException in JTA RollbackException chain");
}
} finally {
if (!((SessionImplementor) session).isClosed()) {
session.close();
}
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// clean up test data
session = openSession();
session.getTransaction().begin();
session.createQuery("delete SimpleEntity").executeUpdate();
session.getTransaction().commit();
session.close();
}
use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.
the class TxUtil method withTxSession.
public static void withTxSession(JtaPlatform jtaPlatform, SessionBuilder sessionBuilder, ThrowingConsumer<Session, Exception> consumer) throws Exception {
if (jtaPlatform != null) {
TransactionManager tm = jtaPlatform.retrieveTransactionManager();
final SessionBuilder sb = sessionBuilder;
Caches.withinTx(tm, () -> {
withSession(sb, s -> {
consumer.accept(s);
s.flush();
});
return null;
});
} else {
withSession(sessionBuilder, s -> withResourceLocalTx(s, consumer));
}
}
use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.
the class TxUtil method withTxSessionApply.
public static <T> T withTxSessionApply(JtaPlatform jtaPlatform, SessionBuilder sessionBuilder, ThrowingFunction<Session, T, Exception> function) throws Exception {
if (jtaPlatform != null) {
TransactionManager tm = jtaPlatform.retrieveTransactionManager();
Callable<T> callable = () -> withSessionApply(sessionBuilder, s -> {
T t = function.apply(s);
s.flush();
return t;
});
return Caches.withinTx(tm, callable);
} else {
return withSessionApply(sessionBuilder, s -> withResourceLocalTx(s, function));
}
}
Aggregations