Java org.apache.ibatis.executor.executor异常:无法提交,事务已关闭-批插入时
我想使用MyBatis(3.5.1)将批插入到PostgreSQL中。当我在sqlSession上提交时,我得到一个异常org.apache.ibatis.executor.ExecutorException:无法提交,事务已经关闭 虽然逐个插入记录可以正常工作 我在自动提交标志设置为false的情况下创建SqlSession mapper.xmlJava org.apache.ibatis.executor.executor异常:无法提交,事务已关闭-批插入时,java,mybatis,Java,Mybatis,我想使用MyBatis(3.5.1)将批插入到PostgreSQL中。当我在sqlSession上提交时,我得到一个异常org.apache.ibatis.executor.ExecutorException:无法提交,事务已经关闭 虽然逐个插入记录可以正常工作 我在自动提交标志设置为false的情况下创建SqlSession mapper.xml <insert id="saveAll" parameterType="com.test.Event" useGene
<insert id="saveAll" parameterType="com.test.Event"
useGeneratedKeys="true" keyProperty="id" keyColumn="id">
INSERT INTO test.events (device_id, user_name)
VALUES
<foreach collection="events" item="event" separator=",">
(#{event.deviceId}, #{event.username})
</foreach>
</insert>
堆栈跟踪
Exception was occured.
org.apache.ibatis.exceptions.PersistenceException:
### Error committing transaction. Cause: org.apache.ibatis.executor.ExecutorException: Cannot commit, transaction is already closed
### Cause: org.apache.ibatis.executor.ExecutorException: Cannot commit, transaction is already closed
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.commit(DefaultSqlSession.java:226)
at org.apache.ibatis.session.defaults.DefaultSqlSession.commit(DefaultSqlSession.java:217)
有解决办法吗
谢谢您的
saveAll
方法关闭SqlSession
并结束事务
此语句在try块后调用SqlSession.close`
try (SqlSession sqlSession = transactionManager.getSession()) {
EventMapper mapper = sqlSession.getMapper(EventMapper.class);
mapper.saveAll(events);
}
为了解决这个问题,首先需要确保使用相同的SqlSession
。根据transactionManager.getSession
的具体操作,它可能返回与调用存储库.saveAll的代码中的SqlSession
不同的
修复此问题并使用相同会话后,需要删除try
块,以便会话不会自动关闭
Exception was occured.
org.apache.ibatis.exceptions.PersistenceException:
### Error committing transaction. Cause: org.apache.ibatis.executor.ExecutorException: Cannot commit, transaction is already closed
### Cause: org.apache.ibatis.executor.ExecutorException: Cannot commit, transaction is already closed
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.commit(DefaultSqlSession.java:226)
at org.apache.ibatis.session.defaults.DefaultSqlSession.commit(DefaultSqlSession.java:217)
try (SqlSession sqlSession = transactionManager.getSession()) {
EventMapper mapper = sqlSession.getMapper(EventMapper.class);
mapper.saveAll(events);
}