Sql Compact 随机产生 AccessViolationException [英] Sql Compact randomly produces AccessViolationException

查看:33
本文介绍了Sql Compact 随机产生 AccessViolationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多线程应用程序中运行 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:

AccessViolationException 试图读或写受保护的内存.这个通常表明其他内存损坏.

AccessViolationException Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

在System.Data.SqlServerCe.SqlCeCommand.CompileQueryPlan()在System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior行为,字符串方法,ResultSetOptions 选项)在System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery()在SqlCompactTest.TransactedCommandGroupExecutionTest.Test()在D:ProjectsPlayGroundSqlCompactTestSqlCompactTestTransactedCommandGroupExecutionTest.cs:line53 在SqlCompactTest.ExecutionTest.RunTest()在D:ProjectsPlayGroundSqlCompactTestSqlCompactTestExecutionTest.cs:line60 在SqlCompactTest.ExecutionTest.TimerElapsed(Object发件人,ElapsedEventArgs e) 在D:ProjectsPlayGroundSqlCompactTestSqlCompactTestExecutionTest.cs:line68 在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:ProjectsPlayGroundSqlCompactTestSqlCompactTestTransactedCommandGroupExecutionTest.cs:line 53 at SqlCompactTest.ExecutionTest.RunTest() in D:ProjectsPlayGroundSqlCompactTestSqlCompactTestExecutionTest.cs:line 60 at SqlCompactTest.ExecutionTest.TimerElapsed(Object sender, ElapsedEventArgs e) in D:ProjectsPlayGroundSqlCompactTestSqlCompactTestExecutionTest.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.

我该如何解决这个问题?

How can I solve this?

推荐答案

仅仅因为你设置了一个连接到 command.Connection 并不意味着它被设置了.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆