@Override public void markForRollbackOnly() { try { accessTransaction().markRollbackOnly(); } catch (Exception ignore) { } }
assertEquals( TransactionStatus.ACTIVE, coordinator.getTransactionDriverControl().getStatus() ); session.getTransaction().markRollbackOnly(); assertEquals( TransactionStatus.MARKED_ROLLBACK, coordinator.getTransactionDriverControl().getStatus() );
@Test public void testMarkRollbackOnlyNoTransaction() { final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder() .applySetting( AvailableSettings.JPA_TRANSACTION_COMPLIANCE, "true" ) .build(); try { final SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) new MetadataSources( ssr ) .buildMetadata() .buildSessionFactory(); try { inSession( sessionFactory, session -> { final Transaction transaction = session.getTransaction(); assertFalse( transaction.isActive() ); // should just happen silently because there is no transaction transaction.markRollbackOnly(); transaction.begin(); transaction.commit(); } ); } finally { sessionFactory.close(); } } finally { StandardServiceRegistryBuilder.destroy( ssr ); } }
@Test @TestForIssue(jiraKey = "HHH-12484") public void testDisconnect() { doInHibernate( this::sessionFactory, session -> { Address address = session.load( Address.class, 1L ); AbstractSharedSessionContract sessionContract = (AbstractSharedSessionContract) session; sessionContract.getJdbcCoordinator().close(); try { address.getClient().getName(); fail( "Should throw LazyInitializationException" ); } catch (LazyInitializationException expected) { assertEquals( "could not initialize proxy " + "[org.hibernate.test.lazyload.LazyLoadingLoggingTest$Address#1] " + "- the owning Session is disconnected", expected.getMessage() ); } session.getTransaction().markRollbackOnly(); } ); }
@Test public void testInsertWithRollback() { sessionFactory().getCache().evictEntityRegions(); sessionFactory().getStatistics().clear(); inTransaction( sessionFactory, s -> { CacheableItem item = new CacheableItem( "data" ); s.save( item ); s.flush(); s.getTransaction().markRollbackOnly(); } ); assertFalse( sessionFactory().getCache().containsEntity( CacheableItem.class, 1L ) ); }
@Test public void testInsertThenUpdateThenRollback() { sessionFactory().getCache().evictEntityRegions(); sessionFactory().getStatistics().clear(); inTransaction( sessionFactory, s -> { CacheableItem item = new CacheableItem( "data" ); s.save( item ); s.flush(); item.setName( "new data" ); s.getTransaction().markRollbackOnly(); } ); assertFalse( sessionFactory().getCache().containsEntity( CacheableItem.class, 1L ) ); }
@Test public void testInsertWithClearThenRollback() { sessionFactory().getCache().evictEntityRegions(); sessionFactory().getStatistics().clear(); inTransaction( sessionFactory, s -> { CacheableItem item = new CacheableItem( "data" ); s.save( item ); s.flush(); s.clear(); item = s.get( CacheableItem.class, item.getId() ); s.getTransaction().markRollbackOnly(); } ); assertFalse( sessionFactory().getCache().containsEntity( CacheableItem.class, 1L ) ); inTransaction( sessionFactory, s -> { final CacheableItem item = s.get( CacheableItem.class, 1L ); assertNull( "it should be null", item ); } ); }
s.flush(); s.refresh( item ); s.getTransaction().markRollbackOnly();
@Override public void markRollbackOnly() { transaction.markRollbackOnly(); }
@Override public void markForRollbackOnly() { try { accessTransaction().markRollbackOnly(); } catch (Exception ignore) { } }
public static void markRollbackOnly(boolean useJta, Session s) { if (useJta) { JtaPlatform jtaPlatform = s.getSessionFactory().getSessionFactoryOptions().getServiceRegistry().getService(JtaPlatform.class); TransactionManager tm = jtaPlatform.retrieveTransactionManager(); try { tm.setRollbackOnly(); } catch (SystemException e) { throw new RuntimeException(e); } } else { s.getTransaction().markRollbackOnly(); } }
@TestForIssue(jiraKey = "HHH-11304") @Test public void testFailedInsert() throws Exception { AdvancedCache pendingPutsCache = getPendingPutsCache(Item.class); assertNoInvalidators(pendingPutsCache); withTxSession(s -> { Item i = new Item("inserted", "bar"); s.persist(i); s.flush(); s.getTransaction().markRollbackOnly(); }); assertNoInvalidators(pendingPutsCache); }
consumer.accept(s); } catch (Exception e) { tx.markRollbackOnly(); throw e; } finally {
@TestForIssue(jiraKey = "HHH-11304") @Test public void testFailedRemove() throws Exception { AdvancedCache pendingPutsCache = getPendingPutsCache(Item.class); assertNoInvalidators(pendingPutsCache); final Item item = new Item("before-remove", "bar"); withTxSession(s -> s.persist(item)); withTxSession(s -> { Item item2 = s.load(Item.class, item.getId()); assertEquals("before-remove", item2.getName()); s.delete(item2); s.flush(); s.getTransaction().markRollbackOnly(); }); assertNoInvalidators(pendingPutsCache); withTxSession(s -> { Item item3 = s.load(Item.class, item.getId()); assertEquals("before-remove", item3.getName()); s.delete(item3); }); assertNoInvalidators(pendingPutsCache); }
@TestForIssue(jiraKey = "HHH-11304") @Test public void testFailedUpdate() throws Exception { AdvancedCache pendingPutsCache = getPendingPutsCache(Item.class); assertNoInvalidators(pendingPutsCache); final Item item = new Item("before-update", "bar"); withTxSession(s -> s.persist(item)); withTxSession(s -> { Item item2 = s.load(Item.class, item.getId()); assertEquals("before-update", item2.getName()); item2.setName("after-update"); s.persist(item2); s.flush(); s.flush(); // workaround for HHH-11312 s.getTransaction().markRollbackOnly(); }); assertNoInvalidators(pendingPutsCache); withTxSession(s -> { Item item3 = s.load(Item.class, item.getId()); assertEquals("before-update", item3.getName()); s.delete(item3); }); assertNoInvalidators(pendingPutsCache); }