计时器和执行 [英] Timer and Execution

查看:94
本文介绍了计时器和执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 我有一个Windows服务.该服务具有一个计时器,该计时器每2秒调用一个类中的方法.该方法将一些数据从我的接口数据库导入到开发数据库,​​并将某些开发数据库导出到接口数据库. 导入后,另一种将导入的数据状态更新为'updated'的方法.同样,我具有5-8导入功能和3-4导出功能.每种功能都正在使用Transaction运行.

 I have a windows service. The service have a timer which calls a method in one class for every 2 seconds. The method imports some data from my interface database to development database and have export some development database to interface databases. After import, another method to update the imported data status as 'updated' . Likewise i have 5-8 import functionality and 3-4 export functionality. Each functionality is running using Transaction.

该服务正在生产中成功运行.但是我经常遇到像僵尸一样的错误.错误消息是" 此SqlTransaction已完成;它不再可用."

The service is runnning successfully in  production. But frequently i am facing the error like zombie problem. The error message is "This SqlTransaction has completed; it is no longer usable."

我正在分析为什么会发生这种情况,并在思考一种情况.我知道计时器中调用的方法在不同的线程中运行.所以我的疑问是当该方法执行2秒钟以上时,再次在另一个线程中调用该方法.所以第二 方法在事务中开始执行,第一个调用关闭连接?我不知道确切的问题.有可能吗?因此,我可以等到该方法执行后再次调用该方法吗?在这种情况下,请引导我.

I am analysing why it is happened and thinking one scenario. I knew that the methods called in the timer is running in a different thread. so my doubt is when the method is excuting more than 2 second, again method is called in another thread. So second method start excuting in the transaction, the first call closes the connection?  I dont know the exact problem. Any possibility for this? So can i wait until the method excute to call the method again? Please guid me for this scenario.

推荐答案

没有足够的信息来检测为什么会出现您的问题.但是错误消息通常是相对于两个线程之间的共享连接或共享连接上的回滚事务而发生的:

There is not enough information to detect why your problem occur. But The error message occurs often relative to  shared connection between two thread or roll back transaction on a shared connection:

通常,连接不是线程安全的(SqlConnection,MySqlConnection和OracleConnection特别提到它们不是线程安全的).不要在线程之间共享连接.只需为每个逻辑事务的范围创建一个新的连接 并让连接池负责其余的工作.

Generally connections are not thread safe(SqlConnection ,MySqlConnection and OracleConnection specifically mention that they are not thread safe).Don't share a connection between threads. Just create a new connection for the scope of each logical transaction and let the connection pooling take care of the rest.

最美好的祝愿!


这篇关于计时器和执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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