Class:DbConnectionManipulator.cs
ylbtech-Class:DbConnectionManipulator.cs |
1.返回顶部 |
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks; namespace DS.JZB.Common {
public abstract class DbConnectionManipulator {
public static string DatabaseConnectionStringName = "DatabaseCs";
public static string IndexDatabaseConnectionStringName = "DatabaseCs";
public const int SplitDatabaseCount = ; protected IDbConnection GetConnection() {
return GetMainDatabaseConnection();
} protected void Run(Action<IDbConnection> work) {
Run(GetConnection(), work);
} protected void Run(IDbConnection connection, Action<IDbConnection> work) {
using (connection) {
work(connection);
}
} protected void RunInTransaction(Action<IDbConnection, IDbTransaction> work) {
RunInTransaction(GetConnection(), work);
} protected void RunInTransaction(IDbConnection connection, Action<IDbConnection, IDbTransaction> work) {
using (connection) {
using (var transaction = connection.BeginTransaction()) {
try {
work(connection, transaction);
transaction.Commit();
}
catch {
transaction.Rollback();
throw;
}
}
}
} public static IDbConnection GetMainDatabaseConnection(string databaseCs = "") {
//if (string.IsNullOrEmpty(databaseCs)) {
// if (UserContext.CurrentUser != null && UserContext.CurrentUser.GlobalTeamId > 0) {
// var teamId = UserContext.CurrentUser.GlobalTeamId;
// DatabaseConnectionStringName = "DatabaseCs" + (teamId % SplitDatabaseCount + 1);
// }
// else {
// DatabaseConnectionStringName = "DatabaseCs";
// }
//}
//else {
// DatabaseConnectionStringName = databaseCs;
//} var connection =
new SqlConnection(ConfigurationManager.ConnectionStrings[DatabaseConnectionStringName].ConnectionString);
connection.EnsureOpen();
return connection;
} public static void RunMainDatabase(Action<IDbConnection> work, string databaseCs = "") {
RunMainDatabase(GetMainDatabaseConnection(databaseCs), work);
} public static T RunMainDatabase<T>(Func<IDbConnection, T> work, string databaseCs = "") {
return RunMainDatabase(GetMainDatabaseConnection(databaseCs), work);
} private static void RunMainDatabase(IDbConnection connection, Action<IDbConnection> work) {
using (connection) {
work(connection);
}
} private static T RunMainDatabase<T>(IDbConnection connection, Func<IDbConnection, T> work) {
using (connection) {
return work(connection);
}
} public static void RunMainDatabaseInTransaction(Action<IDbConnection, IDbTransaction> work,
string databaseCs = "") {
RunMainDatabaseInTransaction(GetMainDatabaseConnection(databaseCs), work);
} public static T RunMainDatabaseInTransaction<T>(Func<IDbConnection, IDbTransaction, T> work,
string databaseCs = "") {
return RunMainDatabaseInTransaction(GetMainDatabaseConnection(databaseCs), work);
} public static void RunMainDatabaseInTransaction(IDbConnection connection,
Action<IDbConnection, IDbTransaction> work) {
#if DEBUG
//var guid = Guid.Empty;
//if (HttpContext.Current.Items.Contains("DUUID")) {
// guid = (Guid)HttpContext.Current.Items["DUUID"];
//}
//else {
// guid = Guid.NewGuid();
// HttpContext.Current.Items["DUUID"] = guid;
//} //var logger = typeof(DbConnectionManipulator).ObtainLog();
//logger.Debug($"[{guid.ToString("n")}] get connection to execute db queries");
#endif using (connection) {
using (var transaction = connection.BeginTransaction()) {
try {
work(connection, transaction);
transaction.Commit();
}
catch(Exception ex) {
transaction.Rollback();
throw;
}
}
} #if DEBUG
//logger.Debug($"[{guid.ToString("n")}] release connection to connection pool");
#endif
} public static T RunMainDatabaseInTransaction<T>(IDbConnection connection,
Func<IDbConnection, IDbTransaction, T> work) {
#if DEBUG
//var guid = Guid.Empty;
//if (HttpContext.Current.Items.Contains("DUUID")) {
// guid = (Guid)HttpContext.Current.Items["DUUID"];
//}
//else {
// guid = Guid.NewGuid();
// HttpContext.Current.Items["DUUID"] = guid;
//} //var logger = typeof(DbConnectionManipulator).ObtainLog();
//logger.Debug($"[{guid.ToString("n")}] get connection to execute db queries");
#endif T result = default(T); using (connection) {
using (var transaction = connection.BeginTransaction()) {
try {
result = work(connection, transaction);
transaction.Commit();
}
catch {
transaction.Rollback();
throw;
}
}
} #if DEBUG
//logger.Debug($"[{guid.ToString("n")}] release connection to connection pool");
#endif return result;
} public static void RunAllSplitDatabase(Action<IDbConnection> work) {
for (var i = ; i <= SplitDatabaseCount; i++) {
RunMainDatabase(work, $"DatabaseCs{i}");
}
} public static void ForEachSplitDatabase(Func<IDbConnection, bool> work) {
for (var i = ; i <= SplitDatabaseCount; i++) {
if (RunMainDatabase(work, $"DatabaseCs{i}")) {
break;
}
}
} public static int GetTeamDatabaseIndex(int teamId) {
return teamId % SplitDatabaseCount + ;
} public static string GetTeamDatabaseName(int teamId) {
return $"DatabaseCs{(teamId % SplitDatabaseCount + 1)}";
} public static string GetIndexDatabaseName() {
return "DatabaseCs";
} protected Task RunAsync(Func<IDbConnection, Task> work) {
return RunAsync(GetConnection(), work);
} protected Task RunAsync(IDbConnection connection, Func<IDbConnection, Task> work) {
#if DEBUG
//var guid = Guid.Empty;
//if (HttpContext.Current.Items.Contains("DUUID")) {
// guid = (Guid)HttpContext.Current.Items["DUUID"];
//}
//else {
// guid = Guid.NewGuid();
// HttpContext.Current.Items["DUUID"] = guid;
//} //var logger = typeof(DbConnectionManipulator).ObtainLog();
//logger.Debug($"[{guid.ToString("n")}] get connection to execute db queries");
#endif Task result = null; using (connection) {
result = work(connection);
connection.Dispose();
} #if DEBUG
//logger.Debug($"[{guid.ToString("n")}] release connection to connection pool");
#endif return result;
} public static Task RunMainDatabaseAsync(Func<IDbConnection, Task> work) {
return RunMainDatabaseAsync(GetMainDatabaseConnection(), work);
} private static async Task RunMainDatabaseAsync(IDbConnection connection, Func<IDbConnection, Task> work) {
#if DEBUG
//var guid = Guid.Empty;
//if (HttpContext.Current.Items.Contains("DUUID")) {
// guid = (Guid) HttpContext.Current.Items["DUUID"];
//}
//else {
// guid = Guid.NewGuid();
// HttpContext.Current.Items["DUUID"] = guid;
//} //var logger = typeof(DbConnectionManipulator).ObtainLog();
//logger.Debug($"[{guid.ToString("n")}] get connection to execute db queries");
#endif using (connection) {
await work(connection);
connection.Dispose();
} #if DEBUG
//logger.Debug($"[{guid.ToString("n")}] release connection to connection pool");
#endif
}
} public static class DbConnectionExtension {
public static void EnsureOpen(this IDbConnection @this) {
if (@this.State == ConnectionState.Closed) {
@this.Open();
}
}
}
}
2.返回顶部 |
3.返回顶部 |
4.返回顶部 |
5.返回顶部 |
6.返回顶部 |
作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
Class:DbConnectionManipulator.cs的更多相关文章
- ASP.NET MVC:UrlHelper.cs
ylbtech-funcation-Utility: ASP.NET MVC:UrlHelper.cs 充当表示 ASP.NET Razor 页的类的基类. 1.UrlHelper 类返回顶部 1-1 ...
- ASP.NET MVC:WebPageBase.cs
ylbtech-funcation-Utility: ASP.NET MVC:WebPageBase.cs 充当表示 ASP.NET Razor 页的类的基类. 1.A,WebPageBase 抽象类 ...
- ASP.NET MVC:WebPageRenderingBase.cs
ylbtech-funcation-Utility: ASP.NET MVC:WebPageRenderingBase.cs 提供用于呈现使用 Razor 视图引擎的页的方法和属性. 1.A,WebP ...
- ASP.NET MVC:WebViewPage.cs
ylbtech-funcation-Utility: ASP.NET MVC:WebViewPage.cs 表示呈现使用 ASP.NET Razor 语法的视图所需的属性和方法. 1.A,WebVie ...
- 基本教程篇--第一节:InitialSampleDemo.cs介绍
第一节:InitialSampleDemo.cs介绍 为了讲解方便,我先附上源代码和效果图. 代码如下: using System; using System.Drawing; ...
- C# Json反序列化 C# 实现表单的自动化测试<通过程序控制一个网页> 验证码处理类:UnCodebase.cs + BauDuAi 读取验证码的值(并非好的解决方案) 大话设计模式:原型模式 C# 深浅复制 MemberwiseClone
C# Json反序列化 Json反序列化有两种方式[本人],一种是生成实体的,方便处理大量数据,复杂度稍高,一种是用匿名类写,方便读取数据,较为简单. 使用了Newtonsoft.Json,可以自 ...
- .Net魔法堂:AssemblyInfo.cs文件详解
一.前言 .net工程的Properties文件夹下自动生成一个名为AssemblyInfo.cs的文件,一般情况下我们很少直接改动该文件.但我们实际上通过另一个形式操作该文件.那就是通过在鼠标右键点 ...
- .NET Core 项目经验总结:Startup.cs 介绍(二)
原文地址(个人博客):http://www.gitblogs.com/Blogs/Details?id=643c9664-dc4e-42cf-a15f-5368ebfcd7c9 第一次面对 Start ...
- 架构风格:万金油CS与分层
计算机科学家David Wheele曾说过这么一句话: All problems in computer science can be solved by another level of indir ...
随机推荐
- 远程连接mongodb服务器
- Centos 6.5 本地局域网基于HTTP搭建YUM
服务端配置 init 3 文本 init5 图形 init 0 关机 init 1 重启 ls 查看 mkdir创建文件 关闭防火墙service iptables stop chkconfig ...
- jquery的div局部刷新
jquery的div局部刷新 //div的局部刷新 $(".dl").load(location.href+" .dl"); 全页面的刷新方法 window.l ...
- 025 SSM简单搭建
参考了同事的文档,自己也写一篇文档. 同时,补充了一下,程序是如何运行的. 一:SSM框架 1.说明 SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC ...
- IDEA设置
一:代码提示 二:自动导入
- python中@staticmethod、@classmethod和实例方法
1.形式上的异同点: 在形式上,Python中:实例方法必须有self,类方法用@classmethod装饰必须有cls,静态方法用@staticmethod装饰不必加cls或self,如下代码所示: ...
- 20165235 学习基础和C语言基础调查
20165235 学习基础和C语言基础调查 首先第一个问题:你有什么技能比大多人(超过90%以上)更好?感觉很难回答这种问题,其实我对很多东西挺感兴趣的,如果非要拿出一种比较突出的技能的话我感觉就是象 ...
- Java字符串处理
代码: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s ...
- RESTful架构&简单使用Django rest framework
RESTful架构 1 什么是REST REST全称是Representational State Transfer,中文意思是表述性状态转移. 它首次出现在2000年Roy Fielding的博士论 ...
- 在控制台下玩玩dotnet core内置原生的DI
转载请注明出处:http://www.cnblogs.com/zhiyong-ITNote/ 在基于dotnet core的web开发中,我们会经常用到DI,那么如果单单使用dotnet core自身 ...