postgresql不支持事务隔离级别4

时间:2018-05-14 04:08:00

标签: postgresql transactions kotlin ktor kotlin-exposed

我正在尝试创建表格(postgres + exposed + ktor + JDBC),我收到了这个错误。

查看我的配置:

的build.gradle

compile group: 'postgresql', name: 'postgresql', version: '9.0-801.jdbc4'

Hello.kt

object Pays : Table() {
val id = integer("id").autoIncrement().primaryKey() // Column
val name = varchar("name", 50) // Column
}

fun main(args: Array) {
Database.connect("jdbc:postgresql://localhost/my_db", driver = "org.postgresql.Driver", user = "user", password = "password")

这是我的错误

Caused by: org.postgresql.util.PSQLException: Transaction isolation level 4 not supported.
at org.postgresql.jdbc2.AbstractJdbc2Connection.setTransactionIsolation(AbstractJdbc2Connection.java:826)
at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManager$ThreadLocalTransaction$connectionLazy$1.invoke(ThreadLocalTransactionManager.kt:25)
at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManager$ThreadLocalTransaction$connectionLazy$1.invoke(ThreadLocalTransactionManager.kt:20)
at kotlin.UnsafeLazyImpl.getValue(Lazy.kt:154)
at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManager$ThreadLocalTransaction.getConnection(ThreadLocalTransactionManager.kt:29)
at org.jetbrains.exposed.sql.Transaction.getConnection(Transaction.kt)
at org.jetbrains.exposed.sql.Database.getMetadata$exposed(Database.kt:17)
at org.jetbrains.exposed.sql.Database$url$2.invoke(Database.kt:26)
at org.jetbrains.exposed.sql.Database$url$2.invoke(Database.kt:15)
at kotlin.SynchronizedLazyImpl.getValue(Lazy.kt:131)
at org.jetbrains.exposed.sql.Database.getUrl(Database.kt)
at org.jetbrains.exposed.sql.Database$dialect$2.invoke(Database.kt:29)
at org.jetbrains.exposed.sql.Database$dialect$2.invoke(Database.kt:15)
at kotlin.SynchronizedLazyImpl.getValue(Lazy.kt:131)
at org.jetbrains.exposed.sql.Database.getDialect$exposed(Database.kt)
at org.jetbrains.exposed.sql.vendors.DefaultKt.getCurrentDialect(Default.kt:319)
at org.jetbrains.exposed.sql.vendors.DefaultKt.getCurrentDialectIfAvailable(Default.kt:323)
at org.jetbrains.exposed.sql.Column.getOnDelete$exposed(Column.kt:14)

2 个答案:

答案 0 :(得分:0)

阅读JDBC驱动程序版本9.0的源代码,我看到了:

/*
 * You can call this method to try to change the transaction
 * isolation level using one of the TRANSACTION_* values.
 *
 * <B>Note:</B> setTransactionIsolation cannot be called while
 * in the middle of a transaction
 *
 * @param level one of the TRANSACTION_* isolation values with
 * the exception of TRANSACTION_NONE; some databases may
 * not support other values
 * @exception SQLException if a database access error occurs
 * @see java.sql.DatabaseMetaData#supportsTransactionIsolationLevel
 */
public void setTransactionIsolation(int level) throws SQLException
{
    checkClosed();

    if (protoConnection.getTransactionState() != ProtocolConnection.TRANSACTION_IDLE)
        throw new PSQLException(GT.tr("Cannot change transaction isolation level in the middle of a transaction."),
                                PSQLState.ACTIVE_SQL_TRANSACTION);

    String isolationLevelName = getIsolationLevelName(level);
    if (isolationLevelName == null)
        throw new PSQLException(GT.tr("Transaction isolation level {0} not supported.", new Integer(level)), PSQLState.NOT_IMPLEMENTED);

    String isolationLevelSQL = "SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL " + isolationLevelName;
    execSQLUpdate(isolationLevelSQL); // nb: no BEGIN triggered
}

getIsolationLevelName的定义如下:

protected String getIsolationLevelName(int level)
{
    boolean pg80 = haveMinimumServerVersion("8.0");

    if (level == Connection.TRANSACTION_READ_COMMITTED)
    {
        return "READ COMMITTED";
    }
    else if (level == Connection.TRANSACTION_SERIALIZABLE)
    {
        return "SERIALIZABLE";
    }
    else if (pg80 && level == Connection.TRANSACTION_READ_UNCOMMITTED)
    {
        return "READ UNCOMMITTED";
    }
    else if (pg80 && level == Connection.TRANSACTION_REPEATABLE_READ)
    {
        return "REPEATABLE READ";
    }

    return null;
}

java.sql.Connection.TRANSACTION_REPEATABLE_READ is 4起,您的错误消息只能表示pg80false

所以唯一的解释是你实际上使用的是早于8.0的PostgreSQL服务器版本,或者更有可能的是,你使用的是PostgreSQL v10,并且JDBC驱动程序太旧而无法理解新的两个版本的编号系统。 / p>

在第一种情况下,您应该升级PostgreSQL,但无论如何,您应该使用当前版本的JDBC驱动程序。这应该照顾你的问题。

答案 1 :(得分:0)

请尝试更新build.gradle

compile group: 'org.postgresql', name: 'postgresql', version: '9.4-1200-jdbc41'