我有一个使用实体框架和MySQL数据库的ASP.NET Web应用程序。我正在做一些动态表的工作,所以采取了写入原始SQL。有什么我需要添加,以使此代码线程安全?此代码可以并且将被我的系统的每个用户非常频繁地调用。它总是打电话给INFORMATION_SCHEMA
那个错误。零星“目前不支持多个同时连接”
这是我收到相当经常试图调用AddMessageAction
(但不是每次)错误:
UserActionModel:CheckTableExists ex: The underlying provider failed on Open..
4/23/2013 9:00:24 AM: UserActionModel:CheckTableExists inner ex: System.NotSupportedException: Multiple simultaneous connections or connections with different connection strings inside the same transaction are not currently supported.
at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception)
at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)
at MySql.Data.MySqlClient.MySqlConnection.Open()
at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure).
,代码:
public static class UserActionModel
{
private const string databaseName = "chat";
public static string CheckTableExists(long _userId)
{
string tableName = null;
List<long> tableExists;
try
{
string date = DateTime.Now.ToUniversalTime().Year.ToString() + DateTime.Now.ToUniversalTime().Month.ToString("D2") + DateTime.Now.ToUniversalTime().Day.ToString("D2");
tableName = "user_actions_" + date + "_" + _userId;
string sql = "SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_NAME = '" + tableName + "' AND TABLE_SCHEMA = 'archive'";
using (var readContext = new ArchiveConnector())
{
tableExists = readContext.Database.SqlQuery<long>(sql).ToList();
readContext.Database.Connection.Close();
}
if (tableExists.Any(tableExist => tableExist == 0))
{
sql = "CREATE TABLE IF NOT EXISTS `" + tableName + "` ("
+ "`id` bigint(20) NOT NULL AUTO_INCREMENT, "
+ "`user_id` bigint(20) NOT NULL, "
+ "`user_device_id` bigint(20) NOT NULL, "
+ "`coordinate_address_id` bigint(20) NOT NULL, "
+ "`message_id` bigint(20) DEFAULT NULL, "
+ "`created` datetime NOT NULL, "
+ "PRIMARY KEY (`id`), "
+ "KEY `user_id` (`user_id`), "
+ "KEY `user_device_id` (`user_device_id`), "
+ "KEY `coordinate_address_id` (`coordinate_address_id`), "
+ "KEY `message_id` (`message_id`); "
using (var writeContext = new ArchiveConnector())
{
writeContext.Database.ExecuteSqlCommand(sql);
writeContext.SaveChanges();
writeContext.Database.Connection.Close();
}
}
}
catch (Exception ex)
{
Logger.LogMessage("UserActionModel:CheckTableExists ex: " + ex.Message);
if (ex.InnerException != null)
Logger.LogMessage("UserActionModel:CheckTableExists inner ex: " + ex.InnerException);
tableName = null;
}
return tableName;
}
public static void AddMessageAction(long _messageId, long _userId, long _userDeviceId, long _coordinateAddressId)
{
try
{
string tableName = CheckTableExists(_userId);
if (String.IsNullOrEmpty(tableName)) return;
using (var dataContext = new ArchiveConnector())
{
string sql = "INSERT INTO " + tableName + " "
+ "(user_id, user_device_id, coordinate_address_id, message_id, created) "
+ "VALUES "
+ "(" + _userId + ", " + _userDeviceId + ", " + _coordinateAddressId + ", " + _messageId + ", NOW());";
dataContext.Database.ExecuteSqlCommand(sql);
dataContext.SaveChanges();
}
}
catch (Exception ex)
{
Logger.LogMessage("UserActionModel:AddMessageAction (" + _messageId + ") ex: " + ex.Message);
}
}
}
从我的web.config连接字符串中删除persist security info=True
做没有区别(我读到这是一个可能的解决方案)。
UDPATE:从连接器6.6.4更新到6.6.5。不用找了。 UDPATE:从连接器6.6.5更新为6.7.1 Alpha。不用找了。
我以为'using'块的结束会自动处理所有的Dispose调用?从MSDN:'你在using语句中创建一个实例,以确保当using语句退出时,在对象上调用Dispose。 – Julien 2013-04-23 15:07:25