/** * If the given UserTransaction is not null, it is committed/rolledback, * and then returned to the UserTransactionHelper. */ private void resolveUserTransaction(UserTransaction userTransaction) { if (userTransaction != null) { try { if (userTransaction.getStatus() == Status.STATUS_MARKED_ROLLBACK) { userTransaction.rollback(); } else { userTransaction.commit(); } } catch (Throwable t) { getLog().error("Failed to resolve UserTransaction for plugin: " + getName(), t); } finally { UserTransactionHelper.returnUserTransaction(userTransaction); } } } }
@Test public void testEnversCompatibility() throws Exception { // revision 1 userTransaction.begin(); entityManager.joinTransaction(); AuditedEntity entity = new AuditedEntity( 1, "Marco Polo" ); entityManager.persist( entity ); userTransaction.commit(); // revision 2 userTransaction.begin(); entityManager.joinTransaction(); entity.setName( "George Washington" ); entityManager.merge( entity ); userTransaction.commit(); entityManager.clear(); // verify audit history revision counts userTransaction.begin(); final AuditReader auditReader = AuditReaderFactory.get( entityManager ); assertEquals( Arrays.asList( 1, 2 ), auditReader.getRevisions( AuditedEntity.class, 1 ) ); userTransaction.commit(); } }
ut.begin(); StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession( kbase, null, env ); ut.commit(); ut.begin(); ksession.setGlobal( "list", list ); ksession.insert( 1 ); ksession.insert( 2 ); ut.commit(); ut.begin(); ksession.fireAllRules(); ut.commit(); assertEquals( 2, list.size() ); ksession.insert( 3 ); ksession.insert( 4 ); ut.commit(); ksession.insert( 7 ); ksession.insert( 8 ); ut.commit();
@Test public final void test01OnCreateNodeAndOnUpdateProperties() throws Exception { newContent(homeFolder, "content4"); txn.commit(); txn = null; assertEquals(1, auditMapList.size()); Map<String, Serializable> auditMap = auditMapList.get(0); assertEquals("CREATE", auditMap.get("action")); assertContains("createNode", auditMap.get("sub-actions")); assertContains("updateNodeProperties", auditMap.get("sub-actions")); assertEquals("/cm:homeFolder/cm:content4", auditMap.get("path")); assertEquals("cm:content", auditMap.get("type")); }
@Test public void jtaTransactionManagerWithNestedBegin() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); JtaTransactionManager ptm = newJtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { // something transactional } }); verify(ut).begin(); verify(ut).commit(); }
@Test public void testDelegateParticipateInApplicationTx() throws Exception { /* if we start a transaction here, persist an entity and then * start a process instance which synchronously invokes a java delegate, * that delegate is invoked in the same transaction and thus has access to * the same entity manager. */ try { utx.begin(); SomeEntity e = new SomeEntity(); entityManager.persist(e); persistenceDelegateBean.setEntity(e); runtimeService.startProcessInstanceByKey("testDelegateParticipateInApplicationTx"); utx.commit(); }catch (Exception e) { utx.rollback(); throw e; } }
@Test public final void test10OnContentRead() throws Exception { serviceRegistry.getContentService().getReader(content1, ContentModel.TYPE_CONTENT); txn.commit(); txn = null; assertEquals(1, auditMapList.size()); Map<String, Serializable> auditMap = auditMapList.get(0); assertEquals("READ", auditMap.get("action")); assertContains("readContent", auditMap.get("sub-actions")); assertEquals("/cm:homeFolder/cm:folder1/cm:content1", auditMap.get("path")); assertEquals("cm:content", auditMap.get("type")); }
/** * If the given UserTransaction is not null, it is committed/rolledback, * and then returned to the UserTransactionHelper. */ private void resolveUserTransaction(UserTransaction userTransaction) { if (userTransaction != null) { try { if (userTransaction.getStatus() == Status.STATUS_MARKED_ROLLBACK) { userTransaction.rollback(); } else { userTransaction.commit(); } } catch (Throwable t) { getLog().error("Failed to resolve UserTransaction for plugin: " + getName(), t); } finally { UserTransactionHelper.returnUserTransaction(userTransaction); } } } }
@Test public void jtaTransactionManagerWithRollbackExceptionOnCommit() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE); willThrow(new RollbackException("unexpected rollback")).given(ut).commit(); try { JtaTransactionManager ptm = newJtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { // something transactional TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() { @Override public void afterCompletion(int status) { assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_ROLLED_BACK); } }); } }); fail("Should have thrown UnexpectedRollbackException"); } catch (UnexpectedRollbackException ex) { // expected } verify(ut).begin(); }
@Test public void testAsyncDelegateNewTx() throws Exception { /* if we start a transaction here, persist an entity and then * start a process instance which asynchronously invokes a java delegate, * that delegate is invoked in a new transaction and thus does not have access to * the same entity manager. */ try { utx.begin(); SomeEntity e = new SomeEntity(); entityManager.persist(e); asyncPersistenceDelegateBean.setEntity(e); runtimeService.startProcessInstanceByKey("testAsyncDelegateNewTx"); utx.commit(); }catch (Exception e) { utx.rollback(); throw e; } waitForJobExecutorToProcessAllJobs(); } }
@Test public final void test06BeforeDeleteNode() throws Exception { serviceRegistry.getNodeService().deleteNode(content0); txn.commit(); txn = null; assertEquals(1, auditMapList.size()); Map<String, Serializable> auditMap = auditMapList.get(0); assertEquals("DELETE", auditMap.get("action")); assertContains("deleteNode", auditMap.get("sub-actions")); assertEquals("/cm:homeFolder/cm:folder0/cm:content0", auditMap.get("path")); assertEquals("cm:content", auditMap.get("type")); }
@After public void after() throws Exception { EntityManagerFactory emf = (EntityManagerFactory) context.get(EnvironmentName.ENTITY_MANAGER_FACTORY); UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction"); ut.begin(); try { EntityManager em = emf.createEntityManager(); em.createQuery("delete from CorrelationPropertyInfo").executeUpdate(); em.createQuery("delete from CorrelationKeyInfo").executeUpdate(); ut.commit(); } catch (Exception ex) { ut.rollback(); Assert.fail("Exception thrown while trying to cleanup correlation data."); } cleanUp(context); }
@Override public void onMessage(Message message) { try { // need to use transaction so entity manager will persist logs UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction"); ut.begin(); super.onMessage(message); ut.commit(); latch.countDown(); } catch (Exception e) { e.printStackTrace(); } }
@Test public void jtaTransactionManagerWithSystemExceptionOnCommit() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE); willThrow(new SystemException("system exception")).given(ut).commit(); try { JtaTransactionManager ptm = newJtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { // something transactional TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() { @Override public void afterCompletion(int status) { assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_UNKNOWN); } }); } }); fail("Should have thrown TransactionSystemException"); } catch (TransactionSystemException ex) { // expected } verify(ut).begin(); }