22
22
import javax .persistence .EntityManager ;
23
23
import javax .persistence .PersistenceException ;
24
24
25
+ import org .apache .commons .logging .LogFactory ;
25
26
import org .hibernate .FlushMode ;
26
27
import org .hibernate .HibernateException ;
27
28
import org .hibernate .NonUniqueObjectException ;
@@ -146,13 +147,12 @@ public Object beginTransaction(EntityManager entityManager, TransactionDefinitio
146
147
147
148
boolean isolationLevelNeeded = (definition .getIsolationLevel () != TransactionDefinition .ISOLATION_DEFAULT );
148
149
Integer previousIsolationLevel = null ;
149
- boolean resetConnection = false ;
150
+ Connection preparedCon = null ;
150
151
151
152
if (isolationLevelNeeded || definition .isReadOnly ()) {
152
153
if (this .prepareConnection ) {
153
- Connection con = HibernateConnectionHandle .doGetConnection (session );
154
- previousIsolationLevel = DataSourceUtils .prepareConnectionForTransaction (con , definition );
155
- resetConnection = true ;
154
+ preparedCon = HibernateConnectionHandle .doGetConnection (session );
155
+ previousIsolationLevel = DataSourceUtils .prepareConnectionForTransaction (preparedCon , definition );
156
156
}
157
157
else if (isolationLevelNeeded ) {
158
158
throw new InvalidIsolationLevelException (getClass ().getSimpleName () +
@@ -167,7 +167,7 @@ else if (isolationLevelNeeded) {
167
167
168
168
// Adapt flush mode and store previous isolation level, if any.
169
169
FlushMode previousFlushMode = prepareFlushMode (session , definition .isReadOnly ());
170
- return new SessionTransactionData (session , previousFlushMode , resetConnection , previousIsolationLevel );
170
+ return new SessionTransactionData (session , previousFlushMode , preparedCon , previousIsolationLevel );
171
171
}
172
172
173
173
@ Override
@@ -176,7 +176,7 @@ public Object prepareTransaction(EntityManager entityManager, boolean readOnly,
176
176
177
177
Session session = getSession (entityManager );
178
178
FlushMode previousFlushMode = prepareFlushMode (session , readOnly );
179
- return new SessionTransactionData (session , previousFlushMode , false , null );
179
+ return new SessionTransactionData (session , previousFlushMode , null , null );
180
180
}
181
181
182
182
protected FlushMode prepareFlushMode (Session session , boolean readOnly ) throws PersistenceException {
@@ -321,25 +321,31 @@ private static class SessionTransactionData {
321
321
322
322
private final FlushMode previousFlushMode ;
323
323
324
- private final boolean resetConnection ;
324
+ private final Connection preparedCon ;
325
325
326
326
private final Integer previousIsolationLevel ;
327
327
328
328
public SessionTransactionData (
329
- Session session , FlushMode previousFlushMode , boolean resetConnection , Integer previousIsolationLevel ) {
329
+ Session session , FlushMode previousFlushMode , Connection preparedCon , Integer previousIsolationLevel ) {
330
330
this .session = session ;
331
331
this .previousFlushMode = previousFlushMode ;
332
- this .resetConnection = resetConnection ;
332
+ this .preparedCon = preparedCon ;
333
333
this .previousIsolationLevel = previousIsolationLevel ;
334
334
}
335
335
336
336
public void resetSessionState () {
337
337
if (this .previousFlushMode != null ) {
338
338
this .session .setFlushMode (this .previousFlushMode );
339
339
}
340
- if (this .resetConnection && this .session .isConnected ()) {
341
- Connection con = HibernateConnectionHandle .doGetConnection (this .session );
342
- DataSourceUtils .resetConnectionAfterTransaction (con , this .previousIsolationLevel );
340
+ if (this .preparedCon != null && this .session .isConnected ()) {
341
+ Connection conToReset = HibernateConnectionHandle .doGetConnection (this .session );
342
+ if (conToReset != this .preparedCon ) {
343
+ LogFactory .getLog (HibernateJpaDialect .class ).warn (
344
+ "JDBC Connection to reset not identical to originally prepared Connection - please " +
345
+ "make sure to use connection release mode ON_CLOSE (the default) and to run against " +
346
+ "Hibernate 4.2+ (or switch HibernateJpaDialect's prepareConnection flag to false" );
347
+ }
348
+ DataSourceUtils .resetConnectionAfterTransaction (conToReset , this .previousIsolationLevel );
343
349
}
344
350
}
345
351
}
0 commit comments