Skip to content

Commit af6bfbf

Browse files
committed
HHH-19559 move management of the schema into LogicalConnectionManagedImpl
as requested by @sebersole
1 parent c99e2ee commit af6bfbf

File tree

2 files changed

+51
-104
lines changed

2 files changed

+51
-104
lines changed

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

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.hibernate.engine.spi.SharedSessionContractImplementor;
1616
import org.hibernate.event.monitor.spi.EventMonitor;
1717
import org.hibernate.event.monitor.spi.DiagnosticEvent;
18-
import org.jboss.logging.Logger;
1918

2019
/**
2120
* @author Steve Ebersole
@@ -25,8 +24,6 @@ public class NonContextualJdbcConnectionAccess implements JdbcConnectionAccess,
2524
private final ConnectionProvider connectionProvider;
2625
private final SharedSessionContractImplementor session;
2726

28-
private static final Logger log = Logger.getLogger( NonContextualJdbcConnectionAccess.class );
29-
3027
public NonContextualJdbcConnectionAccess(
3128
SessionEventListener listener,
3229
ConnectionProvider connectionProvider,
@@ -42,40 +39,18 @@ public NonContextualJdbcConnectionAccess(
4239
public Connection obtainConnection() throws SQLException {
4340
final EventMonitor eventMonitor = session.getEventMonitor();
4441
final DiagnosticEvent connectionAcquisitionEvent = eventMonitor.beginJdbcConnectionAcquisitionEvent();
45-
final Connection connection;
4642
try {
4743
listener.jdbcConnectionAcquisitionStart();
48-
connection = connectionProvider.getConnection();
44+
return connectionProvider.getConnection();
4945
}
5046
finally {
5147
eventMonitor.completeJdbcConnectionAcquisitionEvent( connectionAcquisitionEvent, session, null );
5248
listener.jdbcConnectionAcquisitionEnd();
5349
}
54-
55-
try {
56-
session.afterObtainConnection( connection );
57-
}
58-
catch (SQLException e) {
59-
try {
60-
releaseConnection( connection );
61-
}
62-
catch (SQLException re) {
63-
e.addSuppressed( re );
64-
}
65-
throw e;
66-
}
67-
return connection;
6850
}
6951

7052
@Override
7153
public void releaseConnection(Connection connection) throws SQLException {
72-
try {
73-
session.beforeReleaseConnection( connection );
74-
}
75-
catch (SQLException e) {
76-
log.warn( "Error before releasing JDBC connection", e );
77-
}
78-
7954
final EventMonitor eventMonitor = session.getEventMonitor();
8055
final DiagnosticEvent connectionReleaseEvent = eventMonitor.beginJdbcConnectionReleaseEvent();
8156
try {

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

Lines changed: 50 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@
1111
import java.sql.SQLException;
1212

1313
import org.hibernate.ResourceClosedException;
14-
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
15-
import org.hibernate.engine.jdbc.spi.JdbcServices;
16-
import org.hibernate.engine.jdbc.spi.SqlExceptionHelper;
1714
import org.hibernate.internal.CoreLogging;
1815
import org.hibernate.internal.CoreMessageLogger;
1916
import org.hibernate.resource.jdbc.LogicalConnection;
2017
import org.hibernate.resource.jdbc.ResourceRegistry;
2118
import org.hibernate.resource.jdbc.spi.JdbcEventHandler;
22-
import org.hibernate.resource.jdbc.spi.JdbcSessionContext;
2319
import org.hibernate.resource.jdbc.spi.JdbcSessionOwner;
2420
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
2521

@@ -40,97 +36,72 @@
4036
public class LogicalConnectionManagedImpl extends AbstractLogicalConnectionImplementor {
4137
private static final CoreMessageLogger log = CoreLogging.messageLogger( LogicalConnectionManagedImpl.class );
4238

43-
private final transient JdbcConnectionAccess jdbcConnectionAccess;
44-
private final transient JdbcEventHandler jdbcEventHandler;
45-
private final transient SqlExceptionHelper sqlExceptionHelper;
46-
39+
private final transient JdbcSessionOwner jdbcSessionOwner;
4740
private final transient PhysicalConnectionHandlingMode connectionHandlingMode;
4841

4942
private transient Connection physicalConnection;
5043
private boolean closed;
5144

52-
private final boolean providerDisablesAutoCommit;
53-
54-
public LogicalConnectionManagedImpl(
55-
JdbcConnectionAccess jdbcConnectionAccess,
56-
JdbcSessionContext jdbcSessionContext,
57-
SqlExceptionHelper sqlExceptionHelper,
58-
ResourceRegistry resourceRegistry) {
59-
this.jdbcConnectionAccess = jdbcConnectionAccess;
60-
this.sqlExceptionHelper = sqlExceptionHelper;
45+
public LogicalConnectionManagedImpl(JdbcSessionOwner sessionOwner, ResourceRegistry resourceRegistry) {
46+
this.jdbcSessionOwner = sessionOwner;
6147
this.resourceRegistry = resourceRegistry;
62-
jdbcEventHandler = jdbcSessionContext.getEventHandler();
6348

64-
connectionHandlingMode = determineConnectionHandlingMode( jdbcSessionContext, jdbcConnectionAccess );
49+
connectionHandlingMode = determineConnectionHandlingMode( sessionOwner );
6550
if ( connectionHandlingMode.getAcquisitionMode() == IMMEDIATELY ) {
6651
//noinspection resource
6752
acquireConnectionIfNeeded();
6853
}
6954

70-
providerDisablesAutoCommit = jdbcSessionContext.doesConnectionProviderDisableAutoCommit();
71-
if ( providerDisablesAutoCommit ) {
55+
if ( sessionOwner.getJdbcSessionContext().doesConnectionProviderDisableAutoCommit() ) {
7256
log.connectionProviderDisablesAutoCommitEnabled();
7357
}
7458
}
7559

76-
public LogicalConnectionManagedImpl(
77-
JdbcConnectionAccess jdbcConnectionAccess,
78-
JdbcSessionContext jdbcSessionContext,
79-
ResourceRegistry resourceRegistry,
80-
JdbcServices jdbcServices) {
81-
this(
82-
jdbcConnectionAccess,
83-
jdbcSessionContext,
84-
jdbcServices.getSqlExceptionHelper(),
85-
resourceRegistry
86-
);
87-
}
88-
89-
public LogicalConnectionManagedImpl(JdbcSessionOwner owner, ResourceRegistry resourceRegistry) {
90-
this(
91-
owner.getJdbcConnectionAccess(),
92-
owner.getJdbcSessionContext(),
93-
owner.getSqlExceptionHelper(),
94-
resourceRegistry
95-
);
96-
}
97-
98-
private PhysicalConnectionHandlingMode determineConnectionHandlingMode(
99-
JdbcSessionContext jdbcSessionContext,
100-
JdbcConnectionAccess jdbcConnectionAccess) {
101-
final var connectionHandlingMode = jdbcSessionContext.getPhysicalConnectionHandlingMode();
60+
private PhysicalConnectionHandlingMode determineConnectionHandlingMode(JdbcSessionOwner sessionOwner) {
61+
final var connectionHandlingMode = sessionOwner.getJdbcSessionContext().getPhysicalConnectionHandlingMode();
10262
return connectionHandlingMode.getReleaseMode() == AFTER_STATEMENT
103-
&& !jdbcConnectionAccess.supportsAggressiveRelease()
63+
&& !sessionOwner.getJdbcConnectionAccess().supportsAggressiveRelease()
10464
? DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION
10565
: connectionHandlingMode;
10666
}
10767

108-
private LogicalConnectionManagedImpl(
109-
JdbcConnectionAccess jdbcConnectionAccess,
110-
JdbcSessionContext jdbcSessionContext,
111-
boolean closed) {
112-
this(
113-
jdbcConnectionAccess,
114-
jdbcSessionContext,
115-
new ResourceRegistryStandardImpl(),
116-
jdbcSessionContext.getJdbcServices()
117-
);
68+
private LogicalConnectionManagedImpl(JdbcSessionOwner owner, boolean closed) {
69+
this( owner, new ResourceRegistryStandardImpl() );
11870
this.closed = closed;
11971
}
12072

12173
private Connection acquireConnectionIfNeeded() {
12274
if ( physicalConnection == null ) {
123-
jdbcEventHandler.jdbcConnectionAcquisitionStart();
75+
final JdbcEventHandler eventHandler = jdbcSessionOwner.getJdbcSessionContext().getEventHandler();
76+
eventHandler.jdbcConnectionAcquisitionStart();
12477
try {
125-
physicalConnection = jdbcConnectionAccess.obtainConnection();
78+
physicalConnection = jdbcSessionOwner.getJdbcConnectionAccess().obtainConnection();
12679
}
12780
catch ( SQLException e ) {
128-
throw sqlExceptionHelper.convert( e, "Unable to acquire JDBC Connection" );
81+
throw jdbcSessionOwner.getSqlExceptionHelper()
82+
.convert( e, "Unable to acquire JDBC Connection" );
12983
}
13084
finally {
131-
jdbcEventHandler.jdbcConnectionAcquisitionEnd( physicalConnection );
85+
eventHandler.jdbcConnectionAcquisitionEnd( physicalConnection );
86+
}
87+
}
88+
89+
90+
try {
91+
jdbcSessionOwner.afterObtainConnection( physicalConnection );
92+
}
93+
catch (SQLException e) {
94+
try {
95+
// given the session a chance to set the schema
96+
jdbcSessionOwner.getJdbcConnectionAccess().releaseConnection( physicalConnection );
97+
}
98+
catch (SQLException re) {
99+
e.addSuppressed( re );
132100
}
101+
throw jdbcSessionOwner.getSqlExceptionHelper()
102+
.convert( e, "Error after acquiring JDBC Connection" );
133103
}
104+
134105
return physicalConnection;
135106
}
136107

@@ -210,8 +181,17 @@ public void manualReconnect(Connection suppliedConnection) {
210181
}
211182

212183
private void releaseConnection() {
184+
try {
185+
// give the session a chance to change the schema back to null
186+
jdbcSessionOwner.beforeReleaseConnection( physicalConnection );
187+
}
188+
catch (SQLException e) {
189+
log.warn( "Error before releasing JDBC connection", e );
190+
}
191+
213192
final Connection localVariableConnection = physicalConnection;
214193
if ( localVariableConnection != null ) {
194+
final JdbcEventHandler eventHandler = jdbcSessionOwner.getJdbcSessionContext().getEventHandler();
215195
// We need to set the connection to null before we release resources,
216196
// in order to prevent recursion into this method.
217197
// Recursion can happen when we release resources and when batch statements are in progress:
@@ -223,19 +203,20 @@ private void releaseConnection() {
223203
try {
224204
getResourceRegistry().releaseResources();
225205
if ( !localVariableConnection.isClosed() ) {
226-
sqlExceptionHelper.logAndClearWarnings( localVariableConnection );
206+
jdbcSessionOwner.getSqlExceptionHelper().logAndClearWarnings( localVariableConnection );
227207
}
228208
}
229209
finally {
230-
jdbcEventHandler.jdbcConnectionReleaseStart();
231-
jdbcConnectionAccess.releaseConnection( localVariableConnection );
210+
eventHandler.jdbcConnectionReleaseStart();
211+
jdbcSessionOwner.getJdbcConnectionAccess().releaseConnection( localVariableConnection );
232212
}
233213
}
234214
catch (SQLException e) {
235-
throw sqlExceptionHelper.convert( e, "Unable to release JDBC Connection" );
215+
throw jdbcSessionOwner.getSqlExceptionHelper()
216+
.convert( e, "Unable to release JDBC Connection" );
236217
}
237218
finally {
238-
jdbcEventHandler.jdbcConnectionReleaseEnd();
219+
eventHandler.jdbcConnectionReleaseEnd();
239220
}
240221
}
241222
}
@@ -245,18 +226,9 @@ public void serialize(ObjectOutputStream oos) throws IOException {
245226
oos.writeBoolean( closed );
246227
}
247228

248-
public static LogicalConnectionManagedImpl deserialize(
249-
ObjectInputStream ois,
250-
JdbcConnectionAccess jdbcConnectionAccess,
251-
JdbcSessionContext jdbcSessionContext)
252-
throws IOException {
253-
final boolean isClosed = ois.readBoolean();
254-
return new LogicalConnectionManagedImpl( jdbcConnectionAccess, jdbcSessionContext, isClosed );
255-
}
256-
257229
public static LogicalConnectionManagedImpl deserialize(ObjectInputStream ois, JdbcSessionOwner owner)
258230
throws IOException {
259-
return deserialize( ois, owner.getJdbcConnectionAccess(), owner.getJdbcSessionContext() );
231+
return new LogicalConnectionManagedImpl( owner, ois.readBoolean() );
260232
}
261233

262234
@Override
@@ -302,6 +274,6 @@ protected void afterCompletion() {
302274

303275
@Override
304276
protected boolean doConnectionsFromProviderHaveAutoCommitDisabled() {
305-
return providerDisablesAutoCommit;
277+
return jdbcSessionOwner.getJdbcSessionContext().doesConnectionProviderDisableAutoCommit();
306278
}
307279
}

0 commit comments

Comments
 (0)