1414import org .hibernate .engine .jdbc .connections .spi .JdbcConnectionAccess ;
1515import org .hibernate .engine .jdbc .spi .JdbcServices ;
1616import org .hibernate .engine .jdbc .spi .SqlExceptionHelper ;
17+ import org .hibernate .internal .CoreLogging ;
18+ import org .hibernate .internal .CoreMessageLogger ;
1719import org .hibernate .resource .jdbc .LogicalConnection ;
1820import org .hibernate .resource .jdbc .ResourceRegistry ;
1921import org .hibernate .resource .jdbc .spi .JdbcEventHandler ;
2022import org .hibernate .resource .jdbc .spi .JdbcSessionContext ;
2123import org .hibernate .resource .jdbc .spi .PhysicalConnectionHandlingMode ;
2224
23- import org .jboss .logging .Logger ;
24-
2525import static org .hibernate .ConnectionAcquisitionMode .IMMEDIATELY ;
2626import static org .hibernate .ConnectionReleaseMode .AFTER_STATEMENT ;
2727import static org .hibernate .ConnectionReleaseMode .BEFORE_TRANSACTION_COMPLETION ;
3737 * @author Steve Ebersole
3838 */
3939public class LogicalConnectionManagedImpl extends AbstractLogicalConnectionImplementor {
40- private static final Logger log = Logger . getLogger ( LogicalConnectionManagedImpl .class );
40+ private static final CoreMessageLogger log = CoreLogging . messageLogger ( LogicalConnectionManagedImpl .class );
4141
4242 private final transient JdbcConnectionAccess jdbcConnectionAccess ;
4343 private final transient JdbcEventHandler jdbcEventHandler ;
@@ -72,13 +72,7 @@ public LogicalConnectionManagedImpl(
7272
7373 this .providerDisablesAutoCommit = jdbcSessionContext .doesConnectionProviderDisableAutoCommit ();
7474 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 ();
8276 }
8377 }
8478
@@ -98,12 +92,10 @@ public LogicalConnectionManagedImpl(
9892 private PhysicalConnectionHandlingMode determineConnectionHandlingMode (
9993 PhysicalConnectionHandlingMode connectionHandlingMode ,
10094 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 ;
10799 }
108100
109101 private LogicalConnectionManagedImpl (
@@ -159,7 +151,6 @@ public Connection getPhysicalConnection() {
159151 @ Override
160152 public void afterStatement () {
161153 super .afterStatement ();
162-
163154 if ( connectionHandlingMode .getReleaseMode () == AFTER_STATEMENT ) {
164155 if ( getResourceRegistry ().hasRegisteredResources () ) {
165156 log .debug ( "Skipping aggressive release of JDBC Connection after-statement due to held resources" );
@@ -183,7 +174,6 @@ public void beforeTransactionCompletion() {
183174 @ Override
184175 public void afterTransaction () {
185176 super .afterTransaction ();
186-
187177 if ( connectionHandlingMode .getReleaseMode () != ON_CLOSE ) {
188178 // NOTE : we check for !ON_CLOSE here (rather than AFTER_TRANSACTION) to also catch:
189179 // - AFTER_STATEMENT cases that were circumvented due to held resources
@@ -199,51 +189,48 @@ public Connection manualDisconnect() {
199189 if ( closed ) {
200190 throw new ResourceClosedException ( "Logical connection is closed" );
201191 }
202- final Connection c = physicalConnection ;
192+ final Connection connection = physicalConnection ;
203193 releaseConnection ();
204- return c ;
194+ return connection ;
205195 }
206196
207197 @ Override
208198 public void manualReconnect (Connection suppliedConnection ) {
209199 if ( closed ) {
210200 throw new ResourceClosedException ( "Logical connection is closed" );
211201 }
212-
213202 throw new IllegalStateException ( "Cannot manually reconnect unless Connection was originally supplied by user" );
214203 }
215204
216205 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 ;
230215 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 );
234225 }
235226 }
227+ catch (SQLException e ) {
228+ throw sqlExceptionHelper .convert ( e , "Unable to release JDBC Connection" );
229+ }
236230 finally {
237- jdbcEventHandler .jdbcConnectionReleaseStart ();
238- jdbcConnectionAccess .releaseConnection ( localVariableConnection );
231+ jdbcEventHandler .jdbcConnectionReleaseEnd ();
239232 }
240233 }
241- catch (SQLException e ) {
242- throw sqlExceptionHelper .convert ( e , "Unable to release JDBC Connection" );
243- }
244- finally {
245- jdbcEventHandler .jdbcConnectionReleaseEnd ();
246- }
247234 }
248235
249236 @ Override
@@ -261,20 +248,17 @@ public static LogicalConnectionManagedImpl deserialize(
261248
262249 @ Override
263250 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+ }
278262 }
279263 return null ;
280264 }
@@ -300,7 +284,6 @@ public void begin() {
300284 protected void afterCompletion () {
301285 resetConnection ( initiallyAutoCommit );
302286 initiallyAutoCommit = false ;
303-
304287 afterTransaction ();
305288 }
306289
0 commit comments