mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-12-05 16:16:34 +00:00
parent
690c85f2e2
commit
72b3e5b85e
@ -30,6 +30,7 @@ namespace Milimoe.FunGame.Server.Utility
|
|||||||
private int _UpdateRows = 0;
|
private int _UpdateRows = 0;
|
||||||
private DataSet _DataSet = new();
|
private DataSet _DataSet = new();
|
||||||
private MySQLConnection? _Connection;
|
private MySQLConnection? _Connection;
|
||||||
|
private MySqlTransaction? _Transaction;
|
||||||
private readonly ServerModel? ServerModel;
|
private readonly ServerModel? ServerModel;
|
||||||
private readonly bool _IsOneTime = false;
|
private readonly bool _IsOneTime = false;
|
||||||
|
|
||||||
@ -141,6 +142,36 @@ namespace Milimoe.FunGame.Server.Utility
|
|||||||
_Connection = new MySQLConnection(out _ServerInfo);
|
_Connection = new MySQLConnection(out _ServerInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建一个SQL事务
|
||||||
|
/// </summary>
|
||||||
|
public void NewTransaction()
|
||||||
|
{
|
||||||
|
_Transaction ??= _Connection?.Connection?.BeginTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 提交事务
|
||||||
|
/// </summary>
|
||||||
|
public void Commit()
|
||||||
|
{
|
||||||
|
_Transaction?.Commit();
|
||||||
|
_Transaction = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 回滚事务
|
||||||
|
/// </summary>
|
||||||
|
public void Rollback()
|
||||||
|
{
|
||||||
|
_Transaction?.Rollback();
|
||||||
|
_Transaction = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取客户端名称
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
private string GetClientName()
|
private string GetClientName()
|
||||||
{
|
{
|
||||||
if (ServerModel is null) return "";
|
if (ServerModel is null) return "";
|
||||||
|
|||||||
@ -20,18 +20,22 @@ namespace Milimoe.FunGame.Server.Utility
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
PrepareCommand(Helper, cmd);
|
PrepareCommand(Helper, cmd);
|
||||||
|
Helper.NewTransaction();
|
||||||
|
|
||||||
updaterow = cmd.ExecuteNonQuery();
|
updaterow = cmd.ExecuteNonQuery();
|
||||||
if (updaterow > 0)
|
if (updaterow > 0)
|
||||||
{
|
{
|
||||||
Result = SQLResult.Success;
|
Result = SQLResult.Success;
|
||||||
}
|
}
|
||||||
else Result = SQLResult.NotFound;
|
else Result = SQLResult.NotFound;
|
||||||
|
Helper.Commit();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
ServerHelper.Error(e);
|
ServerHelper.Error(e);
|
||||||
updaterow = -1;
|
updaterow = -1;
|
||||||
Result = SQLResult.Fail;
|
Result = SQLResult.Fail;
|
||||||
|
Helper.Rollback();
|
||||||
}
|
}
|
||||||
|
|
||||||
return updaterow;
|
return updaterow;
|
||||||
@ -89,17 +93,21 @@ namespace Milimoe.FunGame.Server.Utility
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
PrepareCommand(Helper, cmd);
|
PrepareCommand(Helper, cmd);
|
||||||
|
Helper.NewTransaction();
|
||||||
|
|
||||||
updaterow = cmd.ExecuteNonQuery();
|
updaterow = cmd.ExecuteNonQuery();
|
||||||
if (updaterow > 0)
|
if (updaterow > 0)
|
||||||
{
|
{
|
||||||
Result = SQLResult.Success;
|
Result = SQLResult.Success;
|
||||||
}
|
}
|
||||||
else Result = SQLResult.NotFound;
|
else Result = SQLResult.NotFound;
|
||||||
|
Helper.Commit();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
ServerHelper.Error(e);
|
ServerHelper.Error(e);
|
||||||
Result = SQLResult.Fail;
|
Result = SQLResult.Fail;
|
||||||
|
Helper.Rollback();
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd.LastInsertedId;
|
return cmd.LastInsertedId;
|
||||||
@ -110,24 +118,18 @@ namespace Milimoe.FunGame.Server.Utility
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="Helper">MySQLHelper</param>
|
/// <param name="Helper">MySQLHelper</param>
|
||||||
/// <param name="cmd">命令对象</param>
|
/// <param name="cmd">命令对象</param>
|
||||||
/// <param name="trans">事务</param>
|
public static void PrepareCommand(MySQLHelper Helper, MySqlCommand cmd)
|
||||||
public static void PrepareCommand(MySQLHelper Helper, MySqlCommand cmd, MySqlTransaction? trans = null)
|
|
||||||
{
|
{
|
||||||
if (Helper.Connection != null)
|
if (Helper.Connection != null)
|
||||||
{
|
{
|
||||||
MySqlConnection? conn = Helper.Connection.Connection;
|
MySqlConnection? conn = Helper.Connection.Connection;
|
||||||
|
|
||||||
if (conn != null && conn.State != ConnectionState.Open)
|
if (conn != null)
|
||||||
conn.Open();
|
{
|
||||||
|
if (conn.State != ConnectionState.Open) conn.Open();
|
||||||
|
|
||||||
cmd.Connection = conn;
|
cmd.Connection = conn;
|
||||||
cmd.CommandText = Helper.Script;
|
cmd.CommandText = Helper.Script;
|
||||||
|
|
||||||
if (trans != null)
|
|
||||||
{
|
|
||||||
cmd.Transaction = trans;
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd.CommandType = Helper.CommandType;
|
cmd.CommandType = Helper.CommandType;
|
||||||
|
|
||||||
if (Helper.Parameters != null)
|
if (Helper.Parameters != null)
|
||||||
@ -140,4 +142,5 @@ namespace Milimoe.FunGame.Server.Utility
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user