Menu

#45 Transactions broken for DB read

open
nobody
None
5
2003-06-30
2003-06-30
Anonymous
No

Selecting data from the DB while inside a transaction
returns:

Exception: System.NullReferenceException
Message: Object reference not set to an instance of an
object.
Source: ByteFX.Data
at
ByteFX.Data.MySqlClient.MySqlDataReader.GetValue(Int32
i) in
Z:\workingcopy\mysqldriver\MySqlClient\datareader.cs:line
35
at
ByteFX.Data.MySqlClient.MySqlDataReader.GetInt32(Int32
i) in
Z:\workingcopy\mysqldriver\MySqlClient\datareader.cs:line
476
at .... (my code)

The problems seems to originate in the
MySqlClient/Driver.cs class in 'ReadPacket()' where the
packet type is returned as UpdateOrOk, which causes
problems around line 152 of MySqlClient/datareader.cs

Discussion

  • Reggie Burnett

    Reggie Burnett - 2003-07-03

    Logged In: YES
    user_id=523261

    Do you have a code snippet you can send me that shows the
    problem?

    Thanks
    Reggie

     
  • Nobody/Anonymous

    Logged In: NO

    Using MySql for windows version 4.0.13-nt on WinXP pro,
    ByteFx driver version 0.7.0.34801, the following code breaks
    with the given SQL.

    --
    -- SQL
    --
    CREATE TABLE account (
    id INTEGER PRIMARY KEY,
    balance INTEGER NOT NULL
    );

    INSERT INTO account VALUES(1, 100);

    using System;
    using ByteFX.Data.MySqlClient;

    namespace MySqlTest
    {
    class Test1
    {
    public Test1()
    {
    int id = 1;
    int extra = 200;

            string connectionString = "server=localhost;user
    

    id=root;database=test";
    MySqlConnection connection = new
    MySqlConnection(connectionString);
    connection.Open();

            MySqlTransaction transaction = connection.BeginTransaction\(\);
    
            MySqlParameter idParam = new MySqlParameter\("@id",
    

    MySqlDbType.Int24, "id");
    idParam.Value = id;

            string sql = "SELECT balance FROM account WHERE id = @id";            
            MySqlCommand cmd = new MySqlCommand\(sql, connection,
    

    transaction);
    cmd.Parameters.Add(idParam);
    MySqlDataReader reader = cmd.ExecuteReader();

            while \(reader.Read\(\)\) 
            \{
                int balance = reader.GetInt32\(0\);
                reader.Close\(\);
    
                MySqlParameter balanceParam = new
    

    MySqlParameter("@balance", MySqlDbType.Int24, "balance");
    balanceParam.Value = balance + extra;
    idParam.Value = id;

                sql = "UPDATE account SET balance = @balance WHERE id =
    

    @id";
    cmd = new MySqlCommand(sql, connection, transaction);
    cmd.Parameters.Add(idParam);
    cmd.Parameters.Add(balanceParam);

                int updateCount = cmd.ExecuteNonQuery\(\);              
                if \(updateCount == 1\) 
                \{
                    transaction.Commit\(\);
                    connection.Close\(\);
                    return;
                \} 
                else 
                \{
                    transaction.Rollback\(\);
                    connection.Close\(\);
                    return;
                \}
            \}
    
            reader.Close\(\);
            transaction.Rollback\(\);
            connection.Close\(\);
        \}
    
        \[STAThread\]
        static void Main\(string\[\] args\)
        \{
            new Test1\(\);
        \}
    \}
    

    }

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.