MYSQL数据库如何多线程

MYSQL数据库如何多线程,第1张

1。通过线程的互斥来同步 *** 作数据库

2。数据库采用事务处理表中的数据

3。采用共享方式打开数据库,不是以独占方式打开数据库

建立一个mysql连接表加上一个

临界

,表结点是这样的(mysqlcon,bool),根据实际情况定大小。我用的是10个连接。

当要进行mysql *** 作时,就从表中取出一个闲置的mysql连接,并把bool量改为true,使用完后改成false,临界区的做用是保障一个mysql连接一次只能被一个线程使用。

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

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

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存