问题描述
我正在多线程应用程序中运行Sql Server Compact Edition 3.5.1.0 SP1。该应用程序在事务中随机运行插入查询。对于短交易,它可以正常工作。但是,当事务变长而执行之间的延迟变短,或者当我在调试模式下运行应用程序时,SqlCE开始随机抛出以下异常:
I am running Sql Server Compact Edition 3.5.1.0 SP1 in a multi-thread application. The application randomly runs insert queries in transactions. With short transactions, it works fine. But when the transactions get longer and delay between executions get shorter or when I run the application in debug mode, SqlCE begins to throw the following exception randomly:
在
处System.Data.SqlServerCe.SqlCeCommand.CompileQueryPlan()
处的
System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior
行为,字符串方法,
ResultSetOptions选项)位于
System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery ()
在
SqlCompactTest.TransactedCommandGroupExecutionTest.Test()
在
中D:\Projects\PlayGround\SqlCompactTest\SqlCompactTest\TransactedCommandGroupExecutionTest.cs:line
53 at
SqlCompactTest.ExecutionTest.RunTest()
在
中D:\Projects\PlayGround\SqlCompactTest\SqlCompactTest\ExecutionTest.cs:line
60在
中的
SqlCompactTest.ExecutionTest.TimerElapsed(Object
sender,ElapsedEventArgs e)中D:\Projects\PlayGround\SqlCompactTest\SqlCompactTest\ExecutionTest.cs:line
68 at
System.Timers.Timer.MyTimerCallback(对象
状态)
at System.Data.SqlServerCe.SqlCeCommand.CompileQueryPlan() at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options) at System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery() at SqlCompactTest.TransactedCommandGroupExecutionTest.Test() in D:\Projects\PlayGround\SqlCompactTest\SqlCompactTest\TransactedCommandGroupExecutionTest.cs:line 53 at SqlCompactTest.ExecutionTest.RunTest() in D:\Projects\PlayGround\SqlCompactTest\SqlCompactTest\ExecutionTest.cs:line 60 at SqlCompactTest.ExecutionTest.TimerElapsed(Object sender, ElapsedEventArgs e) in D:\Projects\PlayGround\SqlCompactTest\SqlCompactTest\ExecutionTest.cs:line 68 at System.Timers.Timer.MyTimerCallback(Object state)
我正在运行的代码是:
IDbConnection connection = m_connectionProvider.GetConnection(); // Just returns new connection
connection.Open();
IDbTransaction transaction = connection.BeginTransaction();
foreach (IDbCommand command in m_commands)
{
command.Connection = connection;
command.ExecuteNonQuery(); // This line throws exception
Thread.Sleep((int)m_delayBetweenExecutions);
}
transaction.Commit();
connection.Close();
此代码在两个线程中同时运行。连接字符串为:
This code is running simultaneously in two threads. Connection string is:
"Data Source=testDB.sdf;Encrypt Database=True;Password=test;File Mode=Read Write;Persist Security Info=False;Max Database Size=1024"
我在Internet上发现了荒谬的解决方案,例如还原回到Framework 1.1,更改方法参数的顺序,启用/禁用优化等,但是它们都不对我有用。我还通过,但找不到适合我的解决方案。我的数据库文件版本为3.5.0.0。
I found absurd solutions on Internet like reverting back to Framework 1.1, changing the order of method arguments, enabling/disabling optimizations etc. but none of them worked for me. I also iterated through possible solutions on microsoft sites but I could not find a solution that works for me. My database file version is 3.5.0.0.
我该如何解决?
推荐答案
仅仅是因为您为command设置了连接。连接并不意味着已设置它。
Just because you set a connection to command.Connection does not mean it is set.
command.Connection = connection;
command.Connection.GetHashCode() == connection.GetHashCode(); // may return false.
这是问题的原因。可以通过使用connection.CreateCommand();
This is the cause of the problem. Can be corrected by using connection.CreateCommand();
这篇关于SQL Compact随机产生AccessViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!