private <T> T doInNewTransaction(HibernateCallable<T> callable, TransactionManager transactionManager) { try { // start the new isolated transaction transactionManager.begin(); try { T result = callable.call(); // if everything went ok, commit the isolated transaction transactionManager.commit(); return result; } catch (Exception e) { try { transactionManager.rollback(); } catch (Exception ignore) { LOG.unableToRollbackIsolatedTransaction( e, ignore ); } throw new HibernateException( "Could not apply work", e ); } } catch (SystemException e) { throw new HibernateException( "Unable to start isolated transaction", e ); } catch (NotSupportedException e) { throw new HibernateException( "Unable to start isolated transaction", e ); } }
@Test public void jtaTransactionManagerWithPropagationRequiresNewAndAdapter() throws Exception { TransactionManager tm = mock(TransactionManager.class); Transaction tx = mock(Transaction.class); given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE); given(tm.suspend()).willReturn(tx); JtaTransactionManager ptm = newJtaTransactionManager(tm); TransactionTemplate tt = new TransactionTemplate(ptm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { assertTrue(TransactionSynchronizationManager.isSynchronizationActive()); } }); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); verify(tm).begin(); verify(tm).commit(); verify(tm).resume(tx); }
public void testTxCleanupWithSize() throws Exception { tm().begin(); assertEquals(0, cache.size()); TransactionTable txTable = getTransactionTable(cache); assertEquals(1, txTable.getLocalTransactions().size()); tm().commit(); assertEquals(0, txTable.getLocalTransactions().size()); } }
public void testTransactionalReplace(Method m) throws Exception { assertEquals(ComponentStatus.RUNNING, cache.getStatus()); assertNotInCacheAndStore(k(m, 1)); assertNotInCacheAndStore(k(m, 2)); cache.put(k(m, 2), v(m)); tm.begin(); cache.put(k(m, 1), v(m, 1)); cache.replace(k(m, 2), v(m, 1)); Transaction t = tm.suspend(); assertNotInCacheAndStore(k(m, 1)); assertInCacheAndStore(k(m, 2), v(m)); tm.resume(t); tm.commit(); assertInCacheAndStore(k(m, 1), v(m, 1)); assertInCacheAndStore(k(m, 2), v(m, 1)); }
public void testRemoteCommit() throws Exception { assertCommitRollback(0, 0, txInterceptor2); tm.begin(); assertCommitRollback(0, 0, txInterceptor2); //enlist another resource adapter to force TM to execute 2PC (otherwise 1PC) tm.getTransaction().enlistResource(new XAResourceAdapter()); cache2.put("key", "value"); assertCommitRollback(0, 0, txInterceptor2); tm.commit(); assertCommitRollback(1, 0, txInterceptor2); }
assertEquals( 0, sessionFactory().getStatistics().getEntityLoadCount() ); bar.put( "description", "a small bar" ); s.persist( "Item", bar ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); assertEquals(0, sessionFactory().getStatistics().getUpdateTimestampsCacheHitCount()); assertEquals(2, sessionFactory().getStatistics().getUpdateTimestampsCachePutCount()); // One preinvalidate & one invalidate TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin(); Session s2 = openSession(); foo = ( Map ) s2.get( "Item", "Foo" ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); s2.createCriteria( "Item" ).list(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); s2.createCriteria( "Item" ).list(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); s = openSession(); s.createQuery( "delete from Item" ).executeUpdate(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
protected <S extends Score, E extends AbstractTestJpaEntity<S>> void findAndAssert( Class<E> jpaEntityClass, Long id, S score) { try { transactionManager.begin(); EntityManager em = entityManagerFactory.createEntityManager(); E jpaEntity = em.find(jpaEntityClass, id); assertEquals(score, jpaEntity.getScore()); transactionManager.commit(); } catch (NotSupportedException | SystemException | RollbackException | HeuristicMixedException | HeuristicRollbackException e) { throw new RuntimeException("Transaction failed.", e); } }
public void testTxCleanupWithEntrySet() throws Exception { tm().begin(); assertEquals(0, cache.entrySet().size()); TransactionTable txTable = getTransactionTable(cache); assertEquals(1, txTable.getLocalTransactions().size()); tm().commit(); assertEquals(0, txTable.getLocalTransactions().size()); }
bar.put( "description", "a small bar" ); s.persist( "Item", bar ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); Transaction tx1 = TestingJtaPlatformImpl.INSTANCE.getTransactionManager().suspend(); assertEquals( r2.size(), 2 ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx1 ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); s3.createCriteria( "Item" ).addOrder( Order.asc( "description" ) ) .setCacheable( true ).list(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx4 ); List r4 = s4.createCriteria( "Item" ).addOrder( Order.asc( "description" ) ) .setCacheable( true ).list(); assertEquals( r4.size(), 2 ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); s = openSession(); s.createQuery( "delete from Item" ).executeUpdate(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
protected <S extends Score, E extends AbstractTestJpaEntity<S>> Long persistAndAssert(E jpaEntity) { try { transactionManager.begin(); EntityManager em = entityManagerFactory.createEntityManager(); em.persist(jpaEntity); transactionManager.commit(); } catch (NotSupportedException | SystemException | RollbackException | HeuristicRollbackException | HeuristicMixedException e) { throw new RuntimeException("Transaction failed.", e); } Long id = jpaEntity.getId(); assertNotNull(id); return id; }
public void testTxCleanupWithKeySet() throws Exception { tm().begin(); assertEquals(0, cache.keySet().size()); TransactionTable txTable = getTransactionTable(cache); assertEquals(1, txTable.getLocalTransactions().size()); tm().commit(); assertEquals(0, txTable.getLocalTransactions().size()); }
bar.put( "description", "a small bar" ); s.persist( "Item", bar ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); .setCacheable( true ).list(); assertEquals( r2.size(), 2 ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); assertEquals( sessionFactory().getStatistics().getUpdateTimestampsCachePutCount(), 0 ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx1 ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); s3.createCriteria( "Item" ).addOrder( Order.asc( "description" ) ) .setCacheable( true ).list(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); assertEquals( sessionFactory().getStatistics().getUpdateTimestampsCacheMissCount(), 0 ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().resume( tx4 ); List r4 = s4.createCriteria( "Item" ).addOrder( Order.asc( "description" ) ) .setCacheable( true ).list(); assertEquals( r4.size(), 2 ); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit(); s = openSession(); s.createQuery( "delete from Item" ).executeUpdate(); TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
protected <S extends Score, E extends AbstractTestJpaEntity<S>> void findAssertAndChangeScore( Class<E> jpaEntityClass, Long id, S oldScore, S newScore) { try { transactionManager.begin(); EntityManager em = entityManagerFactory.createEntityManager(); E jpaEntity = em.find(jpaEntityClass, id); em.persist(jpaEntity); assertEquals(oldScore, jpaEntity.getScore()); jpaEntity.setScore(newScore); jpaEntity = em.merge(jpaEntity); transactionManager.commit(); } catch (NotSupportedException | SystemException | RollbackException | HeuristicRollbackException | HeuristicMixedException e) { throw new RuntimeException("Transaction failed.", e); } }
public void testTxCleanupWithValues() throws Exception { tm().begin(); assertEquals(0, cache.values().size()); TransactionTable txTable = getTransactionTable(cache); assertEquals(1, txTable.getLocalTransactions().size()); tm().commit(); assertEquals(0, txTable.getLocalTransactions().size()); }
private long getNextIdFor(final String key) { final long nextId; final TransactionManager infinispanTransactionManager = sequenceCache.getAdvancedCache().getTransactionManager(); try { infinispanTransactionManager.begin(); sequenceCache.getAdvancedCache().lock(key); nextId = sequenceCache.get(key) + 1; sequenceCache.put(key, nextId); infinispanTransactionManager.commit(); return nextId; } catch (final Exception e) { throw BatchMessages.MESSAGES.failToGetNextId(e, key); } }
@Test public void testDropDuringActiveJtaTransaction() throws Exception { final SessionFactory sessionFactory = buildSessionFactory(); JtaPlatformStandardTestingImpl.INSTANCE.transactionManager().begin(); try { sessionFactory.close(); } finally { JtaPlatformStandardTestingImpl.INSTANCE.transactionManager().commit(); } }