protected void active(final Session session, final Consumer<Transaction> active, final Consumer<Transaction> inactive) { Transaction trx = session.getTransaction(); if (trx.isActive()) { active.accept(trx); } else { inactive.accept(trx); } }
@Test(expected = IllegalStateException.class) public void anIllegalStateExceptionShouldBeThrownWhenBeginTxIsCalledWithAnAlreadyActiveTX() throws Exception { try (Session s = openSession()) { tm.begin(); Transaction tx = null; try { // A call to begin() with an active Tx should cause an IllegalStateException tx = s.beginTransaction(); } catch (Exception e) { if ( tx != null && tx.isActive() ) { tx.rollback(); } throw e; } } catch (Exception e) { if ( tm.getStatus() == Status.STATUS_ACTIVE ) { tm.rollback(); } throw e; } } }
@Override protected void prepareTest() throws Exception { try (Session session = openSession()) { session.getTransaction().begin(); try { session.save( new MyEntity( 1L, "entity_1", new BigInteger( "3" ) ) ); session.save( new MyEntity( 2L, "entity_2", new BigInteger( "6" ) ) ); session.getTransaction().commit(); } catch (Exception e) { if ( session.getTransaction().isActive() ) { session.getTransaction().rollback(); } throw e; } } }
@Override protected void prepareTest() throws Exception { try (Session session = openSession()) { session.beginTransaction(); try { for ( int i = 0; i < 20; i++ ) { Person p1 = new Person( i, "p" + i ); session.save( p1 ); } session.getTransaction().commit(); } catch (Exception e) { if ( session.getTransaction().isActive() ) { session.getTransaction().rollback(); } throw e; } } }
@Override protected void prepareTest() throws Exception { try (Session session = openSession()) { session.getTransaction().begin(); try { session.save( new MyEntity( 1L, "entity_1" ) ); session.save( new MyEntity( 2L, "entity_2" ) ); session.getTransaction().commit(); } catch (Exception e) { if ( session.getTransaction().isActive() ) { session.getTransaction().rollback(); } throw e; } } }
@Override protected void cleanupTest() throws Exception { try (Session session = openSession()) { session.getTransaction().begin(); try { session.createQuery( "delete from MyEntity" ).executeUpdate(); session.getTransaction().commit(); } catch (Exception e) { if ( session.getTransaction().isActive() ) { session.getTransaction().rollback(); } throw e; } } }
@Override protected void cleanupTest() throws Exception { try (Session session = openSession()) { session.getTransaction().begin(); try { session.createQuery( "delete from MyEntity" ).executeUpdate(); session.getTransaction().commit(); } catch (Exception e) { if ( session.getTransaction().isActive() ) { session.getTransaction().rollback(); } throw e; } } }
@Override protected void cleanupTestData() throws Exception { try (Session session = openSession()) { session.getTransaction().begin(); try { session.createQuery( "delete from TestEntity" ).executeUpdate(); session.getTransaction().commit(); } catch (Exception e) { if ( session.getTransaction().isActive() ) { session.getTransaction().rollback(); } throw e; } } }
/** * Tests that lazy loading without transaction nor open session is generally * working. The magic is done by {@link AbstractLazyInitializer} who opens a * temporary session. */ @SuppressWarnings("unchecked") @Test public void testProxyInitializationWithoutTX() { final Session s = openSession(); final Transaction t = s.beginTransaction(); try { final Map<String, Object> child = (Map<String, Object>) s.load( "ChildEntity", 1L ); final Map<String, Object> parent = (Map<String, Object>) child.get( "parent" ); t.rollback(); session.close(); // assert we have an uninitialized proxy assertTrue( parent instanceof MapProxy ); assertFalse( Hibernate.isInitialized( parent ) ); assertEquals( "TheParent", parent.get( "name" ) ); // assert we have an initialized proxy now assertTrue( Hibernate.isInitialized( parent ) ); } finally { if ( t.isActive() ) { t.rollback(); } s.close(); } }
@After public void tearDown() { try (Session s = openSession()) { session.getTransaction().begin(); try { s.createQuery( "delete from TestEntity" ).executeUpdate(); s.getTransaction().commit(); } catch (Exception e) { if ( s.getTransaction().isActive() ) { s.getTransaction().rollback(); } throw e; } } }
private void insertTestEntity(String name) { final TestEntity entity = new TestEntity(); entity.setName( name ); try (Session s = openSession()) { session.getTransaction().begin(); try { s.save( entity ); s.getTransaction().commit(); } catch (Exception e) { if ( s.getTransaction().isActive() ) { s.getTransaction().rollback(); } throw e; } } }
@Test(expected = IllegalStateException.class) public void anIllegalStateExceptionShouldBeThrownWhenBeginTxIsCalledWithAnAlreadyActiveTX() { Transaction transaction = null; try (Session session = openSession()) { transaction = session.getTransaction(); transaction.begin(); // A call to begin() with an active Tx should cause an IllegalStateException transaction.begin(); } finally { if ( transaction != null && transaction.isActive() ) { transaction.rollback(); } } } }
@Test @TestForIssue(jiraKey = "HHH-11182") public void testSubQueryConstraintPropertyInSuperclassTable() { Session s = openSession(); try { s.getTransaction().begin(); // employee.firstName is in Person table (not Employee) String queryHQL = "from InvestmentCompany investmentCompany " + "where exists " + "(select employee " + "from investmentCompany.employees as employee " + " where employee.firstName = 'Joe')"; s.createQuery( queryHQL ).uniqueResult(); s.getTransaction().commit(); } catch (Exception e) { if ( s.getTransaction() != null && s.getTransaction().isActive() ) { s.getTransaction().rollback(); } throw e; } finally { s.close(); } }
@Override protected void prepareTest() throws Exception { try (Session session = openSession()) { session.getTransaction().begin(); TestEntity entity = new TestEntity(); entity.setDate( new DateAttribute( System.currentTimeMillis() ) ); try { session.persist( entity ); session.getTransaction().commit(); } catch (Exception e) { if ( session.getTransaction().isActive() ) { session.getTransaction().rollback(); } throw e; } } }
@Test @TestForIssue(jiraKey = "HHH-11182") public void testSubQueryConstraintPropertyInEntityTable() { Session s = openSession(); try { s.getTransaction().begin(); // employee.employeeNumber is in Employee table String queryHQL = "from InvestmentCompany investmentCompany " + "where exists " + "(select employee " + "from investmentCompany.employees as employee " + " where employee.employeeNumber = 666 )"; s.createQuery( queryHQL ).uniqueResult(); } catch (Exception e) { if ( s.getTransaction() != null && s.getTransaction().isActive() ) { s.getTransaction().rollback(); } throw e; } finally { s.close(); } }
@Test public void testUpdateRequiresTxn() { inSession( session -> { try { assertFalse( session.getTransaction().isActive() ); // Query session.createQuery( "update Person set name = 'steve'" ).executeUpdate(); fail( "expecting failure" ); } catch (TransactionRequiredException expected) { // expected condition } } ); }
/** * Tests that lazy loading without transaction nor open session is generally * working. The magic is done by {@link AbstractLazyInitializer} who opens a * temporary session. */ @SuppressWarnings("unchecked") @Test public void testProxyInitializationWithoutTX() { final Session s = openSession(); final Transaction t = s.beginTransaction(); try { final ChildEntity child = s.find( ChildEntity.class, 1L ); final SimpleEntity parent = child.getParent(); t.rollback(); session.close(); // assert we have an uninitialized proxy assertTrue( parent instanceof HibernateProxy ); assertFalse( Hibernate.isInitialized( parent ) ); assertEquals( "TheParent", parent.getName() ); // assert we have an initialized proxy now assertTrue( Hibernate.isInitialized( parent ) ); } finally { if ( t.isActive() ) { t.rollback(); } s.close(); } }
@Test public void testInvalidQueryMarksTxnForRollback() { inSession( session -> { try { assertFalse( session.getTransaction().isActive() ); session.getTransaction().begin(); // Query session.createQuery( "invalid" ).list(); fail( "expecting failure" ); } catch (IllegalArgumentException expected) { assertTrue( session.getTransaction().isActive() ); assertTrue( session.getTransaction().getRollbackOnly() ); } } ); } }
session -> { final Transaction transaction = session.getTransaction(); assertFalse( transaction.isActive() ); try { transaction.getRollbackOnly();
session -> { final Transaction transaction = session.getTransaction(); assertFalse( transaction.isActive() ); try { transaction.setRollbackOnly();