0

我正在尝试创建表(postgres + 暴露 + ktor + JDBC),我得到了那个错误。

在下面找到我的配置:

构建.gradle

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

你好.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)
4

2 回答 2

1

阅读 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_READis 4,您的错误信息只能表示pg80is false

因此,唯一的解释是您实际上使用的 PostgreSQL 服务器版本早于 8.0,或者更可能的是,您使用的是 PostgreSQL v10 并且 JDBC 驱动程序太旧而无法理解新的两位数版本编号系统。

在第一种情况下,您应该升级 PostgreSQL,但无论如何,您应该使用当前版本的 JDBC 驱动程序。那应该可以解决您的问题。

于 2018-05-14T08:33:51.377 回答
0

请尝试更新 build.gradle

compile group: 'org.postgresql', name: 'postgresql', version: '9.4-1200-jdbc41'
于 2019-04-24T13:52:14.453 回答