14
14
import org .hibernate .engine .jdbc .connections .spi .JdbcConnectionAccess ;
15
15
import org .hibernate .engine .jdbc .spi .JdbcServices ;
16
16
import org .hibernate .engine .jdbc .spi .SqlExceptionHelper ;
17
+ import org .hibernate .internal .CoreLogging ;
18
+ import org .hibernate .internal .CoreMessageLogger ;
17
19
import org .hibernate .resource .jdbc .LogicalConnection ;
18
20
import org .hibernate .resource .jdbc .ResourceRegistry ;
19
21
import org .hibernate .resource .jdbc .spi .JdbcEventHandler ;
20
22
import org .hibernate .resource .jdbc .spi .JdbcSessionContext ;
21
23
import org .hibernate .resource .jdbc .spi .PhysicalConnectionHandlingMode ;
22
24
23
- import org .jboss .logging .Logger ;
24
-
25
25
import static org .hibernate .ConnectionAcquisitionMode .IMMEDIATELY ;
26
26
import static org .hibernate .ConnectionReleaseMode .AFTER_STATEMENT ;
27
27
import static org .hibernate .ConnectionReleaseMode .BEFORE_TRANSACTION_COMPLETION ;
37
37
* @author Steve Ebersole
38
38
*/
39
39
public class LogicalConnectionManagedImpl extends AbstractLogicalConnectionImplementor {
40
- private static final Logger log = Logger . getLogger ( LogicalConnectionManagedImpl .class );
40
+ private static final CoreMessageLogger log = CoreLogging . messageLogger ( LogicalConnectionManagedImpl .class );
41
41
42
42
private final transient JdbcConnectionAccess jdbcConnectionAccess ;
43
43
private final transient JdbcEventHandler jdbcEventHandler ;
@@ -72,13 +72,7 @@ public LogicalConnectionManagedImpl(
72
72
73
73
this .providerDisablesAutoCommit = jdbcSessionContext .doesConnectionProviderDisableAutoCommit ();
74
74
if ( providerDisablesAutoCommit ) {
75
- log .debug (
76
- "`hibernate.connection.provider_disables_autocommit` was enabled. This setting should only be " +
77
- "enabled when you are certain that the Connections given to Hibernate by the " +
78
- "ConnectionProvider have auto-commit disabled. Enabling this setting when the " +
79
- "Connections do not have auto-commit disabled will lead to Hibernate executing " +
80
- "SQL operations outside of any JDBC/SQL transaction."
81
- );
75
+ log .connectionProviderDisablesAutoCommitEnabled ();
82
76
}
83
77
}
84
78
@@ -98,12 +92,10 @@ public LogicalConnectionManagedImpl(
98
92
private PhysicalConnectionHandlingMode determineConnectionHandlingMode (
99
93
PhysicalConnectionHandlingMode connectionHandlingMode ,
100
94
JdbcConnectionAccess jdbcConnectionAccess ) {
101
- if ( connectionHandlingMode .getReleaseMode () == AFTER_STATEMENT
102
- && !jdbcConnectionAccess .supportsAggressiveRelease () ) {
103
- return DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION ;
104
- }
105
-
106
- return connectionHandlingMode ;
95
+ return connectionHandlingMode .getReleaseMode () == AFTER_STATEMENT
96
+ && !jdbcConnectionAccess .supportsAggressiveRelease ()
97
+ ? DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION
98
+ : connectionHandlingMode ;
107
99
}
108
100
109
101
private LogicalConnectionManagedImpl (
@@ -159,7 +151,6 @@ public Connection getPhysicalConnection() {
159
151
@ Override
160
152
public void afterStatement () {
161
153
super .afterStatement ();
162
-
163
154
if ( connectionHandlingMode .getReleaseMode () == AFTER_STATEMENT ) {
164
155
if ( getResourceRegistry ().hasRegisteredResources () ) {
165
156
log .debug ( "Skipping aggressive release of JDBC Connection after-statement due to held resources" );
@@ -183,7 +174,6 @@ public void beforeTransactionCompletion() {
183
174
@ Override
184
175
public void afterTransaction () {
185
176
super .afterTransaction ();
186
-
187
177
if ( connectionHandlingMode .getReleaseMode () != ON_CLOSE ) {
188
178
// NOTE : we check for !ON_CLOSE here (rather than AFTER_TRANSACTION) to also catch:
189
179
// - AFTER_STATEMENT cases that were circumvented due to held resources
@@ -199,51 +189,48 @@ public Connection manualDisconnect() {
199
189
if ( closed ) {
200
190
throw new ResourceClosedException ( "Logical connection is closed" );
201
191
}
202
- final Connection c = physicalConnection ;
192
+ final Connection connection = physicalConnection ;
203
193
releaseConnection ();
204
- return c ;
194
+ return connection ;
205
195
}
206
196
207
197
@ Override
208
198
public void manualReconnect (Connection suppliedConnection ) {
209
199
if ( closed ) {
210
200
throw new ResourceClosedException ( "Logical connection is closed" );
211
201
}
212
-
213
202
throw new IllegalStateException ( "Cannot manually reconnect unless Connection was originally supplied by user" );
214
203
}
215
204
216
205
private void releaseConnection () {
217
- final Connection localVariableConnection = this .physicalConnection ;
218
- if ( localVariableConnection == null ) {
219
- return ;
220
- }
221
-
222
- // We need to set the connection to null before we release resources,
223
- // in order to prevent recursion into this method.
224
- // Recursion can happen when we release resources and when batch statements are in progress:
225
- // when releasing resources, we'll abort the batch statement,
226
- // which will trigger "logicalConnection.afterStatement()",
227
- // which in some configurations will release the connection.
228
- this .physicalConnection = null ;
229
- try {
206
+ final Connection localVariableConnection = physicalConnection ;
207
+ if ( localVariableConnection != null ) {
208
+ // We need to set the connection to null before we release resources,
209
+ // in order to prevent recursion into this method.
210
+ // Recursion can happen when we release resources and when batch statements are in progress:
211
+ // when releasing resources, we'll abort the batch statement,
212
+ // which will trigger "logicalConnection.afterStatement()",
213
+ // which in some configurations will release the connection.
214
+ physicalConnection = null ;
230
215
try {
231
- getResourceRegistry ().releaseResources ();
232
- if ( !localVariableConnection .isClosed () ) {
233
- sqlExceptionHelper .logAndClearWarnings ( localVariableConnection );
216
+ try {
217
+ getResourceRegistry ().releaseResources ();
218
+ if ( !localVariableConnection .isClosed () ) {
219
+ sqlExceptionHelper .logAndClearWarnings ( localVariableConnection );
220
+ }
221
+ }
222
+ finally {
223
+ jdbcEventHandler .jdbcConnectionReleaseStart ();
224
+ jdbcConnectionAccess .releaseConnection ( localVariableConnection );
234
225
}
235
226
}
227
+ catch (SQLException e ) {
228
+ throw sqlExceptionHelper .convert ( e , "Unable to release JDBC Connection" );
229
+ }
236
230
finally {
237
- jdbcEventHandler .jdbcConnectionReleaseStart ();
238
- jdbcConnectionAccess .releaseConnection ( localVariableConnection );
231
+ jdbcEventHandler .jdbcConnectionReleaseEnd ();
239
232
}
240
233
}
241
- catch (SQLException e ) {
242
- throw sqlExceptionHelper .convert ( e , "Unable to release JDBC Connection" );
243
- }
244
- finally {
245
- jdbcEventHandler .jdbcConnectionReleaseEnd ();
246
- }
247
234
}
248
235
249
236
@ Override
@@ -261,20 +248,17 @@ public static LogicalConnectionManagedImpl deserialize(
261
248
262
249
@ Override
263
250
public Connection close () {
264
- if ( closed ) {
265
- return null ;
266
- }
267
-
268
- getResourceRegistry ().releaseResources ();
269
-
270
- log .trace ( "Closing logical connection" );
271
- try {
272
- releaseConnection ();
273
- }
274
- finally {
275
- // no matter what
276
- closed = true ;
277
- log .trace ( "Logical connection closed" );
251
+ if ( !closed ) {
252
+ getResourceRegistry ().releaseResources ();
253
+ log .closingLogicalConnection ();
254
+ try {
255
+ releaseConnection ();
256
+ }
257
+ finally {
258
+ // no matter what
259
+ closed = true ;
260
+ log .logicalConnectionClosed ();
261
+ }
278
262
}
279
263
return null ;
280
264
}
@@ -300,7 +284,6 @@ public void begin() {
300
284
protected void afterCompletion () {
301
285
resetConnection ( initiallyAutoCommit );
302
286
initiallyAutoCommit = false ;
303
-
304
287
afterTransaction ();
305
288
}
306
289
0 commit comments