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

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

Manual close is not allowed over a Spring managed SqlSession

Mybatis中Manual close is not allowed over a Spring managed SqlSession_第1张图片

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

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

:::::::正确回答:::::::

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

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

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

 
         

     

注意其prototype,

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

@Resource 
protected SqlSessionTemplate sqlSessionTemplate;

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

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

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

在dao中不要直接配置sqlSession ,可以使用SqlSessionDaoSupport 并且配置为@Repository 就可以了。

我的项目中是这么配置的:

5d2b8784cc3af578267b961b5c5415f5d40.jpg

在ServiceImpl中我使用了SqlSessionTemplate

Mybatis中Manual close is not allowed over a Spring managed SqlSession_第2张图片

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

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

090702ae991b90a1dd8d65bc283476ecb42.jpg

 

总结:

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

借鉴:

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

转载于:https://my.oschina.net/u/3725191/blog/3019101

你可能感兴趣的:(Mybatis中Manual close is not allowed over a Spring managed SqlSession)