多线程 连接数据库,C#多线程写数据库

多线程 连接数据库,C#多线程写数据库,第1张

多线程连接数据库的连接池类:

public static class ConnectionPool

{

private static object locker = new object()

private static Dictionary<string, SqlConnection>Connections = null

public static SqlConnection GetConnection<T>() where T : class, new()

{

string databaseName = NA.Common.Extensions.GetDatabaseName<T>()

if (string.IsNullOrEmpty(databaseName))

return null

if (Connections == null)

{

lock (locker)

{

Connections = new Dictionary<string, SqlConnection>()

}

}

string connKey = FindFreeSqlConnection(databaseName)

if (connKey != null)

return Connections[connKey]

else

{

string strconn = NA.Common.Extensions.GetConnectionString<T>()

int poolSize = NA.Common.Extensions.GetConnectionPoolSize<T>()

lock (locker)

{

for (int i = 0i <poolSize++i)

{

SqlConnection conn = new SqlConnection(strconn)

conn.Open()

Connections.Add(databaseName + "_" + i.ToString(), conn)

conn.Close()

}

}

return Connections[FindFreeSqlConnection(databaseName)]

}

}

private static string FindFreeSqlConnection(string databaseName)

{

IEnumerable<string>connKeys = Connections.Keys.Where(item =>item.StartsWith(databaseName))

if (connKeys != null &&connKeys.Count() >0)

{

foreach (string key in connKeys)

{

if (Connections[key].State == ConnectionState.Closed)

return key

}

}

return null

}

}

附加上其中用到的三个方法:

internal static int GetConnectionPoolSize<T>() where T : class, new()

{

string database = GetDatabaseName<T>()

string[] poolSizeArray = ConfigurationManager.AppSettings["ConnectionsPoolSize"].Split('|')

if (poolSizeArray != null)

{

foreach (string sizeItem in poolSizeArray)

{

string[] sizeItemArray = sizeItem.Split(':')

if (database == sizeItemArray[0])

return int.Parse(sizeItemArray[1])

}

}

return 50

}

public static string GetConnectionString<T>() where T : class, new()

{

string tableName = GetTableName<T>()

string[] databaseArray = ConfigurationManager.AppSettings["DatabaseArray"].Split('|')

if (databaseArray != null)

{

foreach (string database in databaseArray)

{

string tableNameList = ConfigurationManager.AppSettings[database]

string[] tables = ConfigurationManager.AppSettings[database].Split('|')

if (tables != null &&tables.Length >0)

if (tables.Contains(tableName))

return ConfigurationManager.ConnectionStrings[database].ConnectionString

}

}

return string.Empty

}

public static string GetDatabaseName<T>() where T : class, new()

{

string tableName = GetTableName<T>()

string[] databaseArray = ConfigurationManager.AppSettings["DatabaseArray"].Split('|')

if (databaseArray != null)

{

foreach (string database in databaseArray)

{

string tableNameList = ConfigurationManager.AppSettings[database]

string[] tables = ConfigurationManager.AppSettings[database].Split('|')

if (tables != null &&tables.Length >0)

if (tables.Contains(tableName))

return database

}

}

return string.Empty

}

先检查一下datareader是不是一定在用完就关了(最好用using来处理),或者在程序中就不要用datareader来读取数据,换成其它的dataset之类的,如果一定要用datareader可以用lock之类的锁定代码段,另外在高版本的sql server上也支持一个连接下多个datareader,见http://hi.baidu.com/dxawddd/item/5b6b2c0aba6ac98a03ce1b75

加个判断

if (SqlHelper.connection.State == System.Data.ConnectionState.Closed)

SqlHelper.connection.Open()

else  (SqlHelper.connection.State == System.Data.ConnectionState.Broken)

{

SqlHelper.connection.Close()

SqlHelper.connection.Open()

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9971208.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存