use of org.hibernate.TransactionException in project hibernate-orm by hibernate.
the class JtaTransactionAdapterUserTransactionImpl method rollback.
@Override
public void rollback() {
try {
if (initiator) {
initiator = false;
log.trace("Calling UserTransaction#rollback");
userTransaction.rollback();
log.trace("Called UserTransaction#rollback");
} else {
markRollbackOnly();
}
} catch (Exception e) {
throw new TransactionException("JTA UserTransaction#rollback failed", e);
}
}
use of org.hibernate.TransactionException in project hibernate-orm by hibernate.
the class JtaTransactionAdapterUserTransactionImpl method begin.
@Override
public void begin() {
try {
if (getStatus() == TransactionStatus.NOT_ACTIVE) {
log.trace("Calling UserTransaction#begin");
userTransaction.begin();
initiator = true;
log.trace("Called UserTransaction#begin");
} else {
log.trace("Skipping TransactionManager#begin due to already active transaction");
}
} catch (Exception e) {
throw new TransactionException("JTA UserTransaction#begin failed", e);
}
}
use of org.hibernate.TransactionException in project hibernate-orm by hibernate.
the class FooBarTest method testLazyCollectionsTouchedDuringPreCommit.
@Test
@TestForIssue(jiraKey = "HHH-7603")
public void testLazyCollectionsTouchedDuringPreCommit() throws Exception {
Session s = openSession();
s.beginTransaction();
Qux q = new Qux();
s.save(q);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
q = (Qux) s.load(Qux.class, q.getKey());
s.getTransaction().commit();
// clear the session
s.clear();
// now reload the proxy and delete it
s.beginTransaction();
final Qux qToDelete = (Qux) s.load(Qux.class, q.getKey());
// register a pre commit process that will touch the collection and delete the entity
((EventSource) s).getActionQueue().registerProcess(new BeforeTransactionCompletionProcess() {
@Override
public void doBeforeTransactionCompletion(SessionImplementor session) {
qToDelete.getFums().size();
}
});
s.delete(qToDelete);
boolean ok = false;
try {
s.getTransaction().commit();
} catch (LazyInitializationException e) {
ok = true;
s.getTransaction().rollback();
} catch (TransactionException te) {
if (te.getCause() instanceof LazyInitializationException) {
ok = true;
}
s.getTransaction().rollback();
} finally {
s.close();
}
assertTrue("lazy collection should have blown in the before trans completion", ok);
s = openSession();
s.beginTransaction();
q = (Qux) s.load(Qux.class, q.getKey());
s.delete(q);
s.getTransaction().commit();
s.close();
}
use of org.hibernate.TransactionException in project hibernate-orm by hibernate.
the class BasicJdbcTransactionTests method testMarkRollbackOnly.
@Test
@SuppressWarnings("EmptyCatchBlock")
public void testMarkRollbackOnly() {
try (final SessionFactoryImplementor sf = generateSessionFactory()) {
inSession(sf, session -> {
final TransactionCoordinator coordinator = session.getTransactionCoordinator();
assertEquals(TransactionStatus.NOT_ACTIVE, coordinator.getTransactionDriverControl().getStatus());
session.getTransaction().begin();
assertEquals(TransactionStatus.ACTIVE, coordinator.getTransactionDriverControl().getStatus());
session.getTransaction().markRollbackOnly();
assertEquals(TransactionStatus.MARKED_ROLLBACK, coordinator.getTransactionDriverControl().getStatus());
try {
session.getTransaction().commit();
} catch (TransactionException expected) {
} finally {
assertThat(coordinator.getTransactionDriverControl().getStatus(), anyOf(is(TransactionStatus.NOT_ACTIVE), is(TransactionStatus.ROLLED_BACK)));
}
});
}
}
use of org.hibernate.TransactionException in project hibernate-orm by hibernate.
the class AbstractLogicalConnectionImplementor method begin.
@Override
public void begin() {
try {
if (!doConnectionsFromProviderHaveAutoCommitDisabled()) {
log.trace("Preparing to begin transaction via JDBC Connection.setAutoCommit(false)");
getConnectionForTransactionManagement().setAutoCommit(false);
log.trace("Transaction begun via JDBC Connection.setAutoCommit(false)");
}
status = TransactionStatus.ACTIVE;
} catch (SQLException e) {
throw new TransactionException("JDBC begin transaction failed: ", e);
}
}
Aggregations