diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceTransactionManager.java b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceTransactionManager.java index 10f4e36875f..421ecb787ef 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceTransactionManager.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/datasource/DataSourceTransactionManager.java @@ -179,7 +179,7 @@ public class DataSourceTransactionManager extends AbstractPlatformTransactionMan 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; } @@ -238,7 +238,10 @@ public class DataSourceTransactionManager extends AbstractPlatformTransactionMan } 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); } } diff --git a/spring-jms/src/main/java/org/springframework/jms/connection/JmsTransactionManager.java b/spring-jms/src/main/java/org/springframework/jms/connection/JmsTransactionManager.java index ebd78180183..6ce1d38c9ca 100644 --- a/spring-jms/src/main/java/org/springframework/jms/connection/JmsTransactionManager.java +++ b/spring-jms/src/main/java/org/springframework/jms/connection/JmsTransactionManager.java @@ -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. @@ -178,6 +178,7 @@ public class JmsTransactionManager extends AbstractPlatformTransactionManager 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; @@ -193,10 +194,17 @@ public class JmsTransactionManager extends AbstractPlatformTransactionManager 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(); diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateTransactionManager.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateTransactionManager.java index b74bc8c33d4..32f61019817 100644 --- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateTransactionManager.java +++ b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/HibernateTransactionManager.java @@ -513,6 +513,7 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana } finally { SessionFactoryUtils.closeSession(session); + txObject.setSessionHolder(null); } } throw new CannotCreateTransactionException("Could not open Hibernate Session for transaction", ex); diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateTransactionManager.java b/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateTransactionManager.java index 5288e55772b..1f5938ae168 100644 --- a/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateTransactionManager.java +++ b/spring-orm/src/main/java/org/springframework/orm/hibernate3/HibernateTransactionManager.java @@ -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. @@ -596,6 +596,7 @@ public class HibernateTransactionManager extends AbstractPlatformTransactionMana } finally { SessionFactoryUtils.closeSession(session); + txObject.setSessionHolder(null); } } throw new CannotCreateTransactionException("Could not open Hibernate Session for transaction", ex); diff --git a/spring-orm/src/main/java/org/springframework/orm/jdo/JdoTransactionManager.java b/spring-orm/src/main/java/org/springframework/orm/jdo/JdoTransactionManager.java index 73fe6d1a53b..e71b0862139 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jdo/JdoTransactionManager.java +++ b/spring-orm/src/main/java/org/springframework/orm/jdo/JdoTransactionManager.java @@ -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. @@ -341,15 +341,16 @@ public class JdoTransactionManager extends AbstractPlatformTransactionManager 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"); } } } @@ -391,6 +392,7 @@ public class JdoTransactionManager extends AbstractPlatformTransactionManager finally { PersistenceManagerFactoryUtils.releasePersistenceManager(pm, getPersistenceManagerFactory()); } + txObject.setPersistenceManagerHolder(null, false); } } diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/JpaTransactionManager.java b/spring-orm/src/main/java/org/springframework/orm/jpa/JpaTransactionManager.java index 7121f9c2cec..a4308ff32de 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/JpaTransactionManager.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/JpaTransactionManager.java @@ -331,8 +331,8 @@ public class JpaTransactionManager extends AbstractPlatformTransactionManager 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); } @@ -400,15 +400,16 @@ public class JpaTransactionManager extends AbstractPlatformTransactionManager 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"); } } } @@ -467,6 +468,7 @@ public class JpaTransactionManager extends AbstractPlatformTransactionManager finally { EntityManagerFactoryUtils.closeEntityManager(em); } + txObject.setEntityManagerHolder(null, false); } } diff --git a/spring-tx/src/main/java/org/springframework/jca/cci/connection/CciLocalTransactionManager.java b/spring-tx/src/main/java/org/springframework/jca/cci/connection/CciLocalTransactionManager.java index 08ac3c1aa0d..f2dc5e9e200 100644 --- a/spring-tx/src/main/java/org/springframework/jca/cci/connection/CciLocalTransactionManager.java +++ b/spring-tx/src/main/java/org/springframework/jca/cci/connection/CciLocalTransactionManager.java @@ -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. @@ -126,7 +126,7 @@ public class CciLocalTransactionManager extends AbstractPlatformTransactionManag protected Object doGetTransaction() { CciLocalTransactionObject txObject = new CciLocalTransactionObject(); ConnectionHolder conHolder = - (ConnectionHolder) TransactionSynchronizationManager.getResource(getConnectionFactory()); + (ConnectionHolder) TransactionSynchronizationManager.getResource(getConnectionFactory()); txObject.setConnectionHolder(conHolder); return txObject; } @@ -159,7 +159,6 @@ public class CciLocalTransactionManager extends AbstractPlatformTransactionManag } TransactionSynchronizationManager.bindResource(getConnectionFactory(), txObject.getConnectionHolder()); } - catch (NotSupportedException ex) { ConnectionFactoryUtils.releaseConnection(con, getConnectionFactory()); throw new CannotCreateTransactionException("CCI Connection does not support local transactions", ex); @@ -268,7 +267,7 @@ public class CciLocalTransactionManager extends AbstractPlatformTransactionManag } public ConnectionHolder getConnectionHolder() { - return connectionHolder; + return this.connectionHolder; } }