publicstaticvoidRelease() { try { var id = GetId(); if (!_cache.ContainsKey(id)) return; Remove(id); } catch (Exception e) { throw e; } } privatestaticboolRemove(string id) { if (!_cache.ContainsKey(id)) returnfalse;
SqlClient client;
int index = 0; bool result = false; while (!(result = _cache.TryRemove(id, out client))) { index++; Thread.Sleep(20); if (index > 3) break; } return result; } privatestaticstringGetId() { var id = _threadLocal.Value; if (string.IsNullOrEmpty(id)) { thrownew Exception("内部错误: SqlSugarClient已丢失."); } return id; }
publicstaticvoidBeginTran() { var instance=GetInstance(); //开启事务 if (!instance.IsBeginTran) { instance.SqlSugarClient.Ado.BeginTran(); instance.IsBeginTran = true; } }
publicstaticvoidCommitTran() { var id = GetId(); if (!_cache.ContainsKey(id)) thrownew Exception("内部错误: SqlSugarClient已丢失."); if (_cache[id].TranCount == 0) { _cache[id].SqlSugarClient.Ado.CommitTran(); _cache[id].IsBeginTran = false; } }
publicstaticvoidRollbackTran() { var id = GetId(); if (!_cache.ContainsKey(id)) thrownew Exception("内部错误: SqlSugarClient已丢失."); _cache[id].SqlSugarClient.Ado.RollbackTran(); _cache[id].IsBeginTran = false; _cache[id].TranCount = 0; }
publicstaticvoidTranCountAddOne() { var id = GetId(); if (!_cache.ContainsKey(id)) thrownew Exception("内部错误: SqlSugarClient已丢失."); _cache[id].TranCount++; } publicstaticvoidTranCountMunisOne() { var id = GetId(); if (!_cache.ContainsKey(id)) thrownew Exception("内部错误: SqlSugarClient已丢失."); _cache[id].TranCount--; } }
using System.Reflection; using Autofac; using Autofac.Extras.DynamicProxy; using Module = Autofac.Module; publicclassBusinessModule : Module { protectedoverridevoidLoad(ContainerBuilder builder) { var business = Assembly.Load("FTY.Business"); builder.RegisterAssemblyTypes(business) .AsImplementedInterfaces().InterceptedBy(typeof(TransactionInterceptor)).EnableInterfaceInterceptors(); builder.RegisterType<TransactionInterceptor>(); } }