From 0a78be150e162aad34c19f233dc2f4ca71f61135 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sat, 4 Nov 2006 17:44:21 +0000 Subject: Support transaction isolation level of "none". --- .../amazon/carbonado/repo/jdbc/JDBCRepository.java | 21 +++++++++++++---- .../carbonado/repo/jdbc/JDBCTransaction.java | 27 ++++++++++++++-------- 2 files changed, 34 insertions(+), 14 deletions(-) (limited to 'src/main/java/com/amazon/carbonado/repo/jdbc') diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java index 3a6ec68..f9c6d25 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCRepository.java @@ -79,7 +79,9 @@ public class JDBCRepository static IsolationLevel mapIsolationLevelFromJdbc(int jdbcLevel) { switch (jdbcLevel) { - case Connection.TRANSACTION_READ_UNCOMMITTED: default: + case Connection.TRANSACTION_NONE: default: + return IsolationLevel.NONE; + case Connection.TRANSACTION_READ_UNCOMMITTED: return IsolationLevel.READ_UNCOMMITTED; case Connection.TRANSACTION_READ_COMMITTED: return IsolationLevel.READ_COMMITTED; @@ -92,7 +94,9 @@ public class JDBCRepository static int mapIsolationLevelToJdbc(IsolationLevel level) { switch (level) { - case READ_UNCOMMITTED: default: + case NONE: default: + return Connection.TRANSACTION_NONE; + case READ_UNCOMMITTED: return Connection.TRANSACTION_READ_UNCOMMITTED; case READ_COMMITTED: return Connection.TRANSACTION_READ_COMMITTED; @@ -513,9 +517,14 @@ public class JDBCRepository // Get connection outside synchronized section since it may block. Connection con = mDataSource.getConnection(); - con.setAutoCommit(false); - if (level != mDefaultIsolationLevel) { - con.setTransactionIsolation(mapIsolationLevelToJdbc(level)); + + if (level == IsolationLevel.NONE) { + con.setAutoCommit(true); + } else { + con.setAutoCommit(false); + if (level != mDefaultIsolationLevel) { + con.setTransactionIsolation(mapIsolationLevelToJdbc(level)); + } } synchronized (mAllTxnMgrs) { @@ -609,6 +618,8 @@ public class JDBCRepository } switch (desiredLevel) { + case NONE: + return IsolationLevel.NONE; case READ_UNCOMMITTED: return mReadUncommittedLevel; case READ_COMMITTED: diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransaction.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransaction.java index d92228d..832745b 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransaction.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransaction.java @@ -34,10 +34,12 @@ import com.amazon.carbonado.IsolationLevel; * @author Brian S O'Neill */ class JDBCTransaction { + // Use this magic value to indicate that the isolation level need not be + // changed when the transaction ends. This is a little optimization to + // avoid a round trip call to the remote database. + private static final int LEVEL_NOT_CHANGED = -1; + private final Connection mConnection; - // Use TRANSACTION_NONE as a magic value to indicate that the isolation - // level need not be changed when the transaction ends. This is a little - // optimization to avoid a round trip call to the remote database. private final int mOriginalLevel; private Savepoint mSavepoint; @@ -46,7 +48,7 @@ class JDBCTransaction { JDBCTransaction(Connection con) { mConnection = con; // Don't change level upon abort. - mOriginalLevel = Connection.TRANSACTION_NONE; + mOriginalLevel = LEVEL_NOT_CHANGED; } /** @@ -57,16 +59,19 @@ class JDBCTransaction { if (level == null) { // Don't change level upon abort. - mOriginalLevel = Connection.TRANSACTION_NONE; + mOriginalLevel = LEVEL_NOT_CHANGED; } else { int newLevel = JDBCRepository.mapIsolationLevelToJdbc(level); int originalLevel = mConnection.getTransactionIsolation(); if (newLevel == originalLevel) { // Don't change level upon abort. - mOriginalLevel = Connection.TRANSACTION_NONE; + mOriginalLevel = LEVEL_NOT_CHANGED; } else { - // Don't change level upon abort. + // Do change level upon abort. mOriginalLevel = originalLevel; + if (originalLevel == Connection.TRANSACTION_NONE) { + mConnection.setAutoCommit(false); + } mConnection.setTransactionIsolation(newLevel); } } @@ -105,8 +110,12 @@ class JDBCTransaction { return mConnection; } else { mConnection.rollback(mSavepoint); - if (mOriginalLevel != Connection.TRANSACTION_NONE) { - mConnection.setTransactionIsolation(mOriginalLevel); + if (mOriginalLevel != LEVEL_NOT_CHANGED) { + if (mOriginalLevel == Connection.TRANSACTION_NONE) { + mConnection.setAutoCommit(true); + } else { + mConnection.setTransactionIsolation(mOriginalLevel); + } } mSavepoint = null; return null; -- cgit v1.2.3