diff --git a/ebean-datasource/src/main/java/io/ebean/datasource/pool/ConnectionPool.java b/ebean-datasource/src/main/java/io/ebean/datasource/pool/ConnectionPool.java index 6106625..ef8d786 100644 --- a/ebean-datasource/src/main/java/io/ebean/datasource/pool/ConnectionPool.java +++ b/ebean-datasource/src/main/java/io/ebean/datasource/pool/ConnectionPool.java @@ -705,6 +705,10 @@ public boolean isAutoCommit() { return autoCommit; } + public boolean isReadOnly() { + return readOnly; + } + int transactionIsolation() { return transactionIsolation; } diff --git a/ebean-datasource/src/main/java/io/ebean/datasource/pool/PooledConnection.java b/ebean-datasource/src/main/java/io/ebean/datasource/pool/PooledConnection.java index 3bdb37e..6327b67 100644 --- a/ebean-datasource/src/main/java/io/ebean/datasource/pool/PooledConnection.java +++ b/ebean-datasource/src/main/java/io/ebean/datasource/pool/PooledConnection.java @@ -107,7 +107,9 @@ final class PooledConnection extends ConnectionDelegator { * connected to a read-only instance and should reset. */ private boolean failoverToReadOnly; - private boolean resetAutoCommit; + private boolean autoCommit; + private boolean readOnly; + private int transactionIsolation; private int schemaState = SCHEMA_CATALOG_UNKNOWN; private int catalogState = SCHEMA_CATALOG_UNKNOWN; @@ -135,7 +137,7 @@ final class PooledConnection extends ConnectionDelegator { * Slot position in the BusyConnectionBuffer. */ private int slotId; - private boolean resetIsolationReadOnlyRequired; + /** * Construct the connection that can refer back to the pool it belongs to. @@ -150,6 +152,9 @@ final class PooledConnection extends ConnectionDelegator { this.name = pool.name() + uniqueId; this.originalSchema = pool.schema(); this.originalCatalog = pool.catalog(); + this.autoCommit = pool.isAutoCommit(); + this.readOnly = pool.isReadOnly(); + this.transactionIsolation = pool.transactionIsolation(); if (originalSchema != null) { this.schemaState = SCHEMA_CATALOG_KNOWN; this.cacheKeySchema = originalSchema; @@ -446,16 +451,20 @@ public void close() throws SQLException { return; } // reset the autoCommit back if client code changed it - if (resetAutoCommit) { + if (autoCommit != pool.isAutoCommit()) { connection.setAutoCommit(pool.isAutoCommit()); - resetAutoCommit = false; + autoCommit = pool.isAutoCommit(); } // Generally resetting Isolation level seems expensive. // Hence using resetIsolationReadOnlyRequired flag // performance reasons. - if (resetIsolationReadOnlyRequired) { - resetIsolationReadOnly(); - resetIsolationReadOnlyRequired = false; + if (transactionIsolation != pool.transactionIsolation()) { + connection.setTransactionIsolation(pool.transactionIsolation()); + transactionIsolation = pool.transactionIsolation(); + } + if (readOnly != pool.isReadOnly()) { + connection.setReadOnly(readOnly); + readOnly = pool.isReadOnly(); } if (catalogState == SCHEMA_CATALOG_CHANGED) { @@ -484,16 +493,6 @@ public void close() throws SQLException { } } - private void resetIsolationReadOnly() throws SQLException { - int level = pool.transactionIsolation(); - if (connection.getTransactionIsolation() != level) { - connection.setTransactionIsolation(level); - } - if (connection.isReadOnly()) { - connection.setReadOnly(false); - } - } - /** * Return true if the connection is too old. */ @@ -567,8 +566,16 @@ void setLastStatement(String lastStatement) { */ @Override public void setReadOnly(boolean readOnly) throws SQLException { - resetIsolationReadOnlyRequired = true; - connection.setReadOnly(readOnly); + if (status == STATUS_IDLE) { + throw new SQLException(IDLE_CONNECTION_ACCESSED_ERROR + "setTransactionIsolation()"); + } + try { + connection.setReadOnly(readOnly); + this.readOnly = readOnly; + } catch (SQLException ex) { + markWithError(ex); + throw ex; + } } @@ -581,8 +588,8 @@ public void setTransactionIsolation(int level) throws SQLException { throw new SQLException(IDLE_CONNECTION_ACCESSED_ERROR + "setTransactionIsolation()"); } try { - resetIsolationReadOnlyRequired = true; connection.setTransactionIsolation(level); + this.transactionIsolation = level; } catch (SQLException ex) { markWithError(ex); throw ex; @@ -727,7 +734,7 @@ public void setAutoCommit(boolean autoCommit) throws SQLException { } try { connection.setAutoCommit(autoCommit); - resetAutoCommit = true; + this.autoCommit = autoCommit; } catch (SQLException ex) { markWithError(ex); throw ex;