[英]Why I'm getting an error informing that “a result was not expected” when executing stored procedures on PostgreSQL from Java in a batch?
I have this procedure in the database: 我在数据库中有这个过程:
CREATE OR REPLACE FUNCTION replacePageRelevance(id INT, value REAL) RETURNS VOID AS $$
BEGIN
INSERT INTO pageRelevance VALUES (id,value);
EXCEPTION WHEN unique_violation THEN
UPDATE pageRelevance SET relevance = value WHERE pageId = id;
END
$$
LANGUAGE plpgsql;
And this code that calls this function: 这段代码调用了这个函数:
private final String PAGE_RELEVANCE_SQL = "SELECT replacePageRelevance(?,?::REAL)";
try (CallableStatement cstm = conn.prepareCall(PAGE_RELEVANCE_SQL)) {
for (Map.Entry<Integer, Double> entry : weightMap.entrySet()) {
cstm.setInt(1, entry.getKey());
cstm.setDouble(2, entry.getValue());
cstm.addBatch();
}
cstm.executeBatch();
} catch (SQLException e) {
LOGGER.error("Error discovering pages relevance: " + e.getNextException());
}
}
When I execute the batch, the values are inserted or replaced in the table, but after that, I'm getting an exception informing that A result was returned when none was expected.
当我执行批处理时,会在表中插入或替换值,但在此之后,我收到一个异常,通知
A result was returned when none was expected.
I don't know what is wrong, if the way I call the procedure or the procedure itself. 我不知道出了什么问题,如果我称之为程序或程序本身。 What can be the problem and how to solve it?
可能是什么问题以及如何解决?
Call a procedure with SELECT
is the right/only way? 使用
SELECT
调用过程是正确/唯一的方法吗?
From what I can tell you are using SELECT
when call
should be used. 从我可以告诉你正在使用
SELECT
时call
应该被使用。
An example from the PostgreSQL documentation on the JDBC interface : JDBC接口上PostgreSQL文档的一个示例:
// Turn transactions off.
con.setAutoCommit(false);
// Procedure call.
CallableStatement upperProc = con.prepareCall("{ ? = call upper( ? ) }");
upperProc.registerOutParameter(1, Types.VARCHAR);
upperProc.setString(2, "lowercase to uppercase");
upperProc.execute();
String upperCased = upperProc.getString(1);
upperProc.close();
Note that the ? = call
请注意
? = call
? = call
syntax for the result is unnecessary in your case - you will want to use just call replacePageRelevance(?,?::REAL)
? = call
在您的情况下,结果的? = call
语法是不必要的 - 您将只想使用call replacePageRelevance(?,?::REAL)
The reason that this syntax differs from actual PostgreSQL is because this is part of the JDBC specification . 这种语法与实际的PostgreSQL不同的原因是因为这是JDBC规范的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.