[已解决问题] 当iBATIS遇到Timer
提问时间: 2007-11-12 11:21
悬赏分:20 浏览:519 次
为了在程序中实现定时任务,我在Global.asax里面加了一个System.Threading.Timer类的实例,然后在 Application_Start()中将其初始化,开始还没问题,前两天遇到了一个奇怪的异常: WebSessionStore: Could not obtain reference to HttpContext.

在网上也没找到解决方案,最近也没时间去仔细看源码,搞不懂在Web项目中如何结合使用iBATIS和Timer.
提问者:Anders Cui - 小虾三级

问题补充:看了 没剑 的回答,得到一点提示,可以通过静态变量来保存HttpContext,在使用一个Timer时是没问题的,但使用两个Timer时却有问题了,不知为何。

最佳答案
在新的线程里不能操作IIS线程...我以前也碰到过,我是用多线程来更新网站的在线人数,你可以参考一下我的代码:
Imports System.Data.SqlClient
Imports System.Threading
Imports System.Runtime.InteropServices
Imports System.Web

Public Class AutoUpdate
    Protected appHandle As GCHandle
    Public Sub New()
        'If System.Web.HttpContext.Current.Application("UpdateTime") Is Nothing Then
        '初始化
        appHandle = GCHandle.Alloc(System.Web.HttpContext.Current, GCHandleType.Normal)
        Dim th As New Thread(New ThreadStart(AddressOf startU))
        th.IsBackground = True
        th.Start()
    End Sub


#Region "更新操作"
    Public Sub startU()
        Dim context As HttpContext = CType(appHandle.Target, System.Web.HttpContext)
        Dim App As HttpApplicationState = context.Application
        Dim Match As New Match
        Dim Common As New Common
        While True
            App.Lock()
            Try
                Dim OnlineUserList As List(Of OnlineUser) = CType(App("OnLineUser"), List(Of OnlineUser))
                If OnlineUserList IsNot Nothing Then
                    If OnlineUserList.Count > 0 Then
                        Dim Temp() As OnlineUser = OnlineUserList.ToArray
                        Dim OnlineRobotList As Hashtable = Nothing
                        Try
                            OnlineRobotList = CType(App("OnLineRobot"), Hashtable)  '已在线的机器人列表
                        Catch ex As Exception
                            OnlineRobotList = New Hashtable(100)
                        End Try
                        For Each user As OnlineUser In Temp
                            If user IsNot Nothing Then
                                'If Match.IsRobot(user.UserName) = False Then
                                If DateDiff(DateInterval.Minute, user.OnlineTime, Now) > 30 Then  '10分钟就算超时
                                    Dim Finder As New Finder(user.UserName)
                                    Dim c A
2007/11/12 12:22:13 回答者:没剑


提问者对于答案的评价:本质上应该是因为Timer处于新线程中而导致的问题,谢谢没剑!
评论
7个月前   wingoo :
解决了么??
我也遇到了这个问题...-__-

这边有个解决方法,不过我没试,,因为用了castle,,还要改源码-___-
你可以尝试下..
http://www.mail-archive.com/user-cs@ibatis.apache.org/msg02052.html

另外在ibatis mapper的帮助里也有这一段
The DatatMapper component store his working ISqlMapSession on different session store. 

On Web environnement, the session is stored Http.Request.Items.


On windows environnement on the current thread. 


You can configure the session storage by specifing the property ISessionStore on ISqlMapper.

This will allow to set a custom session store like the HybridWebThreadSessionStore This is used for scenarios where most of the you need per request session, but you also does some work outside a request (in a thread pool thread, for instance).

Set it after the configuration and before use of the ISqlMapper.

Example 4.12. Custom ISessionStore

sqlMapper.SessionStore = new HybridWebThreadSessionStore( sqlMapper.Id );
...
sqlMapper.QueryOject(...)



7个月前   wingoo :
加上那句后,解决问题,lz可以试下:)

调度程序我用的是quartz,,园子里的shanyou有个专题介绍
也可以看下^_^
   您需要登录以后才能回答!
我的问题    我要提问


快到期问题

> 问题排行榜

有不合适内容,建议去除