Skip to content

Commit 55de4a6

Browse files
committed
SpringSessionSynchronization's beforeCompletion unbinds after disconnect failure as well
Issue: SPR-13089 (cherry picked from commit 8c337a4)
1 parent bf38b3a commit 55de4a6

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

spring-orm-hibernate4/src/main/java/org/springframework/orm/hibernate4/SpringSessionSynchronization.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -112,18 +112,21 @@ public void beforeCommit(boolean readOnly) throws DataAccessException {
112112

113113
@Override
114114
public void beforeCompletion() {
115-
Session session = this.sessionHolder.getSession();
116-
if (this.sessionHolder.getPreviousFlushMode() != null) {
117-
// In case of pre-bound Session, restore previous flush mode.
118-
session.setFlushMode(this.sessionHolder.getPreviousFlushMode());
115+
try {
116+
Session session = this.sessionHolder.getSession();
117+
if (this.sessionHolder.getPreviousFlushMode() != null) {
118+
// In case of pre-bound Session, restore previous flush mode.
119+
session.setFlushMode(this.sessionHolder.getPreviousFlushMode());
120+
}
121+
// Eagerly disconnect the Session here, to make release mode "on_close" work nicely.
122+
session.disconnect();
119123
}
120-
// Eagerly disconnect the Session here, to make release mode "on_close" work nicely.
121-
session.disconnect();
122-
123-
// Unbind at this point if it's a new Session...
124-
if (this.newSession) {
125-
TransactionSynchronizationManager.unbindResource(this.sessionFactory);
126-
this.holderActive = false;
124+
finally {
125+
// Unbind at this point if it's a new Session...
126+
if (this.newSession) {
127+
TransactionSynchronizationManager.unbindResource(this.sessionFactory);
128+
this.holderActive = false;
129+
}
127130
}
128131
}
129132

0 commit comments

Comments
 (0)