@@ -57,6 +57,32 @@ final class PooledConnection extends ConnectionDelegator {
5757
5858 private static final int RO_MYSQL_1290 = 1290 ;
5959
60+ /**
61+ * Constant for schema/catalog, when we are in SCHEMA_CATALOG_UNKNOWN state
62+ * This is used for correct cache key computation. (We cannot use 'null'
63+ * here, as we might get a collision in edge cases)
64+ */
65+ private static final String UNKNOWN = "@unknown" ;
66+
67+ /**
68+ * The schema/catalog is unknown, that means, we have not yet touched the
69+ * value on the underlying connection.
70+ * We do not have to restore it.
71+ */
72+ private static final int SCHEMA_CATALOG_UNKNOWN = 0 ;
73+
74+ /**
75+ * The schema/catalog is changed. The original value has to be restored on
76+ * close()
77+ */
78+ private static final int SCHEMA_CATALOG_CHANGED = 1 ;
79+
80+ /**
81+ * We know the original value of the underlying connection, but there is no
82+ * demand to restore it.
83+ */
84+ private static final int SCHEMA_CATALOG_KNOWN = 2 ;
85+
6086 private final String name ;
6187 private final ConnectionPool pool ;
6288 private final Connection connection ;
@@ -82,12 +108,16 @@ final class PooledConnection extends ConnectionDelegator {
82108 */
83109 private boolean failoverToReadOnly ;
84110 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 ;
111+ private int schemaState = SCHEMA_CATALOG_UNKNOWN ;
112+ private int catalogState = SCHEMA_CATALOG_UNKNOWN ;
113+
114+ // this is used for cache computation
115+ private String cacheKeySchema = UNKNOWN ;
116+ private String cacheKeyCatalog = UNKNOWN ;
117+
118+ // original values are lazily initialized and restored on close()
119+ private String originalSchema ;
120+ private String originalCatalog ;
91121
92122 private long startUseTime ;
93123 private long lastUseTime ;
@@ -118,12 +148,20 @@ final class PooledConnection extends ConnectionDelegator {
118148 this .pool = pool ;
119149 this .connection = connection ;
120150 this .name = pool .name () + uniqueId ;
151+ this .originalSchema = pool .schema ();
152+ this .originalCatalog = pool .catalog ();
153+ if (originalSchema != null ) {
154+ this .schemaState = SCHEMA_CATALOG_KNOWN ;
155+ this .cacheKeySchema = originalSchema ;
156+ }
157+ if (originalCatalog != null ) {
158+ this .catalogState = SCHEMA_CATALOG_KNOWN ;
159+ this .cacheKeyCatalog = originalCatalog ;
160+ }
121161 this .pstmtCache = new PstmtCache (pool .pstmtCacheSize ());
122162 this .maxStackTrace = pool .maxStackTraceSize ();
123163 this .creationTime = System .currentTimeMillis ();
124164 this .lastUseTime = creationTime ;
125- this .currentSchema = this .originalSchema = connection .getSchema ();
126- this .currentCatalog = this .originalCatalog = connection .getCatalog ();
127165 pool .inc ();
128166 }
129167
@@ -139,8 +177,6 @@ final class PooledConnection extends ConnectionDelegator {
139177 this .maxStackTrace = 0 ;
140178 this .creationTime = System .currentTimeMillis ();
141179 this .lastUseTime = creationTime ;
142- this .currentSchema = this .originalSchema = "DEFAULT" ;
143- this .currentCatalog = this .originalCatalog = "DEFAULT" ;
144180 }
145181
146182 /**
@@ -283,7 +319,7 @@ void returnPreparedStatement(ExtendedPreparedStatement pstmt) {
283319 */
284320 @ Override
285321 public PreparedStatement prepareStatement (String sql , int returnKeysFlag ) throws SQLException {
286- String key = sql + ':' + currentSchema + ':' + currentCatalog + ':' + returnKeysFlag ;
322+ String key = sql + ':' + cacheKeySchema + ':' + cacheKeyCatalog + ':' + returnKeysFlag ;
287323 return prepareStatement (sql , true , returnKeysFlag , key );
288324 }
289325
@@ -292,7 +328,7 @@ public PreparedStatement prepareStatement(String sql, int returnKeysFlag) throws
292328 */
293329 @ Override
294330 public PreparedStatement prepareStatement (String sql ) throws SQLException {
295- String key = sql + ':' + currentSchema + ':' + currentCatalog ;
331+ String key = sql + ':' + cacheKeySchema + ':' + cacheKeyCatalog ;
296332 return prepareStatement (sql , false , 0 , key );
297333 }
298334
@@ -422,14 +458,17 @@ public void close() throws SQLException {
422458 resetIsolationReadOnlyRequired = false ;
423459 }
424460
425- if (resetSchema ) {
426- connection .setSchema (originalSchema );
427- resetSchema = false ;
461+ if (catalogState == SCHEMA_CATALOG_CHANGED ) {
462+ connection .setCatalog (originalCatalog );
463+ cacheKeyCatalog = originalCatalog ;
464+ catalogState = SCHEMA_CATALOG_KNOWN ;
428465 }
429466
430- if (resetCatalog ) {
431- connection .setCatalog (originalCatalog );
432- resetCatalog = false ;
467+ if (schemaState == SCHEMA_CATALOG_CHANGED ) {
468+ connection .setSchema (originalSchema );
469+ // we can use original value for cache computation from now on
470+ cacheKeySchema = originalSchema ;
471+ schemaState = SCHEMA_CATALOG_KNOWN ;
433472 }
434473
435474 // the connection is assumed GOOD so put it back in the pool
@@ -700,8 +739,13 @@ public void setSchema(String schema) throws SQLException {
700739 if (status == STATUS_IDLE ) {
701740 throw new SQLException (IDLE_CONNECTION_ACCESSED_ERROR + "setSchema()" );
702741 }
703- currentSchema = schema ;
704- resetSchema = true ;
742+ if (schemaState == SCHEMA_CATALOG_UNKNOWN ) {
743+ // lazily initialise the originalSchema
744+ originalSchema = getSchema ();
745+ // state would be KNOWN here
746+ }
747+ schemaState = SCHEMA_CATALOG_CHANGED ;
748+ cacheKeySchema = schema ;
705749 connection .setSchema (schema );
706750 }
707751
@@ -710,8 +754,11 @@ public void setCatalog(String catalog) throws SQLException {
710754 if (status == STATUS_IDLE ) {
711755 throw new SQLException (IDLE_CONNECTION_ACCESSED_ERROR + "setCatalog()" );
712756 }
713- currentCatalog = catalog ;
714- resetCatalog = true ;
757+ if (schemaState == SCHEMA_CATALOG_UNKNOWN ) {
758+ originalCatalog = getCatalog ();
759+ }
760+ catalogState = SCHEMA_CATALOG_CHANGED ;
761+ cacheKeyCatalog = catalog ;
715762 connection .setCatalog (catalog );
716763 }
717764
0 commit comments