Skip to content

Commit

Permalink
Consistent reset of resource holders on doBegin failure
Browse files Browse the repository at this point in the history
Issue: SPR-12280
(cherry picked from commit e58b33a)
  • Loading branch information
jhoeller committed Oct 6, 2014
1 parent c672678 commit 9758bc7
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 19 deletions.
Expand Up @@ -179,7 +179,7 @@ protected Object doGetTransaction() {
DataSourceTransactionObject txObject = new DataSourceTransactionObject();
txObject.setSavepointAllowed(isNestedTransactionAllowed());
ConnectionHolder conHolder =
(ConnectionHolder) TransactionSynchronizationManager.getResource(this.dataSource);
(ConnectionHolder) TransactionSynchronizationManager.getResource(this.dataSource);
txObject.setConnectionHolder(conHolder, false);
return txObject;
}
Expand Down Expand Up @@ -238,7 +238,10 @@ protected void doBegin(Object transaction, TransactionDefinition definition) {
}

catch (Throwable ex) {
DataSourceUtils.releaseConnection(con, this.dataSource);
if (txObject.isNewConnectionHolder()) {
DataSourceUtils.releaseConnection(con, this.dataSource);
txObject.setConnectionHolder(null, false);
}
throw new CannotCreateTransactionException("Could not open JDBC Connection for transaction", ex);
}
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -178,6 +178,7 @@ protected void doBegin(Object transaction, TransactionDefinition definition) {
if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
throw new InvalidIsolationLevelException("JMS does not support an isolation level concept");
}

JmsTransactionObject txObject = (JmsTransactionObject) transaction;
Connection con = null;
Session session = null;
Expand All @@ -193,10 +194,17 @@ protected void doBegin(Object transaction, TransactionDefinition definition) {
if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
txObject.getResourceHolder().setTimeoutInSeconds(timeout);
}
TransactionSynchronizationManager.bindResource(
getConnectionFactory(), txObject.getResourceHolder());
TransactionSynchronizationManager.bindResource(getConnectionFactory(), txObject.getResourceHolder());
}
catch (Throwable ex) {
if (session != null) {
try {
session.close();
}
catch (Throwable ex2) {
// ignore
}
}
if (con != null) {
try {
con.close();
Expand Down
Expand Up @@ -513,6 +513,7 @@ protected void doBegin(Object transaction, TransactionDefinition definition) {
}
finally {
SessionFactoryUtils.closeSession(session);
txObject.setSessionHolder(null);
}
}
throw new CannotCreateTransactionException("Could not open Hibernate Session for transaction", ex);
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -596,6 +596,7 @@ protected void doBegin(Object transaction, TransactionDefinition definition) {
}
finally {
SessionFactoryUtils.closeSession(session);
txObject.setSessionHolder(null);
}
}
throw new CannotCreateTransactionException("Could not open Hibernate Session for transaction", ex);
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -341,15 +341,16 @@ public int getTimeout() {
conHolder.setTimeoutInSeconds(timeoutToUse);
}
if (logger.isDebugEnabled()) {
logger.debug("Exposing JDO transaction as JDBC transaction [" + conHolder.getConnectionHandle() + "]");
logger.debug("Exposing JDO transaction as JDBC transaction [" +
conHolder.getConnectionHandle() + "]");
}
TransactionSynchronizationManager.bindResource(getDataSource(), conHolder);
txObject.setConnectionHolder(conHolder);
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Not exposing JDO transaction [" + pm + "] as JDBC transaction because JdoDialect [" +
getJdoDialect() + "] does not support JDBC Connection retrieval");
logger.debug("Not exposing JDO transaction [" + pm + "] as JDBC transaction because " +
"JdoDialect [" + getJdoDialect() + "] does not support JDBC Connection retrieval");
}
}
}
Expand Down Expand Up @@ -391,6 +392,7 @@ protected void closePersistenceManagerAfterFailedBegin(JdoTransactionObject txOb
finally {
PersistenceManagerFactoryUtils.releasePersistenceManager(pm, getPersistenceManagerFactory());
}
txObject.setPersistenceManagerHolder(null, false);
}
}

Expand Down
Expand Up @@ -331,8 +331,8 @@ protected Object doGetTransaction() {
TransactionSynchronizationManager.getResource(getEntityManagerFactory());
if (emHolder != null) {
if (logger.isDebugEnabled()) {
logger.debug("Found thread-bound EntityManager [" +
emHolder.getEntityManager() + "] for JPA transaction");
logger.debug("Found thread-bound EntityManager [" + emHolder.getEntityManager() +
"] for JPA transaction");
}
txObject.setEntityManagerHolder(emHolder, false);
}
Expand Down Expand Up @@ -400,15 +400,16 @@ public int getTimeout() {
conHolder.setTimeoutInSeconds(timeoutToUse);
}
if (logger.isDebugEnabled()) {
logger.debug("Exposing JPA transaction as JDBC transaction [" + conHolder.getConnectionHandle() + "]");
logger.debug("Exposing JPA transaction as JDBC transaction [" +
conHolder.getConnectionHandle() + "]");
}
TransactionSynchronizationManager.bindResource(getDataSource(), conHolder);
txObject.setConnectionHolder(conHolder);
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Not exposing JPA transaction [" + em + "] as JDBC transaction because JpaDialect [" +
getJpaDialect() + "] does not support JDBC Connection retrieval");
logger.debug("Not exposing JPA transaction [" + em + "] as JDBC transaction because " +
"JpaDialect [" + getJpaDialect() + "] does not support JDBC Connection retrieval");
}
}
}
Expand Down Expand Up @@ -467,6 +468,7 @@ protected void closeEntityManagerAfterFailedBegin(JpaTransactionObject txObject)
finally {
EntityManagerFactoryUtils.closeEntityManager(em);
}
txObject.setEntityManagerHolder(null, false);
}
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -126,7 +126,7 @@ public Object getResourceFactory() {
protected Object doGetTransaction() {
CciLocalTransactionObject txObject = new CciLocalTransactionObject();
ConnectionHolder conHolder =
(ConnectionHolder) TransactionSynchronizationManager.getResource(getConnectionFactory());
(ConnectionHolder) TransactionSynchronizationManager.getResource(getConnectionFactory());
txObject.setConnectionHolder(conHolder);
return txObject;
}
Expand Down Expand Up @@ -159,7 +159,6 @@ protected void doBegin(Object transaction, TransactionDefinition definition) {
}
TransactionSynchronizationManager.bindResource(getConnectionFactory(), txObject.getConnectionHolder());
}

catch (NotSupportedException ex) {
ConnectionFactoryUtils.releaseConnection(con, getConnectionFactory());
throw new CannotCreateTransactionException("CCI Connection does not support local transactions", ex);
Expand Down Expand Up @@ -268,7 +267,7 @@ public void setConnectionHolder(ConnectionHolder connectionHolder) {
}

public ConnectionHolder getConnectionHolder() {
return connectionHolder;
return this.connectionHolder;
}
}

Expand Down

0 comments on commit 9758bc7

Please sign in to comment.