From 72b3e5b85efc96107294bac4497db972014d79cd Mon Sep 17 00:00:00 2001 From: milimoe <110188673+milimoe@users.noreply.github.com> Date: Wed, 10 May 2023 09:10:29 +0800 Subject: [PATCH] Add SQL Transaction (#12) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加数据库事务,支持提交和回滚操作。 --- .../Utility/DataUtility/MySQLHelper.cs | 31 ++++++++++++++++ .../Utility/DataUtility/MySQLManager.cs | 35 ++++++++++--------- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/FunGame.Server/Utility/DataUtility/MySQLHelper.cs b/FunGame.Server/Utility/DataUtility/MySQLHelper.cs index e31d8b8..72dacff 100644 --- a/FunGame.Server/Utility/DataUtility/MySQLHelper.cs +++ b/FunGame.Server/Utility/DataUtility/MySQLHelper.cs @@ -30,6 +30,7 @@ namespace Milimoe.FunGame.Server.Utility private int _UpdateRows = 0; private DataSet _DataSet = new(); private MySQLConnection? _Connection; + private MySqlTransaction? _Transaction; private readonly ServerModel? ServerModel; private readonly bool _IsOneTime = false; @@ -141,6 +142,36 @@ namespace Milimoe.FunGame.Server.Utility _Connection = new MySQLConnection(out _ServerInfo); } + /// + /// 创建一个SQL事务 + /// + public void NewTransaction() + { + _Transaction ??= _Connection?.Connection?.BeginTransaction(); + } + + /// + /// 提交事务 + /// + public void Commit() + { + _Transaction?.Commit(); + _Transaction = null; + } + + /// + /// 回滚事务 + /// + public void Rollback() + { + _Transaction?.Rollback(); + _Transaction = null; + } + + /// + /// 获取客户端名称 + /// + /// private string GetClientName() { if (ServerModel is null) return ""; diff --git a/FunGame.Server/Utility/DataUtility/MySQLManager.cs b/FunGame.Server/Utility/DataUtility/MySQLManager.cs index 213bcb1..12fe356 100644 --- a/FunGame.Server/Utility/DataUtility/MySQLManager.cs +++ b/FunGame.Server/Utility/DataUtility/MySQLManager.cs @@ -20,18 +20,22 @@ namespace Milimoe.FunGame.Server.Utility try { PrepareCommand(Helper, cmd); + Helper.NewTransaction(); + updaterow = cmd.ExecuteNonQuery(); if (updaterow > 0) { Result = SQLResult.Success; } else Result = SQLResult.NotFound; + Helper.Commit(); } catch (Exception e) { ServerHelper.Error(e); updaterow = -1; Result = SQLResult.Fail; + Helper.Rollback(); } return updaterow; @@ -89,17 +93,21 @@ namespace Milimoe.FunGame.Server.Utility try { PrepareCommand(Helper, cmd); + Helper.NewTransaction(); + updaterow = cmd.ExecuteNonQuery(); if (updaterow > 0) { Result = SQLResult.Success; } else Result = SQLResult.NotFound; + Helper.Commit(); } catch (Exception e) { ServerHelper.Error(e); Result = SQLResult.Fail; + Helper.Rollback(); } return cmd.LastInsertedId; @@ -110,31 +118,26 @@ namespace Milimoe.FunGame.Server.Utility /// /// MySQLHelper /// 命令对象 - /// 事务 - public static void PrepareCommand(MySQLHelper Helper, MySqlCommand cmd, MySqlTransaction? trans = null) + public static void PrepareCommand(MySQLHelper Helper, MySqlCommand cmd) { if (Helper.Connection != null) { MySqlConnection? conn = Helper.Connection.Connection; - if (conn != null && conn.State != ConnectionState.Open) - conn.Open(); - - cmd.Connection = conn; - cmd.CommandText = Helper.Script; - - if (trans != null) + if (conn != null) { - cmd.Transaction = trans; - } + if (conn.State != ConnectionState.Open) conn.Open(); - cmd.CommandType = Helper.CommandType; + cmd.Connection = conn; + cmd.CommandText = Helper.Script; + cmd.CommandType = Helper.CommandType; - if (Helper.Parameters != null) - { - foreach (MySqlParameter parm in Helper.Parameters) + if (Helper.Parameters != null) { - cmd.Parameters.Add(parm); + foreach (MySqlParameter parm in Helper.Parameters) + { + cmd.Parameters.Add(parm); + } } } }