Skip to content

Commit

Permalink
585:DBFast 增加全局事务机制【最近因 Gemini.Workflow,因此对ORM实体这一块进行内部机制大升级】(2019-05…
Browse files Browse the repository at this point in the history
…-04)
  • Loading branch information
cyq1162 committed May 4, 2019
1 parent 7da9647 commit 60827b5
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 59 deletions.
10 changes: 10 additions & 0 deletions Action/MAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ public int RecordsAffected
}
}
/// <summary>
/// 是否事务进行中
/// </summary>
public bool IsTransation
{
get
{
return dalHelper.IsOpenTrans;
}
}
/// <summary>
/// Command Timeout[seconds]
///<para>命令超时设置[单位秒]</para>
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions Action/MProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ public int RecordsAffected
}
}
/// <summary>
/// 是否事务进行中
/// </summary>
public bool IsTransation
{
get
{
return dalHelper.IsOpenTrans;
}
}
/// <summary>
/// The database connection string
///<para>数据库链接字符串</para>
/// </summary>
Expand Down
52 changes: 5 additions & 47 deletions DAL/Conn/ConnObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,59 +85,17 @@ public void SetFocusOnMaster()
{
if (Slave.Count > 0)
{
string id = GetIdentity();//获取当前的标识
string id = StaticTool.GetMasterSlaveKey();//获取当前的标识
Cache.CacheManage.LocalInstance.Set(id, 1, AppConfig.DB.MasterSlaveTime / 60.0);
}
}
public bool IsAllowSlave()
{
if (Slave.Count == 0) { return false; }
string id = GetIdentity();//获取当前的标识
string id = StaticTool.GetMasterSlaveKey();//获取当前的标识
return !Cache.CacheManage.LocalInstance.Contains(id);
}
private string GetIdentity()
{
string id = string.Empty;
if (HttpContext.Current != null)
{
if (HttpContext.Current.Session != null)
{
id = HttpContext.Current.Session.SessionID;
}
else if (HttpContext.Current.Request["Token"] != null)
{
id = HttpContext.Current.Request["Token"];
}
else if (HttpContext.Current.Request.Headers["Token"] != null)
{
id = HttpContext.Current.Request.Headers["Token"];
}
else if (HttpContext.Current.Request["MasterSlaveid"] != null)
{
id = HttpContext.Current.Request["MasterSlaveid"];
}
if (string.IsNullOrEmpty(id))
{
HttpCookie cookie = HttpContext.Current.Request.Cookies["MasterSlaveid"];
if (cookie != null)
{
id = cookie.Value;
}
else
{
id = Guid.NewGuid().ToString().Replace("-", "");
cookie = new HttpCookie("MasterSlaveid", id);
cookie.Expires = DateTime.Now.AddMonths(1);
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
}
if (string.IsNullOrEmpty(id))
{
id = DateTime.Now.Minute + Thread.CurrentThread.ManagedThreadId.ToString();
}
return "MasterSlave_" + id;
}

}

internal partial class ConnObject
Expand Down Expand Up @@ -242,14 +200,14 @@ public static void CheckConnIsOk(object threadid)
ConnObject obj = connDicCache[key];
if (obj != null)
{
if (!obj.Master.IsOK)
if (!obj.Master.IsOK)
{
if (obj.Master.ConnName == obj.Master.ConnString)
{
connDicCache.Remove(key);//移除错误的链接。
continue;
}
obj.Master.TryTestConn();
obj.Master.TryTestConn();
}
if (obj.BackUp != null && !obj.BackUp.IsOK) { obj.BackUp.TryTestConn(); }
if (obj.Slave != null && obj.Slave.Count > 0)
Expand Down
10 changes: 10 additions & 0 deletions DAL/DalBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using CYQ.Data.Tool;
using System.Data.SqlTypes;
using System.Threading;
using CYQ.Data.Orm;


namespace CYQ.Data
Expand Down Expand Up @@ -478,6 +479,10 @@ public virtual bool AddParameters(string parameterName, object value, DbType dbT
{
parameterName = parameterName.Substring(0, 1) == Pre.ToString() ? parameterName : Pre + parameterName;
}
if (Com == null)
{
return false;
}
if (Com.Parameters.Contains(parameterName))//已经存在,不添加
{
return false;
Expand Down Expand Up @@ -789,6 +794,11 @@ internal void WriteError(string err)

public void Dispose()
{
string key = StaticTool.GetTransationKey(UsingConnBean.ConnName);
if (DBFast.HasTransation(key))
{
return;//全局事务由全局控制(全局事务会在移除key后重新调用)。
}
if (_con != null)
{
CloseCon();
Expand Down
43 changes: 40 additions & 3 deletions DAL/DalCreate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
using CYQ.Data.Tool;
using System.Threading;
using System.IO;
using CYQ.Data.Orm;


namespace CYQ.Data
{
/// <summary>
/// 数据库类型操作类
/// </summary>
internal class DalCreate
internal static class DalCreate
{
//private const string SqlClient = "System.Data.SqlClient";
//private const string OleDb = "System.Data.OleDb";
Expand All @@ -24,10 +25,48 @@ internal class DalCreate
//private const string XmlClient = "CYQ.Data.XmlClient";
//private const string XHtmlClient = "CYQ.Data.XHtmlClient";

/// <summary>
/// 全局存档,是为了用单例来实现全局事务。
/// </summary>
private static MDictionary<string, DalBase> dalBaseDic = new MDictionary<string, DalBase>();
public static DalBase Get(string key)
{
if (dalBaseDic.ContainsKey(key))
{
return dalBaseDic[key];
}
return null;
}
public static bool Remove(string key)
{
return dalBaseDic.Remove(key);
}
/// <summary>
/// 简单工厂(Factory Method)
/// </summary>
public static DalBase CreateDal(string connNameOrString)
{
string key = StaticTool.GetTransationKey(connNameOrString);
//检测是否开启了全局事务;
bool isTrans = DBFast.HasTransation(key);
if (isTrans)
{
if (dalBaseDic.ContainsKey(key))
{
return dalBaseDic[key];
}

}
DalBase dal = CreateDalBase(connNameOrString);
if (isTrans)
{
dal.TranLevel = DBFast.GetTransationLevel(key);
dal.IsOpenTrans = true;
dalBaseDic.Add(key, dal);
}
return dal;
}
private static DalBase CreateDalBase(string connNameOrString)
{
//ABCConn
DalBase db = GetDalBaseBy(ConnObject.Create(connNameOrString));
Expand All @@ -43,9 +82,7 @@ public static DalBase CreateDal(string connNameOrString)
}
}
return db;

}

private static DalBase GetDalBaseBy(ConnObject co)
{
DataBaseType dalType = co.Master.ConnDalType;
Expand Down
70 changes: 70 additions & 0 deletions Orm/DBFast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using CYQ.Data.Table;
using CYQ.Data.SQL;
using CYQ.Data.Tool;
using System.Web;
using System.Threading;
using System.Data;


namespace CYQ.Data.Orm
Expand All @@ -13,6 +16,73 @@ namespace CYQ.Data.Orm
/// </summary>
public static class DBFast
{
/// <summary>
/// 当前 用户 是否开启了全局事务
/// </summary>
/// <returns></returns>
internal static bool HasTransation(string key)
{
return TransationKeys.ContainsKey(key);
}
internal static IsolationLevel GetTransationLevel(string key)
{
if (TransationKeys.ContainsKey(key))
{
return TransationKeys[key];
}
return IsolationLevel.ReadCommitted;
}
/// <summary>
/// 存档事务的标识
/// </summary>
public static MDictionary<string, IsolationLevel> TransationKeys = new MDictionary<string, IsolationLevel>();
/// <summary>
/// 开启事务 (Web 状态下以(Session+线程ID)为单位,其它状态下仅以线程ID为单位)
/// 如果已存在事务(则返回false)
/// </summary>
public static bool BeginTransation(string conn)
{
return BeginTransation(conn, IsolationLevel.ReadCommitted);
}
public static bool BeginTransation(string conn, IsolationLevel level)
{
string key = StaticTool.GetTransationKey(conn);
if (!TransationKeys.ContainsKey(key))
{
TransationKeys.Add(key, level);
}
return false;
}
/// <summary>
/// 提交事务
/// </summary>
public static bool EndTransation(string conn)
{
string key = StaticTool.GetTransationKey(conn);
TransationKeys.Remove(key);
DalBase dal = DalCreate.Get(key);
if (dal != null && dal.EndTransaction())//如果事务回滚了,
{
dal.Dispose();
return DalCreate.Remove(key);
}
return false;
}
/// <summary>
/// 事务回滚
/// </summary>
public static bool RollBack(string conn)
{
string key = StaticTool.GetTransationKey(conn);
TransationKeys.Remove(key);
DalBase dal = DalCreate.Get(key);
if (dal != null && dal.RollBack())
{
dal.Dispose();
return DalCreate.Remove(key);
}
return false;
}
/// <summary>
/// 查找单条记录
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions Orm/OrmBaseInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public int RecordsAffected
}
}
/// <summary>
/// 是否事务进行中
/// </summary>
public bool IsTransation
{
get
{
return _Action.IsTransation;
}
}
/// <summary>
/// 获取 数据库的 表名
/// </summary>
public string TableName
Expand Down
2 changes: 1 addition & 1 deletion Orm/SimpleOrmBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ private void GetValueFromEntity()
/// </summary>
public void Dispose()
{
if (Action != null)
if (Action != null && !Action.IsTransation)//ORM的事务,由全局控制,不在这里释放链接。
{
Action.Dispose();
}
Expand Down
Loading

0 comments on commit 60827b5

Please sign in to comment.