use of javax.persistence.RollbackException in project spring-framework by spring-projects.
the class JpaTransactionManager method doCommit.
@Override
protected void doCommit(DefaultTransactionStatus status) {
JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
if (status.isDebug()) {
logger.debug("Committing JPA transaction on EntityManager [" + txObject.getEntityManagerHolder().getEntityManager() + "]");
}
try {
EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
tx.commit();
} catch (RollbackException ex) {
if (ex.getCause() instanceof RuntimeException) {
DataAccessException dex = getJpaDialect().translateExceptionIfPossible((RuntimeException) ex.getCause());
if (dex != null) {
throw dex;
}
}
throw new TransactionSystemException("Could not commit JPA transaction", ex);
} catch (RuntimeException ex) {
// Assumably failed to flush changes to database.
throw DataAccessUtils.translateIfNecessary(ex, getJpaDialect());
}
}
use of javax.persistence.RollbackException in project spring-framework by spring-projects.
the class JpaTransactionManagerTests method testTransactionCommitWithRollbackException.
@Test
public void testTransactionCommitWithRollbackException() {
given(manager.getTransaction()).willReturn(tx);
given(tx.getRollbackOnly()).willReturn(true);
willThrow(new RollbackException()).given(tx).commit();
final List<String> l = new ArrayList<>();
l.add("test");
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
try {
Object result = tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
}
});
assertSame(l, result);
} catch (TransactionSystemException tse) {
// expected
assertTrue(tse.getCause() instanceof RollbackException);
}
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
verify(manager).flush();
verify(manager).close();
}
use of javax.persistence.RollbackException in project spring-framework by spring-projects.
the class JpaTransactionManagerTests method testParticipatingTransactionWithRollbackOnly.
@Test
public void testParticipatingTransactionWithRollbackOnly() {
given(manager.getTransaction()).willReturn(tx);
given(tx.isActive()).willReturn(true);
given(tx.getRollbackOnly()).willReturn(true);
willThrow(new RollbackException()).given(tx).commit();
final List<String> l = new ArrayList<>();
l.add("test");
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
try {
tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
status.setRollbackOnly();
return null;
}
});
}
});
fail("Should have thrown TransactionSystemException");
} catch (TransactionSystemException tse) {
// expected
assertTrue(tse.getCause() instanceof RollbackException);
}
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
verify(manager).flush();
verify(tx).setRollbackOnly();
verify(manager).close();
}
use of javax.persistence.RollbackException in project hibernate-orm by hibernate.
the class MultiCircleJpaCascadeTest method testPersistThenUpdateNoCascadeToTransient.
@Test
public void testPersistThenUpdateNoCascadeToTransient() {
// expected to fail, so nothing will be persisted.
skipCleanup = true;
// remove elements from collections and persist
c.getBCollection().clear();
c.getDCollection().clear();
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist(c);
// now add the elements back
c.getBCollection().add(b);
c.getDCollection().add(d);
try {
em.getTransaction().commit();
fail("should have thrown IllegalStateException");
} catch (RollbackException ex) {
assertTrue(ex.getCause() instanceof IllegalStateException);
IllegalStateException ise = (IllegalStateException) ex.getCause();
// should fail on entity g (due to no cascade to f.g);
// instead it fails on entity e ( due to no cascade to d.e)
// because e is not in the process of being saved yet.
// when HHH-6999 is fixed, this test should be changed to
// check for g and f.g
//noinspection ThrowableResultOfMethodCallIgnored
TransientPropertyValueException tpve = assertTyping(TransientPropertyValueException.class, ise.getCause());
assertEquals(E.class.getName(), tpve.getTransientEntityName());
assertEquals(D.class.getName(), tpve.getPropertyOwnerEntityName());
assertEquals("e", tpve.getPropertyName());
}
em.close();
}
use of javax.persistence.RollbackException in project hibernate-orm by hibernate.
the class PersistTest method testCreateException.
@Test
public void testCreateException() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Node dupe = new Node("dupe");
em.persist(dupe);
em.persist(dupe);
em.getTransaction().commit();
em.close();
em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist(dupe);
try {
em.getTransaction().commit();
fail("Cannot persist() twice the same entity");
} catch (Exception cve) {
//verify that an exception is thrown!
}
em.close();
Node nondupe = new Node("nondupe");
nondupe.addChild(dupe);
em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist(nondupe);
try {
em.getTransaction().commit();
assertFalse(true);
} catch (RollbackException e) {
//verify that an exception is thrown!
}
em.close();
}
Aggregations