简体   繁体   中英

Why won't my transaction make changes to the database?

I am writing an application that needs to update a MySQL database. What I am doing right now is deleting rows from a table and then inserting new data, so I am using a transaction to make sure I get all or none of the changes. However, nothing is being changed and I can't figure out why because no exceptions are thrown.

Here is a code snippet of the transaction:

using(var conn = new MySqlConnection(connectionString))
{
    conn.Open();
    // Use transaction to get all or nothing
    using(var trans = conn.BeginTransaction())
    {
        try
        {
            using(MySqlCommand cmd = new MySqlCommand())
            {
                // Clear the current summary entries
                cmd.Connection = conn;
                cmd.CommandText = "DELETE FROM summaryTable WHERE projectID = @Id";
                cmd.Parameters.AddWithValue("@Id", id);
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
            }

            // Write new entries
            foreach(SummaryObject record in summaryList)
            {
                using(MySqlCommand cmd = new MySqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = "INSERT INTO summaryTable VALUES(/*All values here*/)";
                    // Add With Value statements
                    cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                }
            }
        }
        catch(MySqlException e)
        {
            try
            {
                // try roll back
            }
            catch(MySqlException e1)
            {
                // Catch rollback fail
            }
        }
    }
    conn.Close();
}

I have shortened a couple things but I know that the SQL statements work, because I tested them earlier, before changing it to a transaction.

You must explicitly commit or roll back the transaction using the Commit or Rollback method. Source

Thus, put

trans.Commit();

at the end of your try block.

It seems that you don't commit your transactions

using(var conn = new MySqlConnection(connectionString))
{
    conn.Open();
    // Use transaction to get all or nothing
    using(var trans = conn.BeginTransaction())
    {
        try
        {
            ......
            trans.Commit();
        }
        catch(MySqlException e)
        {
            trans.Rollback();
        }
    }
    // conn.Close(); NOT NEEDED
}

Notice that having the try/catch inside the using of the connection requires you to explicitily rollback in case of exception. If you have setup your try/catch outside the using block of the connection you could spare the rollback because closing the connection without commit is equivalent to an automatic rollback

MySqlTransaction trans;
try
{
    using(var conn = new MySqlConnection(connectionString))
    {
        conn.Open();
        using(trans = conn.BeginTransaction())
        {
            ......
            trans.Commit()
        }
    }
}
catch(MySqlException e)
{
    // NOT NEEDED => gives error 
    // trans.Rollback();
}

You seem to have missing the commit of the transaction. And without auto commit the database does a rollback -> no data will be changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM