Skip to content

Commit

Permalink
The 7.5 backend allows isolation levels that aren't natively supported
Browse files Browse the repository at this point in the history
by "rounding up" to the next stronger supported isolation level.  This
adds support for these levels to the JDBC driver.

Also fix a bug where setting an isolation level would set a class
member variable regardless of wether the call to the backend succeeded
or not.  I believe this only affects pre 7.1 servers, but correct is
correct.
  • Loading branch information
kjurka committed Jan 13, 2004
1 parent 30ef6f3 commit 67ee14e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 44 deletions.
71 changes: 27 additions & 44 deletions org/postgresql/jdbc1/AbstractJdbc1Connection.java
Expand Up @@ -1255,12 +1255,12 @@ public void setAutoCommit(boolean autoCommit) throws SQLException
{
if (haveMinimumServerVersion("7.1"))
{
execSQL("begin;" + getIsolationLevelSQL());
execSQL("begin;");
}
else
{
execSQL("begin");
execSQL(getIsolationLevelSQL());
execSQL(getPre71IsolationLevelSQL(isolationLevel));
}
}
this.autoCommit = autoCommit;
Expand Down Expand Up @@ -1294,13 +1294,13 @@ public void commit() throws SQLException
//TODO: delay starting new transaction until first command
if (haveMinimumServerVersion("7.1"))
{
execSQL("commit;begin;" + getIsolationLevelSQL());
execSQL("commit;begin;");
}
else
{
execSQL("commit");
execSQL("begin");
execSQL(getIsolationLevelSQL());
execSQL(getPre71IsolationLevelSQL(isolationLevel));
}
}

Expand All @@ -1319,13 +1319,13 @@ public void rollback() throws SQLException
//TODO: delay starting transaction until first command
if (haveMinimumServerVersion("7.1"))
{
execSQL("rollback; begin;" + getIsolationLevelSQL());
execSQL("rollback; begin;");
}
else
{
execSQL("rollback");
execSQL("begin");
execSQL(getIsolationLevelSQL());
execSQL(getPre71IsolationLevelSQL(isolationLevel));
}
}

Expand Down Expand Up @@ -1391,30 +1391,34 @@ public void setTransactionIsolation(int level) throws SQLException
//each transaction, thus adding complexity below.
//When we decide to drop support for servers older than 7.1
//this can be simplified
isolationLevel = level;
String isolationLevelSQL;

if (!haveMinimumServerVersion("7.1"))
{
isolationLevelSQL = getIsolationLevelSQL();
isolationLevelSQL = getPre71IsolationLevelSQL(level);
}
else
{
isolationLevelSQL = "SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL ";
switch (isolationLevel)
{
case Connection.TRANSACTION_READ_COMMITTED:
isolationLevelSQL += "READ COMMITTED";
break;
case Connection.TRANSACTION_SERIALIZABLE:
isolationLevelSQL += "SERIALIZABLE";
break;
default:
throw new PSQLException("postgresql.con.isolevel", PSQLState.TRANSACTION_STATE_INVALID,
new Integer(isolationLevel));
}
isolationLevelSQL = "SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL " + getIsolationLevelName(level);
}
execSQL(isolationLevelSQL);
isolationLevel = level;
}

protected String getIsolationLevelName(int level) throws SQLException
{
boolean pg75 = haveMinimumServerVersion("7.5");

if (level == Connection.TRANSACTION_READ_COMMITTED) {
return " READ COMMITTED";
} else if (level == Connection.TRANSACTION_SERIALIZABLE) {
return " SERIALIZABLE";
} else if (pg75 && level == Connection.TRANSACTION_READ_UNCOMMITTED) {
return " READ UNCOMMITTED";
} else if (pg75 && level == Connection.TRANSACTION_REPEATABLE_READ) {
return " REPEATABLE READ";
}
throw new PSQLException("postgresql.con.isolevel", PSQLState.TRANSACTION_STATE_INVALID, new Integer(level));
}

/*
Expand All @@ -1426,30 +1430,9 @@ public void setTransactionIsolation(int level) throws SQLException
* servers, and should be removed when support for these older
* servers are dropped
*/
protected String getIsolationLevelSQL() throws SQLException
protected String getPre71IsolationLevelSQL(int level) throws SQLException
{
//7.1 and higher servers have a default specified so
//no additional SQL is required to set the isolation level
if (haveMinimumServerVersion("7.1"))
{
return "";
}
StringBuffer sb = new StringBuffer("SET TRANSACTION ISOLATION LEVEL");

switch (isolationLevel)
{
case Connection.TRANSACTION_READ_COMMITTED:
sb.append(" READ COMMITTED");
break;

case Connection.TRANSACTION_SERIALIZABLE:
sb.append(" SERIALIZABLE");
break;

default:
throw new PSQLException("postgresql.con.isolevel", PSQLState.TRANSACTION_STATE_INVALID, new Integer(isolationLevel));
}
return sb.toString();
return "SET TRANSACTION ISOLATION LEVEL " + getIsolationLevelName(level);
}

/*
Expand Down
2 changes: 2 additions & 0 deletions org/postgresql/jdbc1/AbstractJdbc1DatabaseMetaData.java
Expand Up @@ -1606,6 +1606,8 @@ public boolean supportsTransactionIsolationLevel(int level) throws SQLException
if (level == Connection.TRANSACTION_SERIALIZABLE ||
level == Connection.TRANSACTION_READ_COMMITTED)
return true;
else if (connection.haveMinimumServerVersion("7.5") && (level == Connection.TRANSACTION_READ_UNCOMMITTED || level == Connection.TRANSACTION_REPEATABLE_READ))
return true;
else
return false;
}
Expand Down

0 comments on commit 67ee14e

Please sign in to comment.