@@ -82,6 +82,13 @@ final class PooledConnection extends ConnectionDelegator {
8282 */
8383 private boolean failoverToReadOnly ;
8484 private boolean resetAutoCommit ;
85+ private boolean resetSchema ;
86+ private boolean resetCatalog ;
87+ private String currentSchema ;
88+ private String currentCatalog ;
89+ private final String originalSchema ;
90+ private final String originalCatalog ;
91+
8592 private long startUseTime ;
8693 private long lastUseTime ;
8794 /**
@@ -106,7 +113,7 @@ final class PooledConnection extends ConnectionDelegator {
106113 * close() will return the connection back to the pool , while
107114 * closeDestroy() will close() the underlining connection properly.
108115 */
109- PooledConnection (ConnectionPool pool , int uniqueId , Connection connection ) {
116+ PooledConnection (ConnectionPool pool , int uniqueId , Connection connection ) throws SQLException {
110117 super (connection );
111118 this .pool = pool ;
112119 this .connection = connection ;
@@ -115,6 +122,8 @@ final class PooledConnection extends ConnectionDelegator {
115122 this .maxStackTrace = pool .maxStackTraceSize ();
116123 this .creationTime = System .currentTimeMillis ();
117124 this .lastUseTime = creationTime ;
125+ this .currentSchema = this .originalSchema = connection .getSchema ();
126+ this .currentCatalog = this .originalCatalog = connection .getCatalog ();
118127 pool .inc ();
119128 }
120129
@@ -130,6 +139,8 @@ final class PooledConnection extends ConnectionDelegator {
130139 this .maxStackTrace = 0 ;
131140 this .creationTime = System .currentTimeMillis ();
132141 this .lastUseTime = creationTime ;
142+ this .currentSchema = this .originalSchema = "DEFAULT" ;
143+ this .currentCatalog = this .originalCatalog = "DEFAULT" ;
133144 }
134145
135146 /**
@@ -272,7 +283,7 @@ void returnPreparedStatement(ExtendedPreparedStatement pstmt) {
272283 */
273284 @ Override
274285 public PreparedStatement prepareStatement (String sql , int returnKeysFlag ) throws SQLException {
275- String key = sql + ':' + currentSchema + ':' + returnKeysFlag ;
286+ String key = sql + ':' + currentSchema + ':' + currentCatalog + ':' + returnKeysFlag ;
276287 return prepareStatement (sql , true , returnKeysFlag , key );
277288 }
278289
@@ -281,7 +292,7 @@ public PreparedStatement prepareStatement(String sql, int returnKeysFlag) throws
281292 */
282293 @ Override
283294 public PreparedStatement prepareStatement (String sql ) throws SQLException {
284- String key = sql + ':' + currentSchema ;
295+ String key = sql + ':' + currentSchema + ':' + currentCatalog ;
285296 return prepareStatement (sql , false , 0 , key );
286297 }
287298
@@ -411,6 +422,16 @@ public void close() throws SQLException {
411422 resetIsolationReadOnlyRequired = false ;
412423 }
413424
425+ if (resetSchema ) {
426+ connection .setSchema (originalSchema );
427+ resetSchema = false ;
428+ }
429+
430+ if (resetCatalog ) {
431+ connection .setCatalog (originalCatalog );
432+ resetCatalog = false ;
433+ }
434+
414435 // the connection is assumed GOOD so put it back in the pool
415436 lastUseTime = System .currentTimeMillis ();
416437 connection .clearWarnings ();
@@ -511,6 +532,7 @@ public void setReadOnly(boolean readOnly) throws SQLException {
511532 connection .setReadOnly (readOnly );
512533 }
513534
535+
514536 /**
515537 * Also note the Isolation level needs to be reset when put back into the pool.
516538 */
@@ -673,11 +695,23 @@ public void setAutoCommit(boolean autoCommit) throws SQLException {
673695 }
674696 }
675697
698+ @ Override
699+ public void setSchema (String schema ) throws SQLException {
700+ if (status == STATUS_IDLE ) {
701+ throw new SQLException (IDLE_CONNECTION_ACCESSED_ERROR + "setSchema()" );
702+ }
703+ currentSchema = schema ;
704+ resetSchema = true ;
705+ connection .setSchema (schema );
706+ }
707+
676708 @ Override
677709 public void setCatalog (String catalog ) throws SQLException {
678710 if (status == STATUS_IDLE ) {
679711 throw new SQLException (IDLE_CONNECTION_ACCESSED_ERROR + "setCatalog()" );
680712 }
713+ currentCatalog = catalog ;
714+ resetCatalog = true ;
681715 connection .setCatalog (catalog );
682716 }
683717
0 commit comments