我试图在使用Npgsql 2.1.3(Postgres v9.3)和Dapper 1.29的事务中运行一些命令,但偶尔会遇到一些意外的异常。有时,代码工作得很好。其他时候,我得到以下例外:
Npgsql.NpgsqlException : ERROR: 57014: canceling statement due to user request
我的连接字符串是(我删除了用户ID,密码和数据库):
PORT=5432;TIMEOUT=15;POOLING=True;MINPOOLSIZE=1;MAXPOOLSIZE=20;COMMANDTIMEOUT=20;
COMPATIBLE=2.1.3.0;HOST=127.0.0.1;USER ID=...;PASSWORD=...;DATABASE=...;SSLMODE=Require
以下是失败的代码示例(为简洁而忽略错误检查):
var connection = new NpgsqlConnection(connectionString);
if (connection.State != ConnectionState.Open)
connection.Open();
IDbTransaction transaction = connection.BeginTransaction(
IsolationLevel.RepeatableRead);
try {
...
// Select some stuff using the connection underlying the transaction
var query = "SELECT * FROM ...";
var result = connection.Query<MyType>(query, dynamicParams, transaction).ToList();
...
// Perform an insert using the same command indicated above
query = "INSERT INTO ... (...) VALUES (...) RETURNING *";
var result2 = connection.Query<MyType2>(query, dynamicParams, transaction).FirstOrDefault();
...
// Delete the entry added above
query = "DELETE FROM ... WHERE id = :id";
var result3 = connection.Query<long>(query, dynamicParams, transaction).FirstOrDefault();
// I Don't explicitly call transaction.Rollback(),
// but I have tried that and it has no effect on the outcome
} finally {
// Note that if I put Thread.Sleep(1000) here, I never get the exception
transaction.Dispose(); // Npgsql.NpgsqlException (sometimes)
connection.Dispose();
}
有谁知道为什么上面的代码会不正常地失败?我已经验证了我对connection.Query&lt;&gt;的调用。抛出一个例外。此外,当我在transaction.Dispose()之前放置一个Thread.Sleep()命令时,异常永远不会发生。似乎有一些异步代码被调用来读取Rollback()的响应,这在事务处理之后发生,从而抛出异常。我查看了NpgsqlConnection和NpgsqlTransaction的所有属性,但它们都没有显示是否已经读取了Rollback()的结果。我将继续对此进行调查,但非常感谢任何其他帮助。
答案 0 :(得分:0)
对于遇到此问题的任何其他人:更新到Npgsql 2. 2 .3(从2. 1 .3)修复了此问题。我没有看到Npgsql发行说明中的任何修复似乎是相关的,但显然有些修改可以修复故障。