org.springframework.transaction.NestedTransactionNotSupportedException Java Examples

The following examples show how to use org.springframework.transaction.NestedTransactionNotSupportedException. 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: WebSphereUowTransactionManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void propagationNestedFailsInCaseOfExistingTransaction() {
	MockUOWManager manager = new MockUOWManager();
	manager.setUOWStatus(UOWManager.UOW_STATUS_ACTIVE);
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);

	try {
		ptm.execute(definition, new TransactionCallback<String>() {
			@Override
			public String doInTransaction(TransactionStatus status) {
				return "result";
			}
		});
		fail("Should have thrown NestedTransactionNotSupportedException");
	}
	catch (NestedTransactionNotSupportedException ex) {
		// expected
	}
}
 
Example #2
Source File: JdbcTransactionObjectSupport.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * This implementation creates a JDBC 3.0 Savepoint and returns it.
 * @see java.sql.Connection#setSavepoint
 */
@Override
public Object createSavepoint() throws TransactionException {
	ConnectionHolder conHolder = getConnectionHolderForSavepoint();
	try {
		if (!conHolder.supportsSavepoints()) {
			throw new NestedTransactionNotSupportedException(
					"Cannot create a nested transaction because savepoints are not supported by your JDBC driver");
		}
		if (conHolder.isRollbackOnly()) {
			throw new CannotCreateTransactionException(
					"Cannot create savepoint for transaction which is already marked as rollback-only");
		}
		return conHolder.createSavepoint();
	}
	catch (SQLException ex) {
		throw new CannotCreateTransactionException("Could not create JDBC savepoint", ex);
	}
}
 
Example #3
Source File: WebSphereUowTransactionManagerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void propagationNestedFailsInCaseOfExistingTransaction() {
	MockUOWManager manager = new MockUOWManager();
	manager.setUOWStatus(UOWManager.UOW_STATUS_ACTIVE);
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);

	try {
		ptm.execute(definition, new TransactionCallback<String>() {
			@Override
			public String doInTransaction(TransactionStatus status) {
				return "result";
			}
		});
		fail("Should have thrown NestedTransactionNotSupportedException");
	}
	catch (NestedTransactionNotSupportedException ex) {
		// expected
	}
}
 
Example #4
Source File: WebSphereUowTransactionManagerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void propagationNestedFailsInCaseOfExistingTransaction() {
	MockUOWManager manager = new MockUOWManager();
	manager.setUOWStatus(UOWManager.UOW_STATUS_ACTIVE);
	WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
	DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
	definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);

	try {
		ptm.execute(definition, new TransactionCallback<String>() {
			@Override
			public String doInTransaction(TransactionStatus status) {
				return "result";
			}
		});
		fail("Should have thrown NestedTransactionNotSupportedException");
	}
	catch (NestedTransactionNotSupportedException ex) {
		// expected
	}
}
 
Example #5
Source File: JdbcTransactionObjectSupport.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * This implementation creates a JDBC 3.0 Savepoint and returns it.
 * @see java.sql.Connection#setSavepoint
 */
@Override
public Object createSavepoint() throws TransactionException {
	ConnectionHolder conHolder = getConnectionHolderForSavepoint();
	try {
		if (!conHolder.supportsSavepoints()) {
			throw new NestedTransactionNotSupportedException(
					"Cannot create a nested transaction because savepoints are not supported by your JDBC driver");
		}
		if (conHolder.isRollbackOnly()) {
			throw new CannotCreateTransactionException(
					"Cannot create savepoint for transaction which is already marked as rollback-only");
		}
		return conHolder.createSavepoint();
	}
	catch (SQLException ex) {
		throw new CannotCreateTransactionException("Could not create JDBC savepoint", ex);
	}
}
 
Example #6
Source File: DefaultTransactionStatus.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * This implementation exposes the {@link SavepointManager} interface
 * of the underlying transaction object, if any.
 * @throws NestedTransactionNotSupportedException if savepoints are not supported
 * @see #isTransactionSavepointManager()
 */
@Override
protected SavepointManager getSavepointManager() {
	Object transaction = this.transaction;
	if (!(transaction instanceof SavepointManager)) {
		throw new NestedTransactionNotSupportedException(
				"Transaction object [" + this.transaction + "] does not support savepoints");
	}
	return (SavepointManager) transaction;
}
 
Example #7
Source File: JdbcTransactionObjectSupport.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
protected ConnectionHolder getConnectionHolderForSavepoint() throws TransactionException {
	if (!isSavepointAllowed()) {
		throw new NestedTransactionNotSupportedException(
				"Transaction manager does not allow nested transactions");
	}
	if (!hasConnectionHolder()) {
		throw new TransactionUsageException(
				"Cannot create nested transaction if not exposing a JDBC transaction");
	}
	return getConnectionHolder();
}
 
Example #8
Source File: JdbcTransactionObjectSupport.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
/**
 * This implementation creates a JDBC 3.0 Savepoint and returns it.
 * @see java.sql.Connection#setSavepoint
 */
@Override
public Object createSavepoint() throws TransactionException {
	ConnectionHolder conHolder = getConnectionHolderForSavepoint();
	try {
		if (!conHolder.supportsSavepoints()) {
			throw new NestedTransactionNotSupportedException(
					"Cannot create a nested transaction because savepoints are not supported by your JDBC driver");
		}
		return conHolder.createSavepoint();
	}
	catch (SQLException ex) {
		throw new CannotCreateTransactionException("Could not create JDBC savepoint", ex);
	}
}
 
Example #9
Source File: JdbcTransactionObjectSupport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected ConnectionHolder getConnectionHolderForSavepoint() throws TransactionException {
	if (!isSavepointAllowed()) {
		throw new NestedTransactionNotSupportedException(
				"Transaction manager does not allow nested transactions");
	}
	if (!hasConnectionHolder()) {
		throw new TransactionUsageException(
				"Cannot create nested transaction if not exposing a JDBC transaction");
	}
	return getConnectionHolder();
}
 
Example #10
Source File: JdbcTransactionObjectSupport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * This implementation creates a JDBC 3.0 Savepoint and returns it.
 * @see java.sql.Connection#setSavepoint
 */
@Override
public Object createSavepoint() throws TransactionException {
	ConnectionHolder conHolder = getConnectionHolderForSavepoint();
	try {
		if (!conHolder.supportsSavepoints()) {
			throw new NestedTransactionNotSupportedException(
					"Cannot create a nested transaction because savepoints are not supported by your JDBC driver");
		}
		return conHolder.createSavepoint();
	}
	catch (SQLException ex) {
		throw new CannotCreateTransactionException("Could not create JDBC savepoint", ex);
	}
}
 
Example #11
Source File: DefaultTransactionStatus.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * This implementation exposes the SavepointManager interface
 * of the underlying transaction object, if any.
 */
@Override
protected SavepointManager getSavepointManager() {
	if (!isTransactionSavepointManager()) {
		throw new NestedTransactionNotSupportedException(
			"Transaction object [" + getTransaction() + "] does not support savepoints");
	}
	return (SavepointManager) getTransaction();
}
 
Example #12
Source File: JpaTransactionManager.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private SavepointManager getSavepointManager() {
	if (!isSavepointAllowed()) {
		throw new NestedTransactionNotSupportedException(
				"Transaction manager does not allow nested transactions");
	}
	SavepointManager savepointManager = getEntityManagerHolder().getSavepointManager();
	if (savepointManager == null) {
		throw new NestedTransactionNotSupportedException(
				"JpaDialect does not support savepoints - check your JPA provider's capabilities");
	}
	return savepointManager;
}
 
Example #13
Source File: JpaTransactionManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private SavepointManager getSavepointManager() {
	if (!isSavepointAllowed()) {
		throw new NestedTransactionNotSupportedException(
				"Transaction manager does not allow nested transactions");
	}
	SavepointManager savepointManager = getEntityManagerHolder().getSavepointManager();
	if (savepointManager == null) {
		throw new NestedTransactionNotSupportedException(
				"JpaDialect does not support savepoints - check your JPA provider's capabilities");
	}
	return savepointManager;
}
 
Example #14
Source File: JdbcTransactionObjectSupport.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected ConnectionHolder getConnectionHolderForSavepoint() throws TransactionException {
	if (!isSavepointAllowed()) {
		throw new NestedTransactionNotSupportedException(
				"Transaction manager does not allow nested transactions");
	}
	if (!hasConnectionHolder()) {
		throw new TransactionUsageException(
				"Cannot create nested transaction when not exposing a JDBC transaction");
	}
	return getConnectionHolder();
}
 
Example #15
Source File: JdbcTransactionObjectSupport.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This implementation creates a JDBC 3.0 Savepoint and returns it.
 * @see java.sql.Connection#setSavepoint
 */
@Override
public Object createSavepoint() throws TransactionException {
	ConnectionHolder conHolder = getConnectionHolderForSavepoint();
	try {
		if (!conHolder.supportsSavepoints()) {
			throw new NestedTransactionNotSupportedException(
					"Cannot create a nested transaction because savepoints are not supported by your JDBC driver");
		}
		return conHolder.createSavepoint();
	}
	catch (SQLException ex) {
		throw new CannotCreateTransactionException("Could not create JDBC savepoint", ex);
	}
}
 
Example #16
Source File: DefaultTransactionStatus.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * This implementation exposes the SavepointManager interface
 * of the underlying transaction object, if any.
 */
@Override
protected SavepointManager getSavepointManager() {
	if (!isTransactionSavepointManager()) {
		throw new NestedTransactionNotSupportedException(
			"Transaction object [" + getTransaction() + "] does not support savepoints");
	}
	return (SavepointManager) getTransaction();
}
 
Example #17
Source File: JdbcTransactionObjectSupport.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected ConnectionHolder getConnectionHolderForSavepoint() throws TransactionException {
	if (!isSavepointAllowed()) {
		throw new NestedTransactionNotSupportedException(
				"Transaction manager does not allow nested transactions");
	}
	if (!hasConnectionHolder()) {
		throw new TransactionUsageException(
				"Cannot create nested transaction when not exposing a JDBC transaction");
	}
	return getConnectionHolder();
}
 
Example #18
Source File: DefaultTransactionStatus.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * This implementation exposes the {@link SavepointManager} interface
 * of the underlying transaction object, if any.
 * @throws NestedTransactionNotSupportedException if savepoints are not supported
 * @see #isTransactionSavepointManager()
 */
@Override
protected SavepointManager getSavepointManager() {
	Object transaction = this.transaction;
	if (!(transaction instanceof SavepointManager)) {
		throw new NestedTransactionNotSupportedException(
				"Transaction object [" + this.transaction + "] does not support savepoints");
	}
	return (SavepointManager) transaction;
}
 
Example #19
Source File: JpaTransactionManager.java    From java-technology-stack with MIT License 5 votes vote down vote up
private SavepointManager getSavepointManager() {
	if (!isSavepointAllowed()) {
		throw new NestedTransactionNotSupportedException(
				"Transaction manager does not allow nested transactions");
	}
	SavepointManager savepointManager = getEntityManagerHolder().getSavepointManager();
	if (savepointManager == null) {
		throw new NestedTransactionNotSupportedException(
				"JpaDialect does not support savepoints - check your JPA provider's capabilities");
	}
	return savepointManager;
}
 
Example #20
Source File: JdbcTransactionObjectSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected ConnectionHolder getConnectionHolderForSavepoint() throws TransactionException {
	if (!isSavepointAllowed()) {
		throw new NestedTransactionNotSupportedException(
				"Transaction manager does not allow nested transactions");
	}
	if (!hasConnectionHolder()) {
		throw new TransactionUsageException(
				"Cannot create nested transaction when not exposing a JDBC transaction");
	}
	return getConnectionHolder();
}
 
Example #21
Source File: JpaTransactionManager.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private SavepointManager getSavepointManager() {
	if (!isSavepointAllowed()) {
		throw new NestedTransactionNotSupportedException(
				"Transaction manager does not allow nested transactions");
	}
	SavepointManager savepointManager = getEntityManagerHolder().getSavepointManager();
	if (savepointManager == null) {
		throw new NestedTransactionNotSupportedException(
				"JpaDialect does not support savepoints - check your JPA provider's capabilities");
	}
	return savepointManager;
}
 
Example #22
Source File: WebSphereUowTransactionManager.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@Nullable
public <T> T execute(@Nullable TransactionDefinition definition, TransactionCallback<T> callback)
		throws TransactionException {

	// Use defaults if no transaction definition given.
	TransactionDefinition def = (definition != null ? definition : TransactionDefinition.withDefaults());

	if (def.getTimeout() < TransactionDefinition.TIMEOUT_DEFAULT) {
		throw new InvalidTimeoutException("Invalid transaction timeout", def.getTimeout());
	}

	UOWManager uowManager = obtainUOWManager();
	int pb = def.getPropagationBehavior();
	boolean existingTx = (uowManager.getUOWStatus() != UOWSynchronizationRegistry.UOW_STATUS_NONE &&
			uowManager.getUOWType() != UOWSynchronizationRegistry.UOW_TYPE_LOCAL_TRANSACTION);

	int uowType = UOWSynchronizationRegistry.UOW_TYPE_GLOBAL_TRANSACTION;
	boolean joinTx = false;
	boolean newSynch = false;

	if (existingTx) {
		if (pb == TransactionDefinition.PROPAGATION_NEVER) {
			throw new IllegalTransactionStateException(
					"Transaction propagation 'never' but existing transaction found");
		}
		if (pb == TransactionDefinition.PROPAGATION_NESTED) {
			throw new NestedTransactionNotSupportedException(
					"Transaction propagation 'nested' not supported for WebSphere UOW transactions");
		}
		if (pb == TransactionDefinition.PROPAGATION_SUPPORTS ||
				pb == TransactionDefinition.PROPAGATION_REQUIRED ||
				pb == TransactionDefinition.PROPAGATION_MANDATORY) {
			joinTx = true;
			newSynch = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
		}
		else if (pb == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
			uowType = UOWSynchronizationRegistry.UOW_TYPE_LOCAL_TRANSACTION;
			newSynch = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);
		}
		else {
			newSynch = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
		}
	}
	else {
		if (pb == TransactionDefinition.PROPAGATION_MANDATORY) {
			throw new IllegalTransactionStateException(
					"Transaction propagation 'mandatory' but no existing transaction found");
		}
		if (pb == TransactionDefinition.PROPAGATION_SUPPORTS ||
				pb == TransactionDefinition.PROPAGATION_NOT_SUPPORTED ||
				pb == TransactionDefinition.PROPAGATION_NEVER) {
			uowType = UOWSynchronizationRegistry.UOW_TYPE_LOCAL_TRANSACTION;
			newSynch = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS);
		}
		else {
			newSynch = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER);
		}
	}

	boolean debug = logger.isDebugEnabled();
	if (debug) {
		logger.debug("Creating new transaction with name [" + def.getName() + "]: " + def);
	}
	SuspendedResourcesHolder suspendedResources = (!joinTx ? suspend(null) : null);
	UOWActionAdapter<T> action = null;
	try {
		if (def.getTimeout() > TransactionDefinition.TIMEOUT_DEFAULT) {
			uowManager.setUOWTimeout(uowType, def.getTimeout());
		}
		if (debug) {
			logger.debug("Invoking WebSphere UOW action: type=" + uowType + ", join=" + joinTx);
		}
		action = new UOWActionAdapter<>(
				def, callback, (uowType == UOWManager.UOW_TYPE_GLOBAL_TRANSACTION), !joinTx, newSynch, debug);
		uowManager.runUnderUOW(uowType, joinTx, action);
		if (debug) {
			logger.debug("Returned from WebSphere UOW action: type=" + uowType + ", join=" + joinTx);
		}
		return action.getResult();
	}
	catch (UOWException | UOWActionException ex) {
		TransactionSystemException tse =
				new TransactionSystemException("UOWManager transaction processing failed", ex);
		Throwable appEx = action.getException();
		if (appEx != null) {
			logger.error("Application exception overridden by rollback exception", appEx);
			tse.initApplicationException(appEx);
		}
		throw tse;
	}
	finally {
		if (suspendedResources != null) {
			resume(null, suspendedResources);
		}
	}
}
 
Example #23
Source File: HazelcastUtils.java    From hazelcastmq with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the transaction context holder bound to the current transaction. If
 * one cannot be found and a transaction is active, a new one will be created
 * and bound to the thread. If one cannot be found and a transaction is not
 * active, this method returns null.
 *
 * @param hazelcastInstance the Hazelcast instance used to create begin a
 * transaction if needed
 *
 * @return the transaction context holder if a transaction is active, null
 * otherwise
 */
private static TransactionContext getHazelcastTransactionContext(
    HazelcastInstance hazelcastInstance,
    boolean synchedLocalTransactionAllowed) {

  HazelcastTransactionContextHolder conHolder =
      (HazelcastTransactionContextHolder) TransactionSynchronizationManager.
      getResource(hazelcastInstance);

  if (conHolder != null && (conHolder.hasTransactionContext() || conHolder.
      isSynchronizedWithTransaction())) {
    // We are already synchronized with the transaction which means
    // someone already requested a transactional resource or the transaction
    // is being managed at the top level by HazelcastTransactionManager.

    if (!conHolder.hasTransactionContext()) {
      // I think this means we are synchronized with the transaction but
      // we don't have a transactional context because the transaction was
      // suspended. I don't see how we can do this with Hazelcast because
      // it binds the transaction context to the thread and doesn't
      // supported nested transactions. Maybe I'm missing something.

      throw new NestedTransactionNotSupportedException("Trying to resume a "
          + "Hazelcast transaction? Can't do that.");
    }
  }
  else if (TransactionSynchronizationManager.isSynchronizationActive()
      && synchedLocalTransactionAllowed) {
    // No holder or no transaction context but we want to be
    // synchronized to the transaction.
    if (conHolder == null) {
      conHolder = new HazelcastTransactionContextHolder();
      TransactionSynchronizationManager.bindResource(hazelcastInstance,
          conHolder);
    }

    TransactionContext transactionContext = hazelcastInstance.
        newTransactionContext();
    transactionContext.beginTransaction();

    conHolder.setTransactionContext(transactionContext);
    conHolder.setSynchronizedWithTransaction(true);
    conHolder.setTransactionActive(true);
    TransactionSynchronizationManager.registerSynchronization(
        new HazelcastTransactionSynchronization(conHolder, hazelcastInstance));
  }

  return conHolder != null ? conHolder.getTransactionContext() : null;
}
 
Example #24
Source File: AbstractTransactionStatus.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Return a SavepointManager for the underlying transaction, if possible.
 * <p>Default implementation always throws a NestedTransactionNotSupportedException.
 * @throws org.springframework.transaction.NestedTransactionNotSupportedException
 * if the underlying transaction does not support savepoints
 */
protected SavepointManager getSavepointManager() {
	throw new NestedTransactionNotSupportedException("This transaction does not support savepoints");
}
 
Example #25
Source File: AbstractTransactionStatus.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Return a SavepointManager for the underlying transaction, if possible.
 * <p>Default implementation always throws a NestedTransactionNotSupportedException.
 * @throws org.springframework.transaction.NestedTransactionNotSupportedException
 * if the underlying transaction does not support savepoints
 */
protected SavepointManager getSavepointManager() {
	throw new NestedTransactionNotSupportedException("This transaction does not support savepoints");
}
 
Example #26
Source File: AbstractTransactionStatus.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Return a SavepointManager for the underlying transaction, if possible.
 * <p>Default implementation always throws a NestedTransactionNotSupportedException.
 * @throws org.springframework.transaction.NestedTransactionNotSupportedException
 * if the underlying transaction does not support savepoints
 */
protected SavepointManager getSavepointManager() {
	throw new NestedTransactionNotSupportedException("This transaction does not support savepoints");
}
 
Example #27
Source File: AbstractTransactionStatus.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Return a SavepointManager for the underlying transaction, if possible.
 * <p>Default implementation always throws a NestedTransactionNotSupportedException.
 * @throws org.springframework.transaction.NestedTransactionNotSupportedException
 * if the underlying transaction does not support savepoints
 */
protected SavepointManager getSavepointManager() {
	throw new NestedTransactionNotSupportedException("This transaction does not support savepoints");
}