Mybatis中Manual close is not allowed over a Spring managed SqlSession

Manual close is not allowed over a Spring managed SqlSession

在Spring托管的SqlSession上不容许手动关闭spring

在项目中出现的警告提示!!!sql

:::::::正确回答:::::::安全

SqlSessionTemplate你不能够手动关闭。SqlSessionTemplate是一个代理类,内部他会为每次请求建立线程安全的sqlsession,并与Spring进行集成.在你的方法调用完毕之后他会自动关闭的。session

-----------------------------------方法一----------------------------------------------------mybatis

解决方法很简单,在spring中配置SqlSessionTemplate为:app

<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"> 
        <constructor-arg index="0" ref="sqlSessionFactory" /> 单元测试

    </bean> 测试

注意其prototype,spa

这样你的Dao使用如下配置就没有问题:.net

@Resource 
protected SqlSessionTemplate sqlSessionTemplate;

-----------------------------------方法二----------------------------------------------------

你这么配置是有问题的,mybatis 中的sqlSession自己是一个快速建立和销毁的类,在与spring的配合中最好不要直接操纵sqlSession,让spring自动管理。

在配置文件中的 sqlSession 段是不须要的,

在dao中不要直接配置sqlSession ,能够使用SqlSessionDaoSupport 而且配置为@Repository 就能够了。

个人项目中是这么配置的:

在ServiceImpl中我使用了SqlSessionTemplate

根据上面仁兄的回答,我是不须要使用SqlSessionTemplate的,直接让spring给我进行管理便可!!!

所以我取消了关于SqlSessionTemplate的配置!而且进行单元测试,发现测试成功,并无报错!因此可见

 

总结:

    在与spring的配合中最好不要直接操纵sqlSession,直接让mapper与mapper.xml对应使用底层spring去操做sqlSession便可!!!

借鉴:

https://www.oschina.net/question/97503_131975?sort=time

相关文章
相关标签/搜索