@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 jtaTransactionManagerWithSystemExceptionOnRollback() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE); willThrow(new SystemException("system exception")).given(ut).rollback(); try { JtaTransactionManager ptm = newJtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() { @Override public void afterCompletion(int status) { assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_UNKNOWN); } }); status.setRollbackOnly(); } }); fail("Should have thrown TransactionSystemException"); } catch (TransactionSystemException ex) { // expected } verify(ut).begin(); }
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.insert( 3 ); ut.rollback(); ut.begin(); ksession.fireAllRules(); ut.commit(); assertEquals( 2, list.size() ); ut.begin(); ksession.insert( 3 ); ksession.insert( 4 ); ut.begin(); ksession.insert( 5 ); ksession.insert( 6 ); ut.rollback();
@Test public void jtaTransactionManagerWithUnsupportedOperationExceptionOnNestedBegin() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); willThrow(new UnsupportedOperationException("not supported")).given(ut).begin(); try { 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 } }); fail("Should have thrown NestedTransactionNotSupportedException"); } catch (NestedTransactionNotSupportedException ex) { // expected } }
@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(); } }
@Test public void testAsyncAuditProducerTransactionalWithRollback() throws Exception { UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction"); ut.begin(); Environment env = createEnvironment(context); // load the process KieBase kbase = createKnowledgeBase(); // create a new session KieSession session = createSession(kbase, env); Map<String, Object> jmsProps = new HashMap<String, Object>(); jmsProps.put("jbpm.audit.jms.transacted", true); jmsProps.put("jbpm.audit.jms.connection.factory", factory); jmsProps.put("jbpm.audit.jms.queue", queue); AbstractAuditLogger logger = AuditLoggerFactory.newInstance(Type.JMS, session, jmsProps); Assertions.assertThat(logger).isNotNull(); Assertions.assertThat((logger instanceof AsyncAuditLogProducer)).isTrue(); // start process instance long processInstanceId = session.startProcess("com.sample.ruleflow").getId(); ut.rollback(); MessageReceiver receiver = new MessageReceiver(); List<Message> messages = receiver.receive(queue); Assertions.assertThat(messages).isNotNull(); Assertions.assertThat(messages.size()).isEqualTo(0); }
@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 void jtaTransactionManagerWithSystemExceptionOnBegin() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION); willThrow(new SystemException("system exception")).given(ut).begin(); try { JtaTransactionManager ptm = newJtaTransactionManager(ut); TransactionTemplate tt = new TransactionTemplate(ptm); tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { // something transactional } }); fail("Should have thrown CannotCreateTransactionException"); } catch (CannotCreateTransactionException ex) { // expected } }
public static void main(String[] args) throws Exception { Region currRegion; Cache cache = null; // DistributedSystem system = null; // HashMap regionDefaultAttrMap = new HashMap(); tableName = CacheUtils.init("TestXACache"); cache = CacheUtils.getCache(); currRegion = cache.getRegion("root"); try { Context ctx = cache.getJNDIContext(); UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction"); utx.begin(); AttributesFactory fac = new AttributesFactory(currRegion.getAttributes()); fac.setCacheLoader(new TestXACacheLoader()); Region re = currRegion.createSubregion("employee", fac.create()); System.out.println(re.get(args[0])); utx.rollback(); System.out.println(re.get(args[0])); cache.close(); ExitCode.FATAL.doSystemExit(); } catch (Exception e) { e.printStackTrace(); cache.close(); } }
@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 testAsyncAuditProducerNonTransactionalWithRollback() throws Exception { UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction"); ut.begin(); Environment env = createEnvironment(context); // load the process KieBase kbase = createKnowledgeBase(); // create a new session KieSession session = createSession(kbase, env); Map<String, Object> jmsProps = new HashMap<String, Object>(); jmsProps.put("jbpm.audit.jms.transacted", false); jmsProps.put("jbpm.audit.jms.connection.factory", jmsServer.lookup("ConnectionFactory")); jmsProps.put("jbpm.audit.jms.queue", queue); AbstractAuditLogger logger = AuditLoggerFactory.newInstance(Type.JMS, session, jmsProps); Assertions.assertThat(logger).isNotNull(); Assertions.assertThat((logger instanceof AsyncAuditLogProducer)).isTrue(); // start process instance long processInstanceId = session.startProcess("com.sample.ruleflow").getId(); ut.rollback(); MessageReceiver receiver = new MessageReceiver(); List<Message> messages = receiver.receive(queue); Assertions.assertThat(messages).isNotNull(); Assertions.assertThat(messages.size()).isEqualTo(11); }
@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 void jtaTransactionManagerWithNotSupportedExceptionOnNestedBegin() throws Exception { UserTransaction ut = mock(UserTransaction.class); given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE); willThrow(new NotSupportedException("not supported")).given(ut).begin(); try { 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 } }); fail("Should have thrown NestedTransactionNotSupportedException"); } catch (NestedTransactionNotSupportedException ex) { // expected } }
@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); }