我正在使用SubSonic 2.1,在执行事务时遇到一个问题
SharedDbConnectionScope和TransactionScope。问题是在obj.Save()方法中,我得到了一个“连接必须是有效的并且打开的”异常
我将问题追踪到下面这行:
// Loads a SubSonic ActiveRecord object
User user = new User(User.Columns.Username, "John Doe");在User类的这个构造函数中,调用了一个方法"LoadParam“,该方法最终完成
if (rdr != null)
rdr.Close();看起来rdr.Close()隐式地关闭了我的连接,这在使用AutomaticConnection时没有问题。但在事务期间,关闭连接通常不是一个好主意:-)
我的问题是,这是故意的,还是MySqlDataReader中的错误。
发布于 2010-06-02 20:14:40
这真是太棘手了!经过一些调试后,我在SubSonic2 MySqlDataReader.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;
}
...
}这是错误的,因为我使用的是SharedDbConnection。在SqlDataProvider中,这个问题已经修复了,但不适用于MySqlDataReader。
它应该看起来像这样:
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;
}相当严重的错误,它使得在事务中查询变得不可能(我必须承认我以前从来不需要这个)。
https://stackoverflow.com/questions/2955972
复制相似问题