Manual commit is not allowed over a Spring managed
来源:互联网 发布:宁波软件开发哪家好 编辑:程序博客网 时间:2024/05/18 00:59
Manual commit is not allowed over a Spring managed SqlSession
Manual close is not allowed over a Spring managed SqlSession
具体展开就像这样:
WARN [DisposableBeanAdapter.java:364] : Invocation of destroy method ‘close’ failed on bean with name ‘sessionTemplate’: java.lang.UnsupportedOperationException: Manual close is not allowed over a Spring managed SqlSession
在使用JUnit测试的时候,出现这两种报错,错误原因都是一样的,通过报错信息,查看SqlSessionTemplate源码可以看到:
/** * {@inheritDoc} */ @Override public void commit() { throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession"); }
触发了上面这个异常,再看自己的Mybatis的配置:
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:framework/mybatis/mapper-config.xml"></property> </bean>
在这里,我们已经通过XML,将SqlSession配置完全,每次在使用sqlsession的时候,我们只要通过@Autowried获取就行,在调用了sqlsession.insert或者sqlsession.select之类的方法之后,不需要我们手动去commit或者close。
配置好的SqlSession将自动完成commit,并在执行完sql之后自动close。
也就是说我们如果手动sqlsession.commit或者close,实际上是重复了一个动作。
我的代码,在sqlseesion.insert执行完毕之后又执行了sqlsession.commit,数据库中是出现了重复的数据的。这也证明了mybatis是自动执行的。
查阅了相关的资料之后,看到如下内容:
SqlSessionTemplate是一个代理类,内部它会为每次请求创建线程安全的sqlsession,并与Spring进行集成,在方法执行完毕之后会自动关闭。
mybatis中的sqlsession本身是一个快速创建和销毁的类,在于spring的配合中最好不要直接操作sqlsession,让spring自动管理。在dao中不要直接配置sqlsession,可以使用SqlsessionDaoSupport并且配置为@Repository就可以了
- Manual commit is not allowed over a Spring managed
- Manual commit is not allowed over a Spring managed SqlSession
- Spring-MyBatis实战之 Manual close is not allowed over a Spring managed SqlSession
- 异常处理Manual close is not allowed over a Spring managed SqlSession
- JUnit测试时,Mybatis报错:Manual close is not allowed over a Spring managed SqlSessio
- iOS 【错误:'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.'】
- Spring Data JPA配置出现Not a managed type错误
- spring data jpa 出现Not a managed type
- spring data jpa 出现Not a managed type
- error: a function-definition is not allowed here before ‘{’ token
- a dependency to an entry point is not allowed
- SVN a peg revision is not allowed here 解决办法
- Spring+Hibernate报错:Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL)
- 27-SVN commit error: “'.' is not a working copy”
- will not be managed by Spring 和was not registered for synchronization because synchronization is n
- Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or r
- Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or rem
- mysql触发器报错 1422 Explicit or implicit commit is not allowed in stored function or trigger.
- 递归法实现折半查找
- 114. Flatten Binary Tree to Linked List
- java查看windows的磁盘空间大小信息
- 每日一记(1)
- HTTP的GET请求和POST请求
- Manual commit is not allowed over a Spring managed
- CMD启动MATLAB并执行脚本小结
- [leetcode]419. Battleships in a Board
- safds
- ViewHolder模式超简洁写法
- ios消息推送机制
- 走进Python世界(8)----使用dict和set
- Intellij IDEA调试功能使用总结
- idea的debug调试