javax.transaction.Status Java Examples

The following examples show how to use javax.transaction.Status. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JtaTransactionManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void jtaTransactionManagerWithExistingTransactionAndSynchronizationNever() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

	JtaTransactionManager ptm = newJtaTransactionManager(ut);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_NEVER);
	ptm.afterPropertiesSet();

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
			status.setRollbackOnly();
		}
	});
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

	verify(ut).setRollbackOnly();
}
 
Example #2
Source File: NodeListener.java    From JPPF with Apache License 2.0 6 votes vote down vote up
/**
 * Terminate the transaction and return an exception if any occurred.
 * @return Exception if any error occurred.
 */
@Override
public Exception call() {
  try {
    final UserTransactionImp utx = new UserTransactionImp();
    if (utx.getStatus() == Status.STATUS_NO_TRANSACTION) output("WARNING: endTransaction() called outside a tx");
    else {
      output("INFO: transaction " + (rollback ? "rollback" : "commit"));
      if (rollback) utx.rollback();
      else utx.commit();
    }
    return null;
  } catch (final Exception e) {
    return e;
  }
}
 
Example #3
Source File: DefaultTransaction.java    From piranha with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Commit the transaction.
 *
 * @throws RollbackException when a rollback error occurs.
 * @throws HeuristicMixedException when the heuristics were mixed.
 * @throws HeuristicRollbackException when a rollback error occurs.
 * @throws SecurityException when a security error occurs.
 * @throws IllegalStateException when the transaction is not active.
 * @throws SystemException when a serious error occurs.
 */
@Override
public synchronized void commit() throws RollbackException, HeuristicMixedException,
        HeuristicRollbackException, SecurityException,
        IllegalStateException, SystemException {
    handleBeforeCompletion();
    try {
        switch (status) {
            case Status.STATUS_COMMITTED:
                break;
            case Status.STATUS_MARKED_ROLLBACK: {
                rollback();
                throw new HeuristicRollbackException();
            }
            case Status.STATUS_ROLLEDBACK: {
                throw new RollbackException();
            }
            default: {
                status = Status.STATUS_COMMITTED;
            }
        }
    } finally {
        handleAfterCompletion();
    }
}
 
Example #4
Source File: JtaTransactionManagerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void jtaTransactionManagerWithExistingTransactionAndRollbackOnlyAndNoGlobalRollback() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

	final TransactionSynchronization synch = mock(TransactionSynchronization.class);

	JtaTransactionManager ptm = newJtaTransactionManager(ut);
	ptm.setGlobalRollbackOnParticipationFailure(false);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
			TransactionSynchronizationManager.registerSynchronization(synch);
			status.setRollbackOnly();
		}
	});
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

	verify(ut).setRollbackOnly();
	verify(synch).beforeCompletion();
	verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
 
Example #5
Source File: JtaTransactionManagerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
}
 
Example #6
Source File: JtaTransactionManagerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void jtaTransactionManagerWithExistingAndPropagationSupports() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

	final TransactionSynchronization synch = mock(TransactionSynchronization.class);

	JtaTransactionManager ptm = newJtaTransactionManager(ut);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
			TransactionSynchronizationManager.registerSynchronization(synch);
			status.setRollbackOnly();
		}
	});
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

	verify(ut).setRollbackOnly();
	verify(synch).beforeCompletion();
	verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
 
Example #7
Source File: XACacheTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Override
public final void run() {
  try {
    transactionManager.begin();
    transactionManager.getCurrentTransaction().addTransactionStatusChangeListener((oldStatus, newStatus) -> {
      if (oldStatus == Status.STATUS_PREPARED) {
        try {
          barrier.await(5L, TimeUnit.SECONDS);
        } catch (Exception e) {
          throw new AssertionError();
        }
      }
    });
    doTxWork();
    transactionManager.commit();
  } catch (Throwable t) {
    this.ex = t;
  }
}
 
Example #8
Source File: JtaTransactionManagerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void jtaTransactionManagerWithExistingTransactionAndSynchronizationNever() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

	JtaTransactionManager ptm = newJtaTransactionManager(ut);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_NEVER);
	ptm.afterPropertiesSet();

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
			status.setRollbackOnly();
		}
	});
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

	verify(ut).setRollbackOnly();
}
 
Example #9
Source File: SpringJtaSynchronizationAdapter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * JTA {@code afterCompletion} callback: invoked after commit/rollback.
 * <p>Needs to invoke the Spring synchronization's {@code beforeCompletion}
 * at this late stage in case of a rollback, since there is no corresponding
 * callback with JTA.
 * @see org.springframework.transaction.support.TransactionSynchronization#beforeCompletion
 * @see org.springframework.transaction.support.TransactionSynchronization#afterCompletion
 */
@Override
public void afterCompletion(int status) {
	if (!this.beforeCompletionCalled) {
		// beforeCompletion not called before (probably because of JTA rollback).
		// Perform the cleanup here.
		this.springSynchronization.beforeCompletion();
	}
	// Call afterCompletion with the appropriate status indication.
	switch (status) {
		case Status.STATUS_COMMITTED:
			this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
			break;
		case Status.STATUS_ROLLEDBACK:
			this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
			break;
		default:
			this.springSynchronization.afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
	}
}
 
Example #10
Source File: JtaTransactionManagerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void jtaTransactionManagerWithPropagationSupportsAndSynchronizationOnActual() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);

	JtaTransactionManager ptm = newJtaTransactionManager(ut);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
	ptm.afterPropertiesSet();

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
			status.setRollbackOnly();
		}
	});
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
 
Example #11
Source File: JtaTransactionManagerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void jtaTransactionManagerWithPropagationSupportsAndSynchronizationNever() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);

	JtaTransactionManager ptm = newJtaTransactionManager(ut);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_NEVER);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
	ptm.afterPropertiesSet();

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
			status.setRollbackOnly();
		}
	});
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
 
Example #12
Source File: ActiveMQRAManagedConnection.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public void checkTransactionActive() throws JMSException {
   // don't bother looking at the transaction if there's an active XID
   if (!inManagedTx && tm != null) {
      try {
         Transaction tx = tm.getTransaction();
         if (tx != null) {
            int status = tx.getStatus();
            // Only allow states that will actually succeed
            if (status != Status.STATUS_ACTIVE && status != Status.STATUS_PREPARING && status != Status.STATUS_PREPARED && status != Status.STATUS_COMMITTING) {
               throw new javax.jms.IllegalStateException("Transaction " + tx + " not active");
            }
         }
      } catch (SystemException e) {
         JMSException jmsE = new javax.jms.IllegalStateException("Unexpected exception on the Transaction ManagerTransaction");
         jmsE.initCause(e);
         throw jmsE;
      }
   }
}
 
Example #13
Source File: JtaTransactionManagerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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
	}
}
 
Example #14
Source File: MemoryTimerStore.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Override
public void afterCompletion(final int status) {
    checkThread();

    // if the tx was not committed, there is nothign to update
    if (status != Status.STATUS_COMMITTED) {
        return;
    }

    // add the new work
    taskStore.putAll(add);

    // remove work
    taskStore.keySet().removeAll(remove);

    tasksByTransaction.remove(tansactionReference.get());
}
 
Example #15
Source File: MongoCompensableRepository.java    From ByteTCC with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void markTransactionRollback(TransactionXid transactionXid) {
	try {
		byte[] global = transactionXid.getGlobalTransactionId();
		String identifier = ByteUtils.byteArrayToString(global);

		String application = CommonUtils.getApplication(this.endpoint);

		String databaseName = application.replaceAll("\\W", "_");
		MongoDatabase mdb = this.mongoClient.getDatabase(databaseName);
		MongoCollection<Document> collection = mdb.getCollection(CONSTANTS_TB_TRANSACTIONS);

		Document document = new Document();
		document.append("$set", new Document("status", Status.STATUS_MARKED_ROLLBACK));

		Bson globalFilter = Filters.eq(CONSTANTS_FD_GLOBAL, identifier);
		Bson statusFilter = Filters.eq("status", Status.STATUS_ACTIVE);

		collection.updateOne(Filters.and(globalFilter, statusFilter), document);
	} catch (RuntimeException error) {
		logger.error("Error occurred while setting the error flag.", error);
	}
}
 
Example #16
Source File: TransactionImpl.java    From ByteJTA with GNU Lesser General Public License v3.0 6 votes vote down vote up
public synchronized void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException,
		SecurityException, IllegalStateException, CommitRequiredException, SystemException {

	if (this.transactionStatus == Status.STATUS_ACTIVE) {
		this.fireCommit();
	} else if (this.transactionStatus == Status.STATUS_MARKED_ROLLBACK) {
		this.fireRollback();
		throw new HeuristicRollbackException();
	} else if (this.transactionStatus == Status.STATUS_ROLLEDBACK) /* should never happen */ {
		throw new RollbackException();
	} else if (this.transactionStatus == Status.STATUS_COMMITTED) /* should never happen */ {
		logger.debug("Current transaction has already been committed.");
	} else {
		throw new IllegalStateException();
	}

}
 
Example #17
Source File: JtaTransactionManagerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExistingWithSuspendException() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	TransactionManager tm = mock(TransactionManager.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
	willThrow(new SystemException()).given(tm).suspend();

	JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	try {
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
			}
		});
		fail("Should have thrown TransactionSystemException");
	}
	catch (TransactionSystemException ex) {
		// expected
	}
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
 
Example #18
Source File: JTATransaction.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public boolean wasCommitted() throws TransactionException {

		//if (!begun || commitFailed) return false;

		final int status;
		try {
			status = ut.getStatus();
		}
		catch (SystemException se) {
			log.error("Could not determine transaction status", se);
			throw new TransactionException("Could not determine transaction status: ", se);
		}
		if (status==Status.STATUS_UNKNOWN) {
			throw new TransactionException("Could not determine transaction status");
		}
		else {
			return status==Status.STATUS_COMMITTED;
		}
	}
 
Example #19
Source File: JtaTransactionManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void jtaTransactionManagerWithPropagationSupportsAndSynchronizationNever() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);

	JtaTransactionManager ptm = newJtaTransactionManager(ut);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_NEVER);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
	ptm.afterPropertiesSet();

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
			status.setRollbackOnly();
		}
	});
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
 
Example #20
Source File: JtaTransactionManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void jtaTransactionManagerWithPropagationNotSupported() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	TransactionManager tm = mock(TransactionManager.class);
	Transaction tx = mock(Transaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
	given(tm.suspend()).willReturn(tx);

	JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
			status.setRollbackOnly();
		}
	});
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

	verify(tm).resume(tx);
}
 
Example #21
Source File: JtaTransactionManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExisting() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	TransactionManager tm = mock(TransactionManager.class);
	Transaction tx = mock(Transaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
	given(tm.suspend()).willReturn(tx);

	JtaTransactionManager ptm = newJtaTransactionManager(ut, 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(ut).begin();
	verify(ut).commit();
	verify(tm).resume(tx);
}
 
Example #22
Source File: JtaTransactionManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExistingWithSuspendException() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	TransactionManager tm = mock(TransactionManager.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
	willThrow(new SystemException()).given(tm).suspend();

	JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	try {
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
			}
		});
		fail("Should have thrown TransactionSystemException");
	}
	catch (TransactionSystemException ex) {
		// expected
	}
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
 
Example #23
Source File: BasicTransactionAssistanceFactoryImplTest.java    From genericconnector with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoTX() throws ResourceException, IllegalStateException, RollbackException, SystemException {
	final UserTransactionManager tm = mock(UserTransactionManager.class);
	when(tm.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION);
	
	BasicTransactionAssistanceFactory f = new BasicTransactionAssistanceFactoryImpl("a"){
		@Override
		protected UserTransactionManager getTM() {
			return tm;
		}
	};

	try{
		//TEST
		f.getTransactionAssistant();
		fail("no exception");
	}catch(ResourceException e){
		//OK, expected
	}
}
 
Example #24
Source File: ConcurrencyVersioningTest.java    From keycloak with Apache License 2.0 6 votes vote down vote up
public static void endBatch(Cache<String, String> cache) {
    boolean commit = true;
    try {
        if (cache.getAdvancedCache().getTransactionManager().getStatus() == Status.STATUS_ACTIVE) {
            if (commit) {
                cache.getAdvancedCache().getTransactionManager().commit();

            } else {
                cache.getAdvancedCache().getTransactionManager().rollback();

            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #25
Source File: JtaTransactionManagerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void jtaTransactionManagerWithIsolationLevel() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);

	try {
		JtaTransactionManager ptm = newJtaTransactionManager(ut);
		TransactionTemplate tt = new TransactionTemplate(ptm);
		tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				// something transactional
			}
		});
		fail("Should have thrown InvalidIsolationLevelException");
	}
	catch (InvalidIsolationLevelException ex) {
		// expected
	}
}
 
Example #26
Source File: TestTransactionalObject.java    From reladomo with Apache License 2.0 6 votes vote down vote up
public void afterCompletion(int status)
{
    this.currentTx = null;
    if (status == Status.STATUS_COMMITTED || status == Status.STATUS_COMMITTING)
    {
        //Load the committed map with the transactional map
        this.commitedMap.putAll(this.transactionalMap);
        this.transactionalMap.clear();
    }
    else if (status == Status.STATUS_ROLLEDBACK || status == Status.STATUS_ROLLING_BACK)
    {
        this.transactionalMap.clear();
    }
    else
    {
        throw new RuntimeException("Unhandled transaction status: " + status);
    }
}
 
Example #27
Source File: JtaTransactionManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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();
}
 
Example #28
Source File: JtaTransactionManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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
	}
}
 
Example #29
Source File: SchedulerPluginWithUserTransactionSupport.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example #30
Source File: JtaTransactionManagerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void jtaTransactionManagerWithPropagationSupportsAndSynchronizationNever() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);

	JtaTransactionManager ptm = newJtaTransactionManager(ut);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_NEVER);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
	ptm.afterPropertiesSet();

	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
			status.setRollbackOnly();
		}
	});
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}