org.aspectj.lang.annotation.AfterThrowing#org.springframework.transaction.TransactionSystemException源码实例Demo

下面列出了org.aspectj.lang.annotation.AfterThrowing#org.springframework.transaction.TransactionSystemException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@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();
		}
	}
}
 
@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);
	}
}
 
@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();
}
 
@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();
}
 
@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();
}
 
@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);
		}
	}
}
 
@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();
		}
	}
}
 
@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();
}
 
源代码9 项目: lams   文件: JpaTransactionManager.java
@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();
		}
	}
}
 
@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();
}
 
@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();
}
 
@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();
		}
	}
}
 
@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();
}
 
源代码14 项目: sdn-rx   文件: Neo4jTransactionManager.java
@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()));
	}
}
 
@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<>();
	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();
}
 
private Mono<Void> completeTransactionAfterThrowing(@Nullable ReactiveTransactionInfo txInfo, Throwable ex) {
	if (txInfo != null && txInfo.getReactiveTransaction() != null) {
		if (logger.isTraceEnabled()) {
			logger.trace("Completing transaction for [" + txInfo.getJoinpointIdentification() +
					"] after exception: " + ex);
		}
		if (txInfo.transactionAttribute != null && txInfo.transactionAttribute.rollbackOn(ex)) {
			return txInfo.getTransactionManager().rollback(txInfo.getReactiveTransaction()).onErrorMap(ex2 -> {
						logger.error("Application exception overridden by rollback exception", ex);
						if (ex2 instanceof TransactionSystemException) {
							((TransactionSystemException) ex2).initApplicationException(ex);
						}
						return ex2;
					}
			);
		}
		else {
			// We don't roll back on this exception.
			// Will still roll back if TransactionStatus.isRollbackOnly() is true.
			return txInfo.getTransactionManager().commit(txInfo.getReactiveTransaction()).onErrorMap(ex2 -> {
						logger.error("Application exception overridden by commit exception", ex);
						if (ex2 instanceof TransactionSystemException) {
							((TransactionSystemException) ex2).initApplicationException(ex);
						}
						return ex2;
					}
			);
		}
	}
	return Mono.empty();
}
 
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.debug("Support for WebLogic transaction names available");
		}
		else {
			logger.debug("Support for WebLogic transaction names not available");
		}

		// Obtain WebLogic ClientTransactionManager interface.
		Class<?> transactionManagerClass =
				getClass().getClassLoader().loadClass(CLIENT_TRANSACTION_MANAGER_CLASS_NAME);
		logger.trace("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.debug("Support for WebLogic forceResume not available");
		}
	}
	catch (Exception ex) {
		throw new TransactionSystemException(
				"Could not initialize WebLogicJtaTransactionManager because WebLogic API classes are not available",
				ex);
	}
}
 
/**
 * Initialize the UserTransaction as well as the TransactionManager handle.
 * @see #initUserTransactionAndTransactionManager()
 */
@Override
public void afterPropertiesSet() throws TransactionSystemException {
	initUserTransactionAndTransactionManager();
	checkUserTransactionAndTransactionManager();
	initTransactionSynchronizationRegistry();
}
 
/**
 * 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);
	}
}
 
/**
 * Look up the JTA 1.1 TransactionSynchronizationRegistry in JNDI via the configured name.
 * <p>Can be overridden in subclasses to provide a different TransactionManager object.
 * @param registryName the JNDI name of the
 * TransactionSynchronizationRegistry
 * @return the TransactionSynchronizationRegistry object
 * @throws TransactionSystemException if the JNDI lookup failed
 * @see #setJndiTemplate
 * @see #setTransactionSynchronizationRegistryName
 */
protected TransactionSynchronizationRegistry lookupTransactionSynchronizationRegistry(String registryName) throws TransactionSystemException {
	try {
		if (logger.isDebugEnabled()) {
			logger.debug("Retrieving JTA TransactionSynchronizationRegistry from JNDI location [" + registryName + "]");
		}
		return getJndiTemplate().lookup(registryName, TransactionSynchronizationRegistry.class);
	}
	catch (NamingException ex) {
		throw new TransactionSystemException(
				"JTA TransactionSynchronizationRegistry is not available at JNDI location [" + registryName + "]", ex);
	}
}
 
public void rollback() {
    log.debug("Performing rollback");
    while (!operationExecutors.isEmpty()) {
        CompensatingTransactionOperationExecutor rollbackOperation = operationExecutors.pop();
        try {
            rollbackOperation.rollback();
        } catch (Exception e) {
            throw new TransactionSystemException(
                    "Error occurred during rollback", e);
        }
    }
}
 
@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);
	}
}
 
@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);
	}
}
 
源代码24 项目: syncope   文件: SCIMExceptionMapper.java
private static ResponseBuilder processInvalidEntityExceptions(final Exception ex) {
    InvalidEntityException iee = null;

    if (ex instanceof InvalidEntityException) {
        iee = (InvalidEntityException) ex;
    }
    if (ex instanceof TransactionSystemException && ROLLBACK_EXCLASS.isAssignableFrom(ex.getCause().getClass())
            && ex.getCause().getCause() instanceof InvalidEntityException) {

        iee = (InvalidEntityException) ex.getCause().getCause();
    }

    if (iee != null) {
        ClientExceptionType exType;
        if (iee.getEntityClassSimpleName().endsWith("Policy")) {
            exType = ClientExceptionType.InvalidPolicy;
        } else if (iee.getEntityClassSimpleName().equals(PlainAttr.class.getSimpleName())) {
            exType = ClientExceptionType.InvalidValues;
        } else {
            try {
                exType = ClientExceptionType.valueOf("Invalid" + iee.getEntityClassSimpleName());
            } catch (IllegalArgumentException e) {
                // ignore
                exType = ClientExceptionType.InvalidEntity;
            }
        }

        StringBuilder msg = new StringBuilder();

        for (Map.Entry<Class<?>, Set<EntityViolationType>> violation : iee.getViolations().entrySet()) {
            for (EntityViolationType violationType : violation.getValue()) {
                msg.append(violationType.name()).append(": ").append(violationType.getMessage()).append('\n');
            }
        }

        return builder(exType, msg.toString());
    }

    return null;
}
 
/**
 * This implementation checks the UserTransaction's rollback-only flag.
 */
@Override
public boolean isRollbackOnly() {
	if (this.userTransaction == null) {
		return false;
	}
	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);
	}
}
 
/**
 * 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);
	}
}
 
/**
 * 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();
	}
}
 
源代码28 项目: spring4-understanding   文件: LobCreatorUtils.java
/**
 * Register a transaction synchronization for closing the given LobCreator,
 * preferring Spring transaction synchronization and falling back to
 * plain JTA transaction synchronization.
 * @param lobCreator the LobCreator to close after transaction completion
 * @param jtaTransactionManager the JTA TransactionManager to fall back to
 * when no Spring transaction synchronization is active (may be {@code null})
 * @throws IllegalStateException if there is neither active Spring transaction
 * synchronization nor active JTA transaction synchronization
 */
public static void registerTransactionSynchronization(
		LobCreator lobCreator, TransactionManager jtaTransactionManager) throws IllegalStateException {

	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		logger.debug("Registering Spring transaction synchronization for LobCreator");
		TransactionSynchronizationManager.registerSynchronization(
			new SpringLobCreatorSynchronization(lobCreator));
	}
	else {
		if (jtaTransactionManager != null) {
			try {
				int jtaStatus = jtaTransactionManager.getStatus();
				if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
					logger.debug("Registering JTA transaction synchronization for LobCreator");
					jtaTransactionManager.getTransaction().registerSynchronization(
							new JtaLobCreatorSynchronization(lobCreator));
					return;
				}
			}
			catch (Throwable ex) {
				throw new TransactionSystemException(
						"Could not register synchronization with JTA TransactionManager", ex);
			}
		}
		throw new IllegalStateException("Active Spring transaction synchronization or active " +
			"JTA transaction with specified [javax.transaction.TransactionManager] required");
	}
}
 
源代码29 项目: lams   文件: LobCreatorUtils.java
/**
 * Register a transaction synchronization for closing the given LobCreator,
 * preferring Spring transaction synchronization and falling back to
 * plain JTA transaction synchronization.
 * @param lobCreator the LobCreator to close after transaction completion
 * @param jtaTransactionManager the JTA TransactionManager to fall back to
 * when no Spring transaction synchronization is active (may be {@code null})
 * @throws IllegalStateException if there is neither active Spring transaction
 * synchronization nor active JTA transaction synchronization
 */
public static void registerTransactionSynchronization(
		LobCreator lobCreator, TransactionManager jtaTransactionManager) throws IllegalStateException {

	if (TransactionSynchronizationManager.isSynchronizationActive()) {
		logger.debug("Registering Spring transaction synchronization for LobCreator");
		TransactionSynchronizationManager.registerSynchronization(
			new SpringLobCreatorSynchronization(lobCreator));
	}
	else {
		if (jtaTransactionManager != null) {
			try {
				int jtaStatus = jtaTransactionManager.getStatus();
				if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
					logger.debug("Registering JTA transaction synchronization for LobCreator");
					jtaTransactionManager.getTransaction().registerSynchronization(
							new JtaLobCreatorSynchronization(lobCreator));
					return;
				}
			}
			catch (Throwable ex) {
				throw new TransactionSystemException(
						"Could not register synchronization with JTA TransactionManager", ex);
			}
		}
		throw new IllegalStateException("Active Spring transaction synchronization or active " +
			"JTA transaction with specified [javax.transaction.TransactionManager] required");
	}
}
 
@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);
	}
}