如何在JDBC中为事务设置局部变量

时间:2018-11-02 11:17:39

标签: java postgresql jdbc

我正在一个项目中,我需要在该项目的数据库中初始化一个会话变量。如果我直接使用sql,则初始化是通过SET语句

完成的
set local app.user_id to "0000";

我尝试使用Connection#setClientInfo()对其进行初始化,但失败了

try(Connection connection = getDataSource().getConnection()) {
    boolean isAutoCommit = connection.getAutoCommit();
    try {
        Properties properties = new Properties();
        properties.put("app.user_id", "0000");

        connection.setAutoCommit(false);
        connection.setClientInfo(properties);

        String query = "insert into positions (name, description) values (?, ?)";

        try(PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setString(1, position.getName());
            statement.setString(2, position.getDescription());

            statement.executeUpdate();
        }

        connection.commit();
    }
    catch (SQLException e) {
        e.printStackTrace();
        connection.rollback();
    }
    finally {
        connection.setAutoCommit(isAutoCommit);
    }
}

我得到PSQLException(插入查询取决于参数,并且不会通过)

org.postgresql.util.PSQLException: ERROR: unrecognized configuration parameter "app.user_id"

如果我使用PreparedStatement,则会收到PSQLException并显示消息错误:“ $ 1”或附近的语法错误

try(Connection connection = getDataSource().getConnection()) {
    boolean isAutoCommit = connection.getAutoCommit();
    try {
        connection.setAutoCommit(false);
        try(PreparedStatement statement = connection.prepareStatement("set local app.user_id to ?")) {
            statement.setString(1, "0000");
            statement.execute();
        }

        String query = "insert into positions (name, description) values (?, ?)";

        try(PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setString(1, position.getName());
            statement.setString(2, position.getDescription());

            statement.executeUpdate();
        }

        connection.commit();
    }
    catch (SQLException e) {
        e.printStackTrace();
        connection.rollback();
    }
    finally {
        connection.setAutoCommit(isAutoCommit);
    }
}

唯一的解决方法是直接执行具有固定值的查询。但是在这样做时,我不得不使用串联来构建查询。而且我不想这样做。

try(Connection connection = getDataSource().getConnection()) {
    boolean isAutoCommit = connection.getAutoCommit();
    try {
        connection.setAutoCommit(false);
        try(Statement statement = connection.createStatement()) {
            statement.execute("set local app.user_id to 0000");
        }

        String query = "insert into positions (name, description) values (?, ?)";

        try(PreparedStatement statement = connection.prepareStatement(query)) {
            statement.setString(1, position.getName());
            statement.setString(2, position.getDescription());

            statement.executeUpdate();
        }

        connection.commit();
    }
    catch (SQLException e) {
        e.printStackTrace();
        connection.rollback();
    }
    finally {
        connection.setAutoCommit(isAutoCommit);
    }
}

初始化此类参数的正确方法是什么? 我使用PostgreSQL 11,JDBC 4.2(带有驱动程序42.2.5)和DBCP 2.5

编辑

我是通过致电set_config来做到这一点的。

try(PreparedStatement statement = connection.prepareStatement("select set_config(?, ?, true)")) {
    statement.setString(1, "app.user_id");
    statement.setString(2, "0000");

    statement.execute();
}

但是问题仍然存在。如何在SET

中调用JDBC

1 个答案:

答案 0 :(得分:0)

我认为您需要在DataSource而不是Connection上进行此操作。

postgresql中,我所知道的唯一方法就是下变频。像这样:

DataSource myDS = getDataSource();
if (DataSource instanceof BaseDataSource.class) {
    BaseDataSource pgDS = (BaseDataSource) myDS;   // expose setProperty method
    pgDS.setProperty("app.user_id", "0000");
}

将其放置在应用程序中的位置显然取决于问题中未提供的许多细节。