如果在调用提交或回滚之前停电了怎么办? [英] What happens if there is a power outage before calling commit or rollback?

查看:287
本文介绍了如果在调用提交或回滚之前停电了怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在用以下代码进行测试:

So I've been testing with this code:

MySqlTransaction trans = null;
                    var con = new MySqlConnection(DBInfo.CON_STRING);
                    try
                    {

                            con.Open();
                            trans = con.BeginTransaction();

                            var query = "INSERT INTO test (num) VALUE (@val1)";
                            var cmd = new MySqlCommand(query, con);
                            cmd.Transaction = trans;
                            cmd.Prepare();

                            cmd.Parameters.AddWithValue("@val1", 0);

                            for (int i = 0; i <= 15; i++)
                            {

                                    cmd.Parameters["@val1"].Value = i;
                                    cmd.ExecuteNonQuery();
                                    if (i == 10)
                                    {
                                            //throw new DivideByZeroException();
                                            MessageBox.Show("pause");
                                    }
                            }

                            trans.Commit();
                    }
                    catch (Exception ex)
                    {

                            MessageBox.Show(ex.StackTrace);

                            try
                            {
                                if (trans != null) trans.Rollback();
                            }
                            catch (Exception ex1)
                            {
                                    MessageBox.Show(ex1.Message);
                            }
                    }
                    finally
                    {
                        con.Close();
                    }

提交和回滚方法.我有一个问题.在for块中,我有一个MessageBox可以暂停代码的执行,因此我可以停止程序以模拟程序的突然执行.问题是查询没有提交,但是服务器发生了什么呢?它会自动回滚吗?如果正在运行该程序的PC恰好是服务器并且断电了,会发生什么情况? (假设停电发生在for块的执行中间.

The commit and rollback method. And i have a question. In the for block i have a MessageBox to pause the execution of the code so i can stop the program to simulate a sudden execution of the program. Thing is that the query didn't commit but what happened to the server then? Does it rollback automatically? and what happens if for example the PC where the program is running happens to be the server and there is a power outage? (let say the power outage happened in the middle of the execution of the for block.

推荐答案

当数据库服务器在停电后重新启动时,它将恢复为仅包含已完成工作的干净状态.

When a database server restarts after a power outages, it reverts back to a clean state that contains only committed work.

当数据库客户端在事务中间被切断时,服务器将通过回滚自动清理事务.

When a database client gets cut off in the middle of a transaction, the server will clean up the transaction automatically by rolling it back.

这篇关于如果在调用提交或回滚之前停电了怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆