亚音速:mysqldatareader关闭连接
-
23-10-2019 - |
题
我正在使用subsonic 2.1,并在执行交易时遇到问题
shareddbconnectionscope和transactionscope。问题在于,在obj.save()方法中,我得到了一个“连接必须有效且打开”例外
我将问题跟踪到这一行:
// Loads a SubSonic ActiveRecord object
User user = new User(User.Columns.Username, "John Doe");
在用户类A的构造函数中,A方法“ LoadParam”最终称为
if (rdr != null)
rdr.Close();
看起来RDR.Close()隐式关闭了我的连接,使用自动连接时很好。但是在交易期间,关闭连接不是一个好主意:-)
我的问题是,这是通过设计还是在MySqlDataReader中的错误。
解决方案
那很棘手!调试后,我在subsonic2 mysqledatareader.cs文件中找到了以下方法:
public override IDataReader GetReader(QueryCommand qry)
{
AutomaticConnectionScope conn = new AutomaticConnectionScope(this);
...
cmd.Connection = (MySqlConnection)conn.Connection;
IDataReader rdr;
try
{
rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(MySqlException x)
{
conn.Dispose();
throw x;
}
...
}
这是错误的,因为我使用的是共享DBConnection。在SQLDATAPROVIDER中,它已经修复,但不能用于MySqDataReader。
看起来应该这样:
try
{
// if it is a shared connection, we shouldn't be telling the reader to close it when it is done
rdr = conn .IsUsingSharedConnection ?
cmd.ExecuteReader() :
cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (MySqlException)
{
// AutoConnectionScope will figure out what to do with the connection
conn.Dispose();
//rethrow retaining stack trace.
throw;
}
很重的错误,它使问题不可能进行查询(我必须承认我以前从不需要这个)。
不隶属于 StackOverflow