【发布时间】:2012-04-03 23:22:11
【问题描述】:
我有一个从 DataTable 运行 Parral.Foreach 的应用程序。在该 Parallel(3-10 并行)内,该类执行更新或选择语句。我有一个 MySQL 连接,{get;set;} 见下文。
public static MySqlConnection Connection { get; set; }
public static MySqlConnection OpenCon(string ServerAddress,int PortAddress, string UserID, string Password,int ConnectionTimeOut)
{
MySqlConnection masterOpenCON = new MySqlConnection("server=" + ServerAddress + ";Port=" + PortAddress + ";UID=" + UserID + ";PASSWORD=" + Password + ";connectiontimeout="+ConnectionTimeOut+";");
masterOpenCON.Open();
return masterOpenCON;
}
这是我的平行线
Parallel.ForEach(urlTable.AsEnumerable(), drow =>
{
WebSiteCrawlerClass WCC = new WebSiteCrawlerClass();
if (drow.ItemArray[0].ToString().Contains("$"))
{
WCC.linkGrabberwDates(drow.ItemArray[0].ToString(), "www.as.com");
}
});
现在在 WCC.LinkGrabberwDates 中像这样执行一个 mysql 命令
string mysql_Update = "update trad_live" + StaticStringClass.tableID + " set price = '"+priceString+"',LastProcessDate = Now() where ListingID = 'AT"+ IDValue+"'";
MySQLProcessing.MySQLProcessor.MySQLInsertUpdate(mysql_UpdateExistingr,"mysql_UpdateExisting");
这里是 MySQLInsertUpdate
public static void MySQLInsertUpdate(string MySQLCommand,string mysqlcommand_name)
{
try
{
MySqlCommand MySQLCommandFunc = new MySqlCommand(MySQLCommand, Connection);
MySQLCommandFunc.CommandTimeout = 240000;
MySQLCommandFunc.ExecuteNonQuery();
}
catch (Exception ex)
{
}
}
我关心的两件事是性能和数据完整性。我知道添加锁会降低性能,但会提高数据完整性。
我不希望创建 10 多个与服务器的连接,所以我的问题是这样的。
在上面的代码中,每个 sql 语句或公共 void MySQLInsertUpdate 中,锁实际上应该放在哪里。我的下一个问题是,除了每个线程的锁定/附加连接之外,还有更好的方法吗?
我确实意识到这目前是静态的,但我正在更改静态状态
【问题讨论】: