@@ -194,16 +194,28 @@ public void manualReconnect(Connection suppliedConnection) {
194194 }
195195
196196 private void releaseConnection () {
197- //Some managed containers might trigger this release concurrently:
198- //this is not how they should do things, still we make a local
199- //copy of the variable to prevent confusing errors due to a race conditions
200- //(to trigger a more clear error, if any).
201197 final Connection localVariableConnection = this .physicalConnection ;
202198 if ( localVariableConnection == null ) {
203199 return ;
204200 }
205201
202+ // We need to set the connection to null before we release resources,
203+ // in order to prevent recursion into this method.
204+ // Recursion can happen when we release resources and when batch statements are in progress:
205+ // when releasing resources, we'll abort the batch statement,
206+ // which will trigger "logicalConnection.afterStatement()",
207+ // which in some configurations will release the connection.
208+
209+ //Some managed containers might trigger this release concurrently:
210+ //this is not how they should do things, still we try to detect it to trigger a more clear error.
211+ boolean concurrentUsageDetected = ( this .physicalConnection == null );
212+ this .physicalConnection = null ;
213+ if ( concurrentUsageDetected ) {
214+ throw new HibernateException ( "Detected concurrent management of connection resources." +
215+ " This might indicate a multi-threaded use of Hibernate in combination with managed resources, which is not supported." );
216+ }
206217 try {
218+ getResourceRegistry ().releaseResources ();
207219 if ( ! localVariableConnection .isClosed () ) {
208220 sqlExceptionHelper .logAndClearWarnings ( localVariableConnection );
209221 }
@@ -214,13 +226,6 @@ private void releaseConnection() {
214226 }
215227 finally {
216228 observer .jdbcConnectionReleaseEnd ();
217- boolean concurrentUsageDetected = ( this .physicalConnection == null );
218- this .physicalConnection = null ;
219- getResourceRegistry ().releaseResources ();
220- if ( concurrentUsageDetected ) {
221- throw new HibernateException ( "Detected concurrent management of connection resources." +
222- " This might indicate a multi-threaded use of Hibernate in combination with managed resources, which is not supported." );
223- }
224229 }
225230 }
226231
0 commit comments