首页 > 代码库 > mybatisnet Web环境下多线程问题

mybatisnet Web环境下多线程问题

 

    最近在Web环境下使用mybatis,在Winform和服务模式下运行(多线程访问数据库)没有问题。但是在IIS的环境中出现了一个WebSessionStore: Could not obtain reference to HttpContext 异常。

之前有听说过mybatis在多线程模式下会有异常,不过一直没有遇到过.以为是老版本的问题,也没在意。直到今天遇到这个问题才想起这么个事,首先问度娘,找到了这篇文章。感谢二位。

    http://q.cnblogs.com/q/24859/ 

    不过我在配置中没有找到配置的方法,在源码中倒是有这么一段:

      

[Test]        public void Can_plug_HybridWebThreadSessionStore()        {            string uri = "assembly://Apache.Ibatis.DataMapper.SqlClient.Test/bin.Debug/SqlMap_Test_Configure.config";            IResource resource = ResourceLoaderRegistry.GetResource(uri);             //主要是这个(楼主注)            ConfigurationSetting setting = new ConfigurationSetting();            setting.SessionStore = new HybridWebThreadSessionStore("test");            setting.ValidateMapperConfigFile = true;            IConfigurationEngine engine = new DefaultConfigurationEngine(setting);            engine.RegisterInterpreter(new XmlConfigurationInterpreter(resource));            IMapperFactory mapperFactory = engine.BuildMapperFactory();            IDataMapper dataMapper = ((IDataMapperAccessor)mapperFactory).DataMapper;            Account account = dataMapper.QueryForObject<Account>("GetAccountViaResultClass", 1);            Assert.AreEqual(1, account.Id, "account.Id");            Assert.AreEqual("Joe", account.FirstName, "account.FirstName");            Assert.AreEqual("Dalton", account.LastName, "account.LastName");            Assert.AreEqual("Joe.Dalton@somewhere.com", account.EmailAddress, "account.EmailAddress");        }

    于是按照这个在代码中进行了一下修改,问题果然解决了。回头在翻翻相关文档,补充一下如果在配置文件中进行配置,有知道的也欢迎指出。

    

    这篇文章也有详细的说明:

    http://www.iloveher.cn/ibatis/hybridwebthreadsessionstore.html

    HttpContext的详细说明请查阅:

    http://www.cnblogs.com/fish-li/archive/2013/04/06/3002940.html

mybatisnet Web环境下多线程问题