Skip to content

Commit f314ead

Browse files
committed
minor cleanups and logging improvements related to logical connections
1 parent 6c62b81 commit f314ead

File tree

4 files changed

+83
-83
lines changed

4 files changed

+83
-83
lines changed

hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@
3333
import jakarta.transaction.Synchronization;
3434
import org.checkerframework.checker.nullness.qual.Nullable;
3535

36+
import static org.hibernate.cfg.JdbcSettings.CONNECTION_PROVIDER_DISABLES_AUTOCOMMIT;
37+
import static org.hibernate.cfg.ValidationSettings.JAKARTA_VALIDATION_MODE;
3638
import static org.jboss.logging.Logger.Level.DEBUG;
3739
import static org.jboss.logging.Logger.Level.ERROR;
3840
import static org.jboss.logging.Logger.Level.INFO;
41+
import static org.jboss.logging.Logger.Level.TRACE;
3942
import static org.jboss.logging.Logger.Level.WARN;
4043

4144
/**
@@ -562,7 +565,7 @@ void cannotResolveNonNullableTransientDependencies(
562565
void explicitSkipLockedLockCombo();
563566

564567
@LogMessage(level = INFO)
565-
@Message(value = "'jakarta.persistence.validation.mode' named multiple values: %s", id = 448)
568+
@Message(value = "'" + JAKARTA_VALIDATION_MODE + "' named multiple values: %s", id = 448)
566569
void multipleValidationModes(String modes);
567570

568571
@LogMessage(level = WARN)
@@ -722,22 +725,22 @@ void cannotResolveNonNullableTransientDependencies(
722725
void flushAndEvictOnRemove(String entityName);
723726

724727
@LogMessage(level = ERROR)
725-
@Message(value = "Illegal argument on static metamodel field injection : %s#%s; expected type : %s; encountered type : %s", id = 15007)
728+
@Message(value = "Illegal argument on static metamodel field injection: %s#%s; expected type: %s; encountered type: %s", id = 15007)
726729
void illegalArgumentOnStaticMetamodelFieldInjection(
727730
String name,
728731
String name2,
729732
String name3,
730733
String name4);
731734

732735
@LogMessage(level = WARN)
733-
@Message(value = "Unable to locate static metamodel field : %s#%s; this may or may not indicate a problem with the static metamodel", id = 15011)
736+
@Message(value = "Unable to locate static metamodel field: %s#%s; this may or may not indicate a problem with the static metamodel", id = 15011)
734737
void unableToLocateStaticMetamodelField(
735738
String name,
736739
String name2);
737740

738741
@LogMessage(level = DEBUG)
739-
@Message(value = "Returning null (as required by JPA spec) rather than throwing EntityNotFoundException, " +
740-
"as the entity (type=%s, id=%s) does not exist", id = 15013)
742+
@Message(value = "Returning null (as required by JPA spec) rather than throwing EntityNotFoundException " +
743+
"since the entity of type '%s' with id [%s] does not exist", id = 15013)
741744
void ignoringEntityNotFound(String entityName, String identifier);
742745

743746
@LogMessage(level = DEBUG)
@@ -754,6 +757,27 @@ void unableToLocateStaticMetamodelField(
754757
)
755758
void duplicatedPersistenceUnitName(String name);
756759

760+
@LogMessage(level = DEBUG)
761+
@Message(
762+
id = 455,
763+
value =
764+
"'" + CONNECTION_PROVIDER_DISABLES_AUTOCOMMIT + "' " +
765+
"""
766+
was enabled. This setting should only be enabled when JDBC Connections obtained by Hibernate \
767+
from the ConnectionProvider have auto-commit disabled. Enabling this setting when connections \
768+
have auto-commit enabled leads to execution of SQL operations outside of any JDBC transaction.\
769+
"""
770+
)
771+
void connectionProviderDisablesAutoCommitEnabled();
772+
773+
@LogMessage(level = TRACE)
774+
@Message(value = "Closing logical connection", id = 456)
775+
void closingLogicalConnection();
776+
777+
@LogMessage(level = TRACE)
778+
@Message(value = "Logical connection closed", id = 457)
779+
void logicalConnectionClosed();
780+
757781
@LogMessage(level = DEBUG)
758782
@Message(
759783
id = 401,

hibernate-core/src/main/java/org/hibernate/resource/jdbc/internal/LogicalConnectionManagedImpl.java

Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
1515
import org.hibernate.engine.jdbc.spi.JdbcServices;
1616
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
17+
import org.hibernate.internal.CoreLogging;
18+
import org.hibernate.internal.CoreMessageLogger;
1719
import org.hibernate.resource.jdbc.LogicalConnection;
1820
import org.hibernate.resource.jdbc.ResourceRegistry;
1921
import org.hibernate.resource.jdbc.spi.JdbcEventHandler;
2022
import org.hibernate.resource.jdbc.spi.JdbcSessionContext;
2123
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
2224

23-
import org.jboss.logging.Logger;
24-
2525
import static org.hibernate.ConnectionAcquisitionMode.IMMEDIATELY;
2626
import static org.hibernate.ConnectionReleaseMode.AFTER_STATEMENT;
2727
import static org.hibernate.ConnectionReleaseMode.BEFORE_TRANSACTION_COMPLETION;
@@ -37,7 +37,7 @@
3737
* @author Steve Ebersole
3838
*/
3939
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 );
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

hibernate-core/src/main/java/org/hibernate/resource/jdbc/internal/LogicalConnectionProvidedImpl.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@
99
import java.io.ObjectOutputStream;
1010
import java.sql.Connection;
1111

12+
import org.hibernate.internal.CoreLogging;
13+
import org.hibernate.internal.CoreMessageLogger;
1214
import org.hibernate.resource.jdbc.LogicalConnection;
1315
import org.hibernate.resource.jdbc.ResourceRegistry;
1416
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
1517

16-
import org.jboss.logging.Logger;
18+
import static org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode.IMMEDIATE_ACQUISITION_AND_HOLD;
1719

1820
/**
1921
* @author Steve Ebersole
2022
*/
2123
public class LogicalConnectionProvidedImpl extends AbstractLogicalConnectionImplementor {
22-
private static final Logger log = Logger.getLogger( LogicalConnection.class );
24+
private static final CoreMessageLogger log = CoreLogging.messageLogger( LogicalConnection.class );
2325

2426
private transient Connection providedConnection;
2527
private final boolean initiallyAutoCommit;
@@ -30,7 +32,6 @@ public LogicalConnectionProvidedImpl(Connection providedConnection, ResourceRegi
3032
if ( providedConnection == null ) {
3133
throw new IllegalArgumentException( "Provided Connection cannot be null" );
3234
}
33-
3435
this.providedConnection = providedConnection;
3536
this.initiallyAutoCommit = determineInitialAutoCommitMode( providedConnection );
3637
}
@@ -43,7 +44,7 @@ private LogicalConnectionProvidedImpl(boolean closed, boolean initiallyAutoCommi
4344

4445
@Override
4546
public PhysicalConnectionHandlingMode getConnectionHandlingMode() {
46-
return PhysicalConnectionHandlingMode.IMMEDIATE_ACQUISITION_AND_HOLD;
47+
return IMMEDIATE_ACQUISITION_AND_HOLD;
4748
}
4849

4950
@Override
@@ -53,17 +54,15 @@ public boolean isOpen() {
5354

5455
@Override
5556
public Connection close() {
56-
log.trace( "Closing logical connection" );
57-
57+
log.closingLogicalConnection();
5858
getResourceRegistry().releaseResources();
59-
6059
try {
6160
return providedConnection;
6261
}
6362
finally {
6463
providedConnection = null;
6564
closed = true;
66-
log.trace( "Logical connection closed" );
65+
log.logicalConnectionClosed();
6766
}
6867
}
6968

@@ -99,14 +98,13 @@ public Connection manualDisconnect() {
9998
return providedConnection;
10099
}
101100
finally {
102-
this.providedConnection = null;
101+
providedConnection = null;
103102
}
104103
}
105104

106105
@Override
107106
public void manualReconnect(Connection connection) {
108107
errorIfClosed();
109-
110108
if ( connection == null ) {
111109
throw new IllegalArgumentException( "cannot reconnect using a null connection" );
112110
}
@@ -131,7 +129,6 @@ protected Connection getConnectionForTransactionManagement() {
131129
@Override
132130
protected void afterCompletion() {
133131
afterTransaction();
134-
135132
resetConnection( initiallyAutoCommit );
136133
}
137134
}

tooling/hibernate-ant/src/main/java/org/hibernate/tool/hbm2ddl/ConnectionHelper.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,18 @@ public interface ConnectionHelper {
2222
*
2323
* @param needsAutoCommit Should connection be forced to auto-commit
2424
* if not already.
25-
* @throws SQLException
2625
*/
27-
public void prepare(boolean needsAutoCommit) throws SQLException;
26+
void prepare(boolean needsAutoCommit) throws SQLException;
2827

2928
/**
3029
* Get a reference to the connection we are using.
3130
*
3231
* @return The JDBC connection.
33-
* @throws SQLException
3432
*/
35-
public Connection getConnection() throws SQLException;
33+
Connection getConnection() throws SQLException;
3634

3735
/**
3836
* Release any resources held by this helper.
39-
*
40-
* @throws SQLException
4137
*/
42-
public void release() throws SQLException;
38+
void release() throws SQLException;
4339
}

0 commit comments

Comments
 (0)