diff --git a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringSessionSynchronization.java b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringSessionSynchronization.java index b2b86bc2d6..6eee684dfb 100644 --- a/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringSessionSynchronization.java +++ b/spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringSessionSynchronization.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -112,18 +112,21 @@ public class SpringSessionSynchronization implements TransactionSynchronization, @Override public void beforeCompletion() { - Session session = this.sessionHolder.getSession(); - if (this.sessionHolder.getPreviousFlushMode() != null) { - // In case of pre-bound Session, restore previous flush mode. - session.setFlushMode(this.sessionHolder.getPreviousFlushMode()); + try { + Session session = this.sessionHolder.getSession(); + if (this.sessionHolder.getPreviousFlushMode() != null) { + // In case of pre-bound Session, restore previous flush mode. + session.setFlushMode(this.sessionHolder.getPreviousFlushMode()); + } + // Eagerly disconnect the Session here, to make release mode "on_close" work nicely. + session.disconnect(); } - // Eagerly disconnect the Session here, to make release mode "on_close" work nicely. - session.disconnect(); - - // Unbind at this point if it's a new Session... - if (this.newSession) { - TransactionSynchronizationManager.unbindResource(this.sessionFactory); - this.holderActive = false; + finally { + // Unbind at this point if it's a new Session... + if (this.newSession) { + TransactionSynchronizationManager.unbindResource(this.sessionFactory); + this.holderActive = false; + } } } diff --git a/spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/SpringSessionSynchronization.java b/spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/SpringSessionSynchronization.java index 8cea09d490..ca56b71a39 100644 --- a/spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/SpringSessionSynchronization.java +++ b/spring-orm-hibernate5/src/main/java/org/springframework/orm/hibernate5/SpringSessionSynchronization.java @@ -112,18 +112,21 @@ public class SpringSessionSynchronization implements TransactionSynchronization, @Override public void beforeCompletion() { - Session session = this.sessionHolder.getSession(); - if (this.sessionHolder.getPreviousFlushMode() != null) { - // In case of pre-bound Session, restore previous flush mode. - session.setFlushMode(this.sessionHolder.getPreviousFlushMode()); + try { + Session session = this.sessionHolder.getSession(); + if (this.sessionHolder.getPreviousFlushMode() != null) { + // In case of pre-bound Session, restore previous flush mode. + session.setFlushMode(this.sessionHolder.getPreviousFlushMode()); + } + // Eagerly disconnect the Session here, to make release mode "on_close" work nicely. + session.disconnect(); } - // Eagerly disconnect the Session here, to make release mode "on_close" work nicely. - session.disconnect(); - - // Unbind at this point if it's a new Session... - if (this.newSession) { - TransactionSynchronizationManager.unbindResource(this.sessionFactory); - this.holderActive = false; + finally { + // Unbind at this point if it's a new Session... + if (this.newSession) { + TransactionSynchronizationManager.unbindResource(this.sessionFactory); + this.holderActive = false; + } } }