SubSonic:MySqlDataReader 关闭连接

发布于 2024-09-04 01:36:14 字数 517 浏览 5 评论 0原文

执行事务时遇到问题

我正在使用 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 中的错误。

I am using SubSonic 2.1 and entcountered a problem while executing a Transaction with

SharedDbConnectionScope and TransactionScope.
The problem is that in the obj.Save() method I get an "The connection must be valid and open" exception

I tracked down the problem to this line:

// Loads a SubSonic ActiveRecord object
User user = new User(User.Columns.Username, "John Doe");

in this Constructor of the User class a method "LoadParam" is called which eventually does

if (rdr != null)
    rdr.Close();

It looks like the rdr.Close() implicitly closes my connection which is fine when using the AutomaticConnection. But during a transaction it is usally not a good idea to close the connection :-)

My Question is if this is by design or if it's an error in the MySqlDataReader.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿透光 2024-09-11 01:36:14

这很棘手!
经过一番调试后,我在 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;
        }

相当严重的错误,它使得事务中的查询变得不可能(我必须承认我以前从未需要过这个)。

That was tricky!
After a bit debugging I found the following method in the SubSonic2 MySqlDataReader.cs file:

    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;
        }

        ...
    }

Which is wrong since I am using a SharedDbConnection. In the SqlDataProvider it has been fixed already but not for MySqlDataReader.

It should look like this:

        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;
        }

Pretty heavy bug, it rendered Querying in a Transaction impossible (I must admit I never needed this before).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文