use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class PersistTest method testCreateExceptionWithGeneratedId.
@Test
public void testCreateExceptionWithGeneratedId() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
NumberedNode dupe = new NumberedNode("dupe");
em.persist(dupe);
em.persist(dupe);
em.getTransaction().commit();
em.close();
em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
em.persist(dupe);
fail();
} catch (PersistenceException poe) {
// verify that an exception is thrown!
}
em.getTransaction().rollback();
em.close();
NumberedNode nondupe = new NumberedNode("nondupe");
nondupe.addChild(dupe);
em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
em.persist(nondupe);
fail();
} catch (PersistenceException poe) {
// verify that an exception is thrown!
}
em.getTransaction().rollback();
em.close();
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class TestConnectionPool method testConnectionPoolDoesNotConsumeAllConnections.
@Test
public void testConnectionPoolDoesNotConsumeAllConnections() {
for (int i = 0; i < CONNECTION_POOL_SIZE + 1; ++i) {
EntityManager entityManager = getOrCreateEntityManager();
try {
for (int j = 0; j < 2; j++) {
try {
final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
final CriteriaQuery<TestEntity> criteriaQuery = builder.createQuery(TestEntity.class);
criteriaQuery.select(criteriaQuery.from(TestEntity.class));
entityManager.createQuery(criteriaQuery).getResultList();
} catch (PersistenceException e) {
if (e.getCause() instanceof SQLGrammarException) {
// expected, the schema was not created
} else {
throw e;
}
}
}
} finally {
entityManager.close();
}
}
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class TransactionJoiningTest method testMultiThreadTransactionTimeout.
/**
* In certain JTA environments (JBossTM, etc.), a background thread (reaper)
* can rollback a transaction if it times out. These timeouts are rare and
* typically come from server failures. However, we need to handle the
* multi-threaded nature of the transaction afterCompletion action.
* Emulate a timeout with a simple afterCompletion call in a thread.
* See HHH-7910
*/
@Test
@TestForIssue(jiraKey = "HHH-7910")
public void testMultiThreadTransactionTimeout() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
EntityManager em = entityManagerFactory().createEntityManager();
final SessionImpl sImpl = em.unwrap(SessionImpl.class);
final CountDownLatch latch = new CountDownLatch(1);
Thread thread = new Thread() {
public void run() {
((JtaTransactionCoordinatorImpl) sImpl.getTransactionCoordinator()).getSynchronizationCallbackCoordinator().afterCompletion(Status.STATUS_ROLLEDBACK);
latch.countDown();
}
};
thread.start();
latch.await();
boolean caught = false;
try {
em.persist(new Book("The Book of Foo", 1));
} catch (PersistenceException e) {
caught = e.getCause().getClass().equals(HibernateException.class);
}
assertTrue(caught);
// Ensure that the connection was closed by the background thread.
caught = false;
try {
em.createQuery("from Book").getResultList();
} catch (PersistenceException e) {
// HHH-9312
caught = true;
} catch (Exception e) {
caught = true;
}
assertTrue(caught);
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
em.close();
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class EmbeddableIntegratorTest method testWithoutIntegrator.
/**
* Throws a mapping exception because DollarValue is not mapped
*/
@Test
public void testWithoutIntegrator() {
SessionFactory sf = new Configuration().addAnnotatedClass(Investor.class).setProperty("hibernate.hbm2ddl.auto", "create-drop").buildSessionFactory();
try {
Session sess = sf.openSession();
try {
sess.getTransaction().begin();
Investor myInv = getInvestor();
myInv.setId(1L);
sess.save(myInv);
sess.flush();
fail("A JDBCException expected");
sess.clear();
Investor inv = (Investor) sess.get(Investor.class, 1L);
assertEquals(new BigDecimal("100"), inv.getInvestments().get(0).getAmount().getAmount());
} catch (PersistenceException e) {
assertTyping(JDBCException.class, e.getCause());
sess.getTransaction().rollback();
}
sess.close();
} finally {
sf.close();
}
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class AssociationOverrideTest method testOverriding.
@Test
public void testOverriding() throws Exception {
Location paris = new Location();
paris.setName("Paris");
Location atlanta = new Location();
atlanta.setName("Atlanta");
Trip trip = new Trip();
trip.setFrom(paris);
// trip.setTo( atlanta );
Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist(paris);
s.persist(atlanta);
try {
s.persist(trip);
s.flush();
fail("Should be non nullable");
} catch (PersistenceException e) {
// success
} finally {
tx.rollback();
s.close();
}
}
Aggregations