LINQ数据库连接对象制造工厂
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Threading;
namespace Data
{
/// <summary>
/// LINQ数据库连接对象制造工厂
/// </summary>
static internal class DataContextFactory
{
static System.Timers.Timer sysTimer = new System.Timers.Timer(10000);
volatile static Dictionary<Thread, DataContext[]> divDataContext = new Dictionary<Thread, DataContext[]>();
static public int DataContextCount { get { return divDataContext.Count; } }
static DataContextFactory()
{
sysTimer.AutoReset = true;
sysTimer.Enabled = true;
sysTimer.Elapsed += new System.Timers.ElapsedEventHandler(sysTimer_Elapsed);
sysTimer.Start();
}
static void sysTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
List<Thread> list = new List<Thread>();
foreach (Thread item in divDataContext.Keys)
{
if (item.ThreadState == ThreadState.Stopped)
{
list.Add(item);
}
}
for (int index = 0; index < list.Count; index++)
{
divDataContext.Remove(list[index]);
list[index] = null;
}
list = null;
}
/// <summary>
/// 通过工厂的制造模式获取相应的LINQ数据库连接对象
/// </summary>
/// <param name="dbName">数据库名称(需要与真实数据库名称保持一致)</param>
/// <returns>LINQ数据库连接对象</returns>
public static DataContext Intance(string dbName)
{
return Intance(dbName, Thread.CurrentThread);
}
/// <summary>
/// 通过工厂的制造模式获取相应的LINQ数据库连接对象
/// </summary>
/// <param name="dbName">数据库名称(需要与真实数据库名称保持一致)</param>
/// <param name="thread">当前线程引用的对象</param>
/// <returns>LINQ数据库连接对象</returns>
public static DataContext Intance(string dbName, Thread thread)
{
if (!divDataContext.Keys.Contains(thread))
{
divDataContext.Add(thread, new DataContext[4]);
}
if (dbName.Equals("zzl"))
{
if (divDataContext[thread][0] == null)
{
divDataContext[thread][0] = new Data.LinqzzlDataContext();
}
return divDataContext[thread][0];
}
else
{
return null;
}
}
}
}
LINQ数据库连接对象制造工厂的更多相关文章
- 重构版机房收费系统之分层、接口、数据库连接、反射+工厂(vb.net)
分层 分层是为了减少层与层之间的依赖,添加程序的可读性,让整个系统结构清晰明白.还可大大减少维护成本,可是分层也有一定的缺点,有些能够直接訪问数据库的层,却要通过负责訪问数据库的层进行訪问.这样,在訪 ...
- 获取数据库连接对象Connection
2018-11-04 19:50:52 开始写 public Connection getConn() {//返回类型为Connection try { Class.forName("co ...
- 获取数据库连接对象(线程ThreadLocal)
/** * 负责数据库连接定义的程序类 * 该类可以负责所有操作线程的数据库连接,利用get()方法可以获得连接对象 */ public class DatabaseConnection { priv ...
- JDBCToolsV2:利用ThreadLocal保证当前线程操作同一个数据库连接对象。
JDBCToolsV2: 利用ThreadLocal保证当前线程操作同一个数据库连接对象. package com.dgd.test; import com.alibaba.druid.poo ...
- JDBC——Connection数据库连接对象
功能 1.获取执行SQL的对象 方法:createStatement() 用于创建向数据库发送SQL语句的一个对象.修饰/返回值类型:Statement(声明) 方法:prepareStatement ...
- 数据库学习任务二:数据库连接对象SqlConnection
数据库应用程序的开发流程一般主要分为以下几个步骤: 创建数据库 使用Connection对象连接数据库 使用Command对象对数据源执行SQL命令并返回数据 使用DataReader和DataSet ...
- 《LINQ技术详解C#》-4.LINQ到对象
public static string[] Presidents { get; } = { "Adams", "Arthur", "Buchanan ...
- Linq复杂对象查询
复杂的查询对象, using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- js面向对象(对象/类/工厂模式/构造函数/公有和原型)
https://www.cnblogs.com/sandraryan/ 什么是对象 js中一切都是对象(有行为和特征).js允许自定义对象,也提供了内建对象(string date math等) 对象 ...
随机推荐
- 【转】Android bluetooth介绍(三): 蓝牙扫描(scan)设备分析
原文网址:http://blog.csdn.net/xubin341719/article/details/38584469 关键词:蓝牙blueZ A2DP.SINK.sink_connect.s ...
- C#中类的默认访问是私有的
如果类前面没有访问修饰符的话,那里面的成员默认是私有private. 下面的代码是从MSDN上拷的,那个d它说明了私有:class Employee{private int i;double d; / ...
- 将批量下载的博客导入到手机后,通过豆约翰博客阅读器APP(Android手机)进行浏览,白字黑底,保护眼睛,图文并茂。
首先下面演示的博文来自于以下地址:http://www.douban.com/note/423939291/ 需要先通过博客备份专家将导出的博文导入到手机(还不会用的朋友请先阅读http://www. ...
- Normal Vector Using WorldInverseTranspose
shader里面经常看到normal向量是用WorldInverseTranspose矩阵做变换的,有时候也可以用WorldMatrix变换. 原理: If your object is only e ...
- 作品第二课----点击DIV显示其内容
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python进度1
Python 错误和异常 异常参数: 3.4与2.7有些不同 3.4中 try: x except NameError as e: print(type(e)) print(e) 运行结果: < ...
- oracle查看表锁及解锁
--kill session语句 1 alter system kill SESSION '2171,60490'; --以下几个为相关表 1 2 3 4 5 6 7 SELECT * FROM v$ ...
- Windows不能在本地计算机启动OracleDBConsoleorcl .错误代码2
Windows 不能在 本地计算机 启动 OracleDBConsoleorcl.有关更多信息,查阅系统事件日志.如果这是非 Microsoft 服务,请与服务厂商联系,并参考特定服务错误代码 2. ...
- 5 approach to load UIView from Xib
After the past few years I found that the only manageable way for creating/maintaining view (or any ...
- 使用GruntJS链接与压缩多个JavaScript文件
使用GruntJS链接与压缩多个JavaScript文件 自己写了个简单的HTML5 Canvas的图表库,可以支持饼图,折线图,散点图,盒子图 柱状图,同时支持鼠标提示,绘制过程动画效果等.最终我想 ...