2010-06-02 41 views
0

我使用亞音速2.1和在執行交易與亞音速:MySqlDataReader將關閉連接

SharedDbConnectionScope和TransactionScope的entcountered問題。 的問題是,在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中的錯誤。

回答

0

這很棘手! 有點調試後,我發現在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; 
     } 

相當沉重的錯誤,它不可能完成的交易呈現查詢(我必須承認,我從來沒有這種需要)。