1111import java .sql .SQLException ;
1212
1313import 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 ;
1714import org .hibernate .internal .CoreLogging ;
1815import org .hibernate .internal .CoreMessageLogger ;
1916import org .hibernate .resource .jdbc .LogicalConnection ;
2017import org .hibernate .resource .jdbc .ResourceRegistry ;
2118import org .hibernate .resource .jdbc .spi .JdbcEventHandler ;
22- import org .hibernate .resource .jdbc .spi .JdbcSessionContext ;
2319import org .hibernate .resource .jdbc .spi .JdbcSessionOwner ;
2420import org .hibernate .resource .jdbc .spi .PhysicalConnectionHandlingMode ;
2521
4036public 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