Skip to content

Commit 2ef90a7

Browse files
authored
Merge pull request #109 from FOCONIS/track-readonly-isolationlevel
Track current autoCommit/readonly/transactionIsolation
2 parents 0feb41a + 6984924 commit 2ef90a7

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

ebean-datasource/src/main/java/io/ebean/datasource/pool/ConnectionPool.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,10 @@ public boolean isAutoCommit() {
708708
return autoCommit;
709709
}
710710

711+
public boolean isReadOnly() {
712+
return readOnly;
713+
}
714+
711715
int transactionIsolation() {
712716
return transactionIsolation;
713717
}

ebean-datasource/src/main/java/io/ebean/datasource/pool/PooledConnection.java

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ final class PooledConnection extends ConnectionDelegator {
107107
* connected to a read-only instance and should reset.
108108
*/
109109
private boolean failoverToReadOnly;
110-
private boolean resetAutoCommit;
110+
private boolean autoCommit;
111+
private boolean readOnly;
112+
private int transactionIsolation;
111113
private int schemaState = SCHEMA_CATALOG_UNKNOWN;
112114
private int catalogState = SCHEMA_CATALOG_UNKNOWN;
113115

@@ -135,7 +137,7 @@ final class PooledConnection extends ConnectionDelegator {
135137
* Slot position in the BusyConnectionBuffer.
136138
*/
137139
private int slotId;
138-
private boolean resetIsolationReadOnlyRequired;
140+
139141

140142
/**
141143
* Construct the connection that can refer back to the pool it belongs to.
@@ -150,6 +152,9 @@ final class PooledConnection extends ConnectionDelegator {
150152
this.name = pool.name() + uniqueId;
151153
this.originalSchema = pool.schema();
152154
this.originalCatalog = pool.catalog();
155+
this.autoCommit = pool.isAutoCommit();
156+
this.readOnly = pool.isReadOnly();
157+
this.transactionIsolation = pool.transactionIsolation();
153158
if (originalSchema != null) {
154159
this.schemaState = SCHEMA_CATALOG_KNOWN;
155160
this.cacheKeySchema = originalSchema;
@@ -446,16 +451,20 @@ public void close() throws SQLException {
446451
return;
447452
}
448453
// reset the autoCommit back if client code changed it
449-
if (resetAutoCommit) {
454+
if (autoCommit != pool.isAutoCommit()) {
450455
connection.setAutoCommit(pool.isAutoCommit());
451-
resetAutoCommit = false;
456+
autoCommit = pool.isAutoCommit();
452457
}
453458
// Generally resetting Isolation level seems expensive.
454459
// Hence using resetIsolationReadOnlyRequired flag
455460
// performance reasons.
456-
if (resetIsolationReadOnlyRequired) {
457-
resetIsolationReadOnly();
458-
resetIsolationReadOnlyRequired = false;
461+
if (transactionIsolation != pool.transactionIsolation()) {
462+
connection.setTransactionIsolation(pool.transactionIsolation());
463+
transactionIsolation = pool.transactionIsolation();
464+
}
465+
if (readOnly != pool.isReadOnly()) {
466+
connection.setReadOnly(readOnly);
467+
readOnly = pool.isReadOnly();
459468
}
460469

461470
if (catalogState == SCHEMA_CATALOG_CHANGED) {
@@ -484,16 +493,6 @@ public void close() throws SQLException {
484493
}
485494
}
486495

487-
private void resetIsolationReadOnly() throws SQLException {
488-
int level = pool.transactionIsolation();
489-
if (connection.getTransactionIsolation() != level) {
490-
connection.setTransactionIsolation(level);
491-
}
492-
if (connection.isReadOnly()) {
493-
connection.setReadOnly(false);
494-
}
495-
}
496-
497496
/**
498497
* Return true if the connection is too old.
499498
*/
@@ -567,8 +566,16 @@ void setLastStatement(String lastStatement) {
567566
*/
568567
@Override
569568
public void setReadOnly(boolean readOnly) throws SQLException {
570-
resetIsolationReadOnlyRequired = true;
571-
connection.setReadOnly(readOnly);
569+
if (status == STATUS_IDLE) {
570+
throw new SQLException(IDLE_CONNECTION_ACCESSED_ERROR + "setTransactionIsolation()");
571+
}
572+
try {
573+
connection.setReadOnly(readOnly);
574+
this.readOnly = readOnly;
575+
} catch (SQLException ex) {
576+
markWithError(ex);
577+
throw ex;
578+
}
572579
}
573580

574581

@@ -581,8 +588,8 @@ public void setTransactionIsolation(int level) throws SQLException {
581588
throw new SQLException(IDLE_CONNECTION_ACCESSED_ERROR + "setTransactionIsolation()");
582589
}
583590
try {
584-
resetIsolationReadOnlyRequired = true;
585591
connection.setTransactionIsolation(level);
592+
this.transactionIsolation = level;
586593
} catch (SQLException ex) {
587594
markWithError(ex);
588595
throw ex;
@@ -727,7 +734,7 @@ public void setAutoCommit(boolean autoCommit) throws SQLException {
727734
}
728735
try {
729736
connection.setAutoCommit(autoCommit);
730-
resetAutoCommit = true;
737+
this.autoCommit = autoCommit;
731738
} catch (SQLException ex) {
732739
markWithError(ex);
733740
throw ex;

0 commit comments

Comments
 (0)