org.springframework.transaction.TransactionSystemException Java Examples

The following examples show how to use org.springframework.transaction.TransactionSystemException. 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: JpaTransactionManager.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
protected void doRollback(DefaultTransactionStatus status) {
	JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Rolling back JPA transaction on EntityManager [" +
				txObject.getEntityManagerHolder().getEntityManager() + "]");
	}
	try {
		EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
		if (tx.isActive()) {
			tx.rollback();
		}
	}
	catch (PersistenceException ex) {
		throw new TransactionSystemException("Could not roll back JPA transaction", ex);
	}
	finally {
		if (!txObject.isNewEntityManagerHolder()) {
			// Clear all pending inserts/updates/deletes in the EntityManager.
			// Necessary for pre-bound EntityManagers, to avoid inconsistent state.
			txObject.getEntityManagerHolder().getEntityManager().clear();
		}
	}
}
 
Example #2
Source File: JdoTransactionManager.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
protected void doRollback(DefaultTransactionStatus status) {
	JdoTransactionObject txObject = (JdoTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Rolling back JDO transaction on PersistenceManager [" +
				txObject.getPersistenceManagerHolder().getPersistenceManager() + "]");
	}
	try {
		Transaction tx = txObject.getPersistenceManagerHolder().getPersistenceManager().currentTransaction();
		if (tx.isActive()) {
			tx.rollback();
		}
	}
	catch (JDOException ex) {
		throw new TransactionSystemException("Could not roll back JDO transaction", ex);
	}
}
 
Example #3
Source File: DataSourceTransactionManagerTests.java    From effectivejava with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransactionWithExceptionOnCommit() throws Exception {
	willThrow(new SQLException("Cannot commit")).given(con).commit();

	TransactionTemplate tt = new TransactionTemplate(tm);
	try {
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				// something transactional
			}
		});
		fail("Should have thrown TransactionSystemException");
	}
	catch (TransactionSystemException ex) {
		// expected
	}

	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
	verify(con).close();
}
 
Example #4
Source File: JpaTransactionManager.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected void doRollback(DefaultTransactionStatus status) {
	JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Rolling back JPA transaction on EntityManager [" +
				txObject.getEntityManagerHolder().getEntityManager() + "]");
	}
	try {
		EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
		if (tx.isActive()) {
			tx.rollback();
		}
	}
	catch (PersistenceException ex) {
		throw new TransactionSystemException("Could not roll back JPA transaction", ex);
	}
	finally {
		if (!txObject.isNewEntityManagerHolder()) {
			// Clear all pending inserts/updates/deletes in the EntityManager.
			// Necessary for pre-bound EntityManagers, to avoid inconsistent state.
			txObject.getEntityManagerHolder().getEntityManager().clear();
		}
	}
}
 
Example #5
Source File: JpaTransactionManager.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
protected void doRollback(DefaultTransactionStatus status) {
	JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Rolling back JPA transaction on EntityManager [" +
				txObject.getEntityManagerHolder().getEntityManager() + "]");
	}
	try {
		EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
		if (tx.isActive()) {
			tx.rollback();
		}
	}
	catch (PersistenceException ex) {
		throw new TransactionSystemException("Could not roll back JPA transaction", ex);
	}
	finally {
		if (!txObject.isNewEntityManagerHolder()) {
			// Clear all pending inserts/updates/deletes in the EntityManager.
			// Necessary for pre-bound EntityManagers, to avoid inconsistent state.
			txObject.getEntityManagerHolder().getEntityManager().clear();
		}
	}
}
 
Example #6
Source File: DataSourceTransactionManagerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testTransactionWithExceptionOnRollback() throws Exception {
	given(con.getAutoCommit()).willReturn(true);
	willThrow(new SQLException("Cannot rollback")).given(con).rollback();

	TransactionTemplate tt = new TransactionTemplate(tm);
	try {
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
				status.setRollbackOnly();
			}
		});
		fail("Should have thrown TransactionSystemException");
	}
	catch (TransactionSystemException ex) {
		// expected
	}

	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
	InOrder ordered = inOrder(con);
	ordered.verify(con).setAutoCommit(false);
	ordered.verify(con).rollback();
	ordered.verify(con).setAutoCommit(true);
	verify(con).close();
}
 
Example #7
Source File: DataSourceTransactionManagerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testTransactionWithExceptionOnCommitAndRollbackOnCommitFailure() throws Exception {
	willThrow(new SQLException("Cannot commit")).given(con).commit();

	tm.setRollbackOnCommitFailure(true);
	TransactionTemplate tt = new TransactionTemplate(tm);
	try {
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				// something transactional
			}
		});
		fail("Should have thrown TransactionSystemException");
	}
	catch (TransactionSystemException ex) {
		// expected
	}

	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
	verify(con).rollback();
	verify(con).close();
}
 
Example #8
Source File: JpaTransactionManager.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void doRollback(DefaultTransactionStatus status) {
	JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
	if (status.isDebug()) {
		logger.debug("Rolling back JPA transaction on EntityManager [" +
				txObject.getEntityManagerHolder().getEntityManager() + "]");
	}
	try {
		EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
		if (tx.isActive()) {
			tx.rollback();
		}
	}
	catch (PersistenceException ex) {
		throw new TransactionSystemException("Could not roll back JPA transaction", ex);
	}
	finally {
		if (!txObject.isNewEntityManagerHolder()) {
			// Clear all pending inserts/updates/deletes in the EntityManager.
			// Necessary for pre-bound EntityManagers, to avoid inconsistent state.
			txObject.getEntityManagerHolder().getEntityManager().clear();
		}
	}
}
 
Example #9
Source File: DataSourceTransactionManagerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testTransactionWithExceptionOnCommitAndRollbackOnCommitFailure() throws Exception {
	willThrow(new SQLException("Cannot commit")).given(con).commit();

	tm.setRollbackOnCommitFailure(true);
	TransactionTemplate tt = new TransactionTemplate(tm);
	try {
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				// something transactional
			}
		});
		fail("Should have thrown TransactionSystemException");
	}
	catch (TransactionSystemException ex) {
		// expected
	}

	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
	verify(con).rollback();
	verify(con).close();
}
 
Example #10
Source File: DataSourceTransactionManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testTransactionWithExceptionOnCommit() throws Exception {
	willThrow(new SQLException("Cannot commit")).given(con).commit();

	TransactionTemplate tt = new TransactionTemplate(tm);
	try {
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				// something transactional
			}
		});
		fail("Should have thrown TransactionSystemException");
	}
	catch (TransactionSystemException ex) {
		// expected
	}

	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
	verify(con).close();
}
 
Example #11
Source File: DataSourceTransactionManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testTransactionWithExceptionOnCommitAndRollbackOnCommitFailure() throws Exception {
	willThrow(new SQLException("Cannot commit")).given(con).commit();

	tm.setRollbackOnCommitFailure(true);
	TransactionTemplate tt = new TransactionTemplate(tm);
	try {
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				// something transactional
			}
		});
		fail("Should have thrown TransactionSystemException");
	}
	catch (TransactionSystemException ex) {
		// expected
	}

	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
	verify(con).rollback();
	verify(con).close();
}
 
Example #12
Source File: DataSourceTransactionManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testTransactionWithExceptionOnRollback() throws Exception {
	given(con.getAutoCommit()).willReturn(true);
	willThrow(new SQLException("Cannot rollback")).given(con).rollback();

	TransactionTemplate tt = new TransactionTemplate(tm);
	try {
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
				status.setRollbackOnly();
			}
		});
		fail("Should have thrown TransactionSystemException");
	}
	catch (TransactionSystemException ex) {
		// expected
	}

	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
	InOrder ordered = inOrder(con);
	ordered.verify(con).setAutoCommit(false);
	ordered.verify(con).rollback();
	ordered.verify(con).setAutoCommit(true);
	verify(con).close();
}
 
Example #13
Source File: JmsTransactionManager.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected void doRollback(DefaultTransactionStatus status) {
	JmsTransactionObject txObject = (JmsTransactionObject) status.getTransaction();
	Session session = txObject.getResourceHolder().getOriginalSession();
	if (session != null) {
		try {
			if (status.isDebug()) {
				logger.debug("Rolling back JMS transaction on Session [" + session + "]");
			}
			session.rollback();
		}
		catch (JMSException ex) {
			throw new TransactionSystemException("Could not roll back JMS transaction", ex);
		}
	}
}
 
Example #14
Source File: WebLogicJtaTransactionManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void loadWebLogicTransactionClasses() throws TransactionSystemException {
	try {
		Class<?> userTransactionClass = getClass().getClassLoader().loadClass(USER_TRANSACTION_CLASS_NAME);
		this.weblogicUserTransactionAvailable = userTransactionClass.isInstance(getUserTransaction());
		if (this.weblogicUserTransactionAvailable) {
			this.beginWithNameMethod = userTransactionClass.getMethod("begin", String.class);
			this.beginWithNameAndTimeoutMethod = userTransactionClass.getMethod("begin", String.class, int.class);
			logger.info("Support for WebLogic transaction names available");
		}
		else {
			logger.info("Support for WebLogic transaction names not available");
		}

		// Obtain WebLogic ClientTransactionManager interface.
		Class<?> transactionManagerClass =
				getClass().getClassLoader().loadClass(CLIENT_TRANSACTION_MANAGER_CLASS_NAME);
		logger.debug("WebLogic ClientTransactionManager found");

		this.weblogicTransactionManagerAvailable = transactionManagerClass.isInstance(getTransactionManager());
		if (this.weblogicTransactionManagerAvailable) {
			Class<?> transactionClass = getClass().getClassLoader().loadClass(TRANSACTION_CLASS_NAME);
			this.forceResumeMethod = transactionManagerClass.getMethod("forceResume", Transaction.class);
			this.setPropertyMethod = transactionClass.getMethod("setProperty", String.class, Serializable.class);
			logger.debug("Support for WebLogic forceResume available");
		}
		else {
			logger.warn("Support for WebLogic forceResume not available");
		}
	}
	catch (Exception ex) {
		throw new TransactionSystemException(
				"Could not initialize WebLogicJtaTransactionManager because WebLogic API classes are not available",
				ex);
	}
}
 
Example #15
Source File: TransactionInterceptor.java    From micronaut-spring with Apache License 2.0 5 votes vote down vote up
private PlatformTransactionManager resolveTransactionManager(String transactionManagerName) {
    try {
        if (transactionManagerName != null) {
            return this.transactionManagerMap.computeIfAbsent(transactionManagerName, s ->
                beanLocator.getBean(PlatformTransactionManager.class, Qualifiers.byName(transactionManagerName))
            );
        } else {
            return this.transactionManagerMap.computeIfAbsent("default", s ->
                beanLocator.getBean(PlatformTransactionManager.class)
            );
        }
    } catch (NoSuchBeanException e) {
        throw new TransactionSystemException("No transaction manager configured" + (transactionManagerName != null ? " for name: " + transactionManagerName : ""));
    }
}
 
Example #16
Source File: DatastoreTransactionManagerTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testDoCommitFailure() {
	this.expectedException.expect(TransactionSystemException.class);
	this.expectedException.expectMessage("Cloud Datastore transaction failed to commit.; " +
			"nested exception is com.google.cloud.datastore.DatastoreException: ");
	when(this.transaction.isActive()).thenReturn(true);
	this.tx.setTransaction(this.transaction);
	when(this.transaction.commit()).thenThrow(new DatastoreException(0, "", ""));
	this.manager.doCommit(this.status);
}
 
Example #17
Source File: JtaTransactionObject.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * This implementation checks the UserTransaction's rollback-only flag.
 */
@Override
public boolean isRollbackOnly() {
	try {
		int jtaStatus = this.userTransaction.getStatus();
		return (jtaStatus == Status.STATUS_MARKED_ROLLBACK || jtaStatus == Status.STATUS_ROLLEDBACK);
	}
	catch (SystemException ex) {
		throw new TransactionSystemException("JTA failure on getStatus", ex);
	}
}
 
Example #18
Source File: JtaTransactionManager.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected Object doSuspend(Object transaction) {
	JtaTransactionObject txObject = (JtaTransactionObject) transaction;
	try {
		return doJtaSuspend(txObject);
	}
	catch (SystemException ex) {
		throw new TransactionSystemException("JTA failure on suspend", ex);
	}
}
 
Example #19
Source File: JmsTransactionManager.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected void doRollback(DefaultTransactionStatus status) {
	JmsTransactionObject txObject = (JmsTransactionObject) status.getTransaction();
	Session session = txObject.getResourceHolder().getSession();
	try {
		if (status.isDebug()) {
			logger.debug("Rolling back JMS transaction on Session [" + session + "]");
		}
		session.rollback();
	}
	catch (JMSException ex) {
		throw new TransactionSystemException("Could not roll back JMS transaction", ex);
	}
}
 
Example #20
Source File: JtaTransactionManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find the JTA 1.1 TransactionSynchronizationRegistry through autodetection:
 * checking whether the UserTransaction object or TransactionManager object
 * implements it, and checking Java EE 5's standard JNDI location.
 * <p>The default implementation simply returns {@code null}.
 * @param ut the JTA UserTransaction object
 * @param tm the JTA TransactionManager object
 * @return the JTA TransactionSynchronizationRegistry handle to use,
 * or {@code null} if none found
 * @throws TransactionSystemException in case of errors
 */
protected TransactionSynchronizationRegistry findTransactionSynchronizationRegistry(UserTransaction ut, TransactionManager tm)
		throws TransactionSystemException {

	if (this.userTransactionObtainedFromJndi) {
		// UserTransaction has already been obtained from JNDI, so the
		// TransactionSynchronizationRegistry probably sits there as well.
		String jndiName = DEFAULT_TRANSACTION_SYNCHRONIZATION_REGISTRY_NAME;
		try {
			TransactionSynchronizationRegistry tsr = getJndiTemplate().lookup(jndiName, TransactionSynchronizationRegistry.class);
			if (logger.isDebugEnabled()) {
				logger.debug("JTA TransactionSynchronizationRegistry found at default JNDI location [" + jndiName + "]");
			}
			return tsr;
		}
		catch (NamingException ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("No JTA TransactionSynchronizationRegistry found at default JNDI location [" + jndiName + "]", ex);
			}
		}
	}
	// Check whether the UserTransaction or TransactionManager implements it...
	if (ut instanceof TransactionSynchronizationRegistry) {
		return (TransactionSynchronizationRegistry) ut;
	}
	if (tm instanceof TransactionSynchronizationRegistry) {
		return (TransactionSynchronizationRegistry) tm;
	}
	// OK, so no JTA 1.1 TransactionSynchronizationRegistry is available...
	return null;
}
 
Example #21
Source File: WebSphereUowTransactionManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtain the WebSphere UOWManager from the default JNDI location
 * "java:comp/websphere/UOWManager".
 * @return the UOWManager object
 * @throws TransactionSystemException if the JNDI lookup failed
 * @see #setJndiTemplate
 */
protected UOWManager lookupDefaultUowManager() throws TransactionSystemException {
	try {
		logger.debug("Retrieving WebSphere UOWManager from default JNDI location [" + DEFAULT_UOW_MANAGER_NAME + "]");
		return getJndiTemplate().lookup(DEFAULT_UOW_MANAGER_NAME, UOWManager.class);
	}
	catch (NamingException ex) {
		logger.debug("WebSphere UOWManager is not available at default JNDI location [" +
				DEFAULT_UOW_MANAGER_NAME + "] - falling back to UOWManagerFactory lookup");
		return UOWManagerFactory.getUOWManager();
	}
}
 
Example #22
Source File: Neo4jTransactionManager.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Override
protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException {
	Neo4jTransactionObject transactionObject = extractNeo4jTransaction(transaction);

	TransactionConfig transactionConfig = createTransactionConfigFrom(definition);
	boolean readOnly = definition.isReadOnly();


	TransactionSynchronizationManager.setCurrentTransactionReadOnly(readOnly);

	try {
		// Prepare configuration data
		Neo4jTransactionContext context = new Neo4jTransactionContext(
			databaseSelectionProvider.getDatabaseSelection().getValue(),
			bookmarkManager.getBookmarks()
		);

		// Configure and open session together with a native transaction
		Session session = this.driver
			.session(sessionConfig(readOnly, context.getBookmarks(), context.getDatabaseName()));
		Transaction nativeTransaction = session.beginTransaction(transactionConfig);

		// Synchronize on that
		Neo4jTransactionHolder transactionHolder = new Neo4jTransactionHolder(context, session, nativeTransaction);
		transactionHolder.setSynchronizedWithTransaction(true);
		transactionObject.setResourceHolder(transactionHolder);

		TransactionSynchronizationManager.bindResource(this.driver, transactionHolder);
	} catch (Exception ex) {
		throw new TransactionSystemException(String.format("Could not open a new Neo4j session: %s", ex.getMessage()));
	}
}
 
Example #23
Source File: JtaTransactionManager.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Find the JTA 1.1 TransactionSynchronizationRegistry through autodetection:
 * checking whether the UserTransaction object or TransactionManager object
 * implements it, and checking Java EE 5's standard JNDI location.
 * <p>The default implementation simply returns {@code null}.
 * @param ut the JTA UserTransaction object
 * @param tm the JTA TransactionManager object
 * @return the JTA TransactionSynchronizationRegistry handle to use,
 * or {@code null} if none found
 * @throws TransactionSystemException in case of errors
 */
@Nullable
protected TransactionSynchronizationRegistry findTransactionSynchronizationRegistry(
		@Nullable UserTransaction ut, @Nullable TransactionManager tm) throws TransactionSystemException {

	if (this.userTransactionObtainedFromJndi) {
		// UserTransaction has already been obtained from JNDI, so the
		// TransactionSynchronizationRegistry probably sits there as well.
		String jndiName = DEFAULT_TRANSACTION_SYNCHRONIZATION_REGISTRY_NAME;
		try {
			TransactionSynchronizationRegistry tsr = getJndiTemplate().lookup(jndiName, TransactionSynchronizationRegistry.class);
			if (logger.isDebugEnabled()) {
				logger.debug("JTA TransactionSynchronizationRegistry found at default JNDI location [" + jndiName + "]");
			}
			return tsr;
		}
		catch (NamingException ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("No JTA TransactionSynchronizationRegistry found at default JNDI location [" + jndiName + "]", ex);
			}
		}
	}
	// Check whether the UserTransaction or TransactionManager implements it...
	if (ut instanceof TransactionSynchronizationRegistry) {
		return (TransactionSynchronizationRegistry) ut;
	}
	if (tm instanceof TransactionSynchronizationRegistry) {
		return (TransactionSynchronizationRegistry) tm;
	}
	// OK, so no JTA 1.1 TransactionSynchronizationRegistry is available...
	return null;
}
 
Example #24
Source File: DataSourceTransactionManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void doCommit(DefaultTransactionStatus status) {
	DataSourceTransactionObject txObject = (DataSourceTransactionObject) status.getTransaction();
	Connection con = txObject.getConnectionHolder().getConnection();
	if (status.isDebug()) {
		logger.debug("Committing JDBC transaction on Connection [" + con + "]");
	}
	try {
		con.commit();
	}
	catch (SQLException ex) {
		throw new TransactionSystemException("Could not commit JDBC transaction", ex);
	}
}
 
Example #25
Source File: JtaTransactionManager.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isExistingTransaction(Object transaction) {
	JtaTransactionObject txObject = (JtaTransactionObject) transaction;
	try {
		return (txObject.getUserTransaction().getStatus() != Status.STATUS_NO_TRANSACTION);
	}
	catch (SystemException ex) {
		throw new TransactionSystemException("JTA failure on getStatus", ex);
	}
}
 
Example #26
Source File: JpaTransactionManagerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testTransactionCommitWithRollbackException() {
	given(manager.getTransaction()).willReturn(tx);
	given(tx.getRollbackOnly()).willReturn(true);
	willThrow(new RollbackException()).given(tx).commit();

	final List<String> l = new ArrayList<String>();
	l.add("test");

	assertTrue(!TransactionSynchronizationManager.hasResource(factory));
	assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());

	try {
		Object result = tt.execute(new TransactionCallback() {
			@Override
			public Object doInTransaction(TransactionStatus status) {
				assertTrue(TransactionSynchronizationManager.hasResource(factory));
				EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
				return l;
			}
		});
		assertSame(l, result);
	}
	catch (TransactionSystemException tse) {
		// expected
		assertTrue(tse.getCause() instanceof RollbackException);
	}

	assertTrue(!TransactionSynchronizationManager.hasResource(factory));
	assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());

	verify(manager).flush();
	verify(manager).close();
}
 
Example #27
Source File: WebLogicJtaTransactionManager.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void loadWebLogicTransactionClasses() throws TransactionSystemException {
	try {
		Class<?> userTransactionClass = getClass().getClassLoader().loadClass(USER_TRANSACTION_CLASS_NAME);
		this.weblogicUserTransactionAvailable = userTransactionClass.isInstance(getUserTransaction());
		if (this.weblogicUserTransactionAvailable) {
			this.beginWithNameMethod = userTransactionClass.getMethod("begin", String.class);
			this.beginWithNameAndTimeoutMethod = userTransactionClass.getMethod("begin", String.class, int.class);
			logger.info("Support for WebLogic transaction names available");
		}
		else {
			logger.info("Support for WebLogic transaction names not available");
		}

		// Obtain WebLogic ClientTransactionManager interface.
		Class<?> transactionManagerClass =
				getClass().getClassLoader().loadClass(CLIENT_TRANSACTION_MANAGER_CLASS_NAME);
		logger.debug("WebLogic ClientTransactionManager found");

		this.weblogicTransactionManagerAvailable = transactionManagerClass.isInstance(getTransactionManager());
		if (this.weblogicTransactionManagerAvailable) {
			Class<?> transactionClass = getClass().getClassLoader().loadClass(TRANSACTION_CLASS_NAME);
			this.forceResumeMethod = transactionManagerClass.getMethod("forceResume", Transaction.class);
			this.setPropertyMethod = transactionClass.getMethod("setProperty", String.class, Serializable.class);
			logger.debug("Support for WebLogic forceResume available");
		}
		else {
			logger.warn("Support for WebLogic forceResume not available");
		}
	}
	catch (Exception ex) {
		throw new TransactionSystemException(
				"Could not initialize WebLogicJtaTransactionManager because WebLogic API classes are not available",
				ex);
	}
}
 
Example #28
Source File: JtaTransactionManager.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Look up the JTA TransactionManager in JNDI via the configured name.
 * <p>Called by {@code afterPropertiesSet} if no direct TransactionManager reference was set.
 * Can be overridden in subclasses to provide a different TransactionManager object.
 * @param transactionManagerName the JNDI name of the TransactionManager
 * @return the UserTransaction object
 * @throws TransactionSystemException if the JNDI lookup failed
 * @see #setJndiTemplate
 * @see #setTransactionManagerName
 */
protected TransactionManager lookupTransactionManager(String transactionManagerName)
		throws TransactionSystemException {
	try {
		if (logger.isDebugEnabled()) {
			logger.debug("Retrieving JTA TransactionManager from JNDI location [" + transactionManagerName + "]");
		}
		return getJndiTemplate().lookup(transactionManagerName, TransactionManager.class);
	}
	catch (NamingException ex) {
		throw new TransactionSystemException(
				"JTA TransactionManager is not available at JNDI location [" + transactionManagerName + "]", ex);
	}
}
 
Example #29
Source File: JtaTransactionManager.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Look up the JTA UserTransaction in JNDI via the configured name.
 * <p>Called by {@code afterPropertiesSet} if no direct UserTransaction reference was set.
 * Can be overridden in subclasses to provide a different UserTransaction object.
 * @param userTransactionName the JNDI name of the UserTransaction
 * @return the UserTransaction object
 * @throws TransactionSystemException if the JNDI lookup failed
 * @see #setJndiTemplate
 * @see #setUserTransactionName
 */
protected UserTransaction lookupUserTransaction(String userTransactionName)
		throws TransactionSystemException {
	try {
		if (logger.isDebugEnabled()) {
			logger.debug("Retrieving JTA UserTransaction from JNDI location [" + userTransactionName + "]");
		}
		return getJndiTemplate().lookup(userTransactionName, UserTransaction.class);
	}
	catch (NamingException ex) {
		throw new TransactionSystemException(
				"JTA UserTransaction is not available at JNDI location [" + userTransactionName + "]", ex);
	}
}
 
Example #30
Source File: WebSphereUowTransactionManager.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Look up the WebSphere UOWManager in JNDI via the configured name.
 * @param uowManagerName the JNDI name of the UOWManager
 * @return the UOWManager object
 * @throws TransactionSystemException if the JNDI lookup failed
 * @see #setJndiTemplate
 * @see #setUowManagerName
 */
protected UOWManager lookupUowManager(String uowManagerName) throws TransactionSystemException {
	try {
		if (logger.isDebugEnabled()) {
			logger.debug("Retrieving WebSphere UOWManager from JNDI location [" + uowManagerName + "]");
		}
		return getJndiTemplate().lookup(uowManagerName, UOWManager.class);
	}
	catch (NamingException ex) {
		throw new TransactionSystemException(
				"WebSphere UOWManager is not available at JNDI location [" + uowManagerName + "]", ex);
	}
}