Mysql数据库,批量入库操作
使用ibatis批处理
代码如下:
public int addMessages(List list) {
int result = 0;
try {
//this.getSqlMapTransactionManager().startTransaction();
this.startBatch();
for(int i=0;i<list.size();i++) {
this.insert("addMessage",(Message)list.get(i));
}
this.executeBatch();
//this.getSqlMapTransactionManager().commitTransaction();
result = 1;
} catch (Exception e) {
result = 0;
e.printStackTrace();
} finally {
try {
//this.getSqlMapTransactionManager().endTransaction();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
我将事务管理的代码注掉了是没有错误的,但是不能实现事务管理。
我加的事务代码是会报错的,希望各位大侠指点谜经。我在这里先谢谢了!
加上我的事务管理代码报如下错误:
com.ibatis.common.jdbc.exception.NestedSQLException: Could not start transaction. Cause: com.ibatis.sqlmap.engine.transaction.TransactionException: TransactionManager could not start a new transaction. A transaction is already started.
Caused by: com.ibatis.sqlmap.engine.transaction.TransactionException: TransactionManager could not start a new transaction. A transaction is already started.
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.startTransaction(SqlMapExecutorDelegate.java:728)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.startTransaction(SqlMapSessionImpl.java:125)
at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.startTransaction(SqlMapClientImpl.java:102)
at com.vodone.cpms.dao.impl.MessageDaoImpl.addMessages(MessageDaoImpl.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.ibatis.dao.engine.impl.DaoProxy.invoke(DaoProxy.java:72)
at $Proxy1.addMessages(Unknown Source)
at com.vodone.cpms.service.MessageService.addMessages(MessageService.java:26)
at com.vodone.cpms.service.MessageService.main(MessageService.java:70)
Caused by: com.ibatis.sqlmap.engine.transaction.TransactionException: TransactionManager could not start a new transaction. A transaction is already started.
at com.ibatis.sqlmap.engine.transaction.TransactionManager.begin(TransactionManager.java:46)
at com.ibatis.sqlmap.engine.transaction.TransactionManager.begin(TransactionManager.java:39)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.startTransaction(SqlMapExecutorDelegate.java:726)
... 11 more
Caused by:
com.ibatis.sqlmap.engine.transaction.TransactionException: TransactionManager could not start a new transaction. A transaction is already started.
at com.ibatis.sqlmap.engine.transaction.TransactionManager.begin(TransactionManager.java:46)
at com.ibatis.sqlmap.engine.transaction.TransactionManager.begin(TransactionManager.java:39)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.startTransaction(SqlMapExecutorDelegate.java:726)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.startTransaction(SqlMapSessionImpl.java:125)
at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.startTransaction(SqlMapClientImpl.java:102)
at com.vodone.cpms.dao.impl.MessageDaoImpl.addMessages(MessageDaoImpl.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.ibatis.dao.engine.impl.DaoProxy.invoke(DaoProxy.java:72)
at $Proxy1.addMessages(Unknown Source)
at com.vodone.cpms.service.MessageService.addMessages(MessageService.java:26)
at com.vodone.cpms.service.MessageService.main(MessageService.java:70)
Caused by:
com.ibatis.sqlmap.engine.transaction.TransactionException: TransactionManager could not start a new transaction. A transaction is already started.
at com.ibatis.sqlmap.engine.transaction.TransactionManager.begin(TransactionManager.java:46)
at com.ibatis.sqlmap.engine.transaction.TransactionManager.begin(TransactionManager.java:39)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.startTransaction(SqlMapExecutorDelegate.java:726)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.startTransaction(SqlMapSessionImpl.java:125)
at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.startTransaction(SqlMapClientImpl.java:102)
at com.vodone.cpms.dao.impl.MessageDaoImpl.addMessages(MessageDaoImpl.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.ibatis.dao.engine.impl.DaoProxy.invoke(DaoProxy.java:72)
at $Proxy1.addMessages(Unknown Source)
at com.vodone.cpms.service.MessageService.addMessages(MessageService.java:26)
at com.vodone.cpms.service.MessageService.main(MessageService.java:70)
Exception in thread "main" com.ibatis.dao.client.DaoException: Error committing SQL Map transaction. Cause: com.ibatis.common.jdbc.exception.NestedSQLException: Could not commit transaction. Cause: com.ibatis.sqlmap.engine.transaction.TransactionException: TransactionManager could not commit. No transaction is started.
Caused by: com.ibatis.sqlmap.engine.transaction.TransactionException: TransactionManager could not commit. No transaction is started.
Caused by: com.ibatis.common.jdbc.exception.NestedSQLException: Could not commit transaction. Cause: com.ibatis.sqlmap.engine.transaction.TransactionException: TransactionManager could not commit. No transaction is started.
Caused by: com.ibatis.sqlmap.engine.transaction.TransactionException: TransactionManager could not commit. No transaction is started.
at com.ibatis.dao.engine.transaction.sqlmap.SqlMapDaoTransaction.commit(SqlMapDaoTransaction.java:43)
at com.ibatis.dao.engine.transaction.sqlmap.SqlMapDaoTransactionManager.commitTransaction(SqlMapDaoTransactionManager.java:75)
at com.ibatis.dao.engine.impl.DaoContext.commitTransaction(DaoContext.java:101)
at com.ibatis.dao.engine.impl.DaoProxy.invoke(DaoProxy.java:73)
at $Proxy1.addMessages(Unknown Source)
at com.vodone.cpms.service.MessageService.addMessages(MessageService.java:26)
at com.vodone.cpms.service.MessageService.main(MessageService.java:70)
Caused by: com.ibatis.common.jdbc.exception.NestedSQLException: Could not commit transaction. Cause: com.ibatis.sqlmap.engine.transaction.TransactionException: TransactionManager could not commit. No transaction is started.
Caused by: com.ibatis.sqlmap.engine.transaction.TransactionException: TransactionManager could not commit. No transaction is started.
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.commitTransaction(SqlMapExecutorDelegate.java:761)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.commitTransaction(SqlMapSessionImpl.java:133)
at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.commitTransaction(SqlMapClientImpl.java:110)
at com.ibatis.dao.engine.transaction.sqlmap.SqlMapDaoTransaction.commit(SqlMapDaoTransaction.java:40)
... 6 more
Caused by: com.ibatis.sqlmap.engine.transaction.TransactionException: TransactionManager could not commit. No transaction is started.
at com.ibatis.sqlmap.engine.transaction.TransactionManager.commit(TransactionManager.java:80)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.commitTransaction(SqlMapExecutorDelegate.java:759)
... 9 more
Caused by:
com.ibatis.common.jdbc.exception.NestedSQLException: Could not commit transaction. Cause: com.ibatis.sqlmap.engine.transaction.TransactionException: TransactionManager could not commit. No transaction is started.
Caused by: com.ibatis.sqlmap.engine.transaction.TransactionException: TransactionManager could not commit. No transaction is started.
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.commitTransaction(SqlMapExecutorDelegate.java:761)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.commitTransaction(SqlMapSessionImpl.java:133)
at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.commitTransaction(SqlMapClientImpl.java:110)
at com.ibatis.dao.engine.transaction.sqlmap.SqlMapDaoTransaction.commit(SqlMapDaoTransaction.java:40)
at com.ibatis.dao.engine.transaction.sqlmap.SqlMapDaoTransactionManager.commitTransaction(SqlMapDaoTransactionManager.java:75)
at com.ibatis.dao.engine.impl.DaoContext.commitTransaction(DaoContext.java:101)
at com.ibatis.dao.engine.impl.DaoProxy.invoke(DaoProxy.java:73)
at $Proxy1.addMessages(Unknown Source)
at com.vodone.cpms.service.MessageService.addMessages(MessageService.java:26)
at com.vodone.cpms.service.MessageService.main(MessageService.java:70)
Caused by: com.ibatis.sqlmap.engine.transaction.TransactionException: TransactionManager could not commit. No transaction is started.
at com.ibatis.sqlmap.engine.transaction.TransactionManager.commit(TransactionManager.java:80)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.commitTransaction(SqlMapExecutorDelegate.java:759)
... 9 more
Caused by:
com.ibatis.sqlmap.engine.transaction.TransactionException: TransactionManager could not commit. No transaction is started.
at com.ibatis.sqlmap.engine.transaction.TransactionManager.commit(TransactionManager.java:80)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.commitTransaction(SqlMapExecutorDelegate.java:759)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.commitTransaction(SqlMapSessionImpl.java:133)
at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.commitTransaction(SqlMapClientImpl.java:110)
at com.ibatis.dao.engine.transaction.sqlmap.SqlMapDaoTransaction.commit(SqlMapDaoTransaction.java:40)
at com.ibatis.dao.engine.transaction.sqlmap.SqlMapDaoTransactionManager.commitTransaction(SqlMapDaoTransactionManager.java:75)
at com.ibatis.dao.engine.impl.DaoContext.commitTransaction(DaoContext.java:101)
at com.ibatis.dao.engine.impl.DaoProxy.invoke(DaoProxy.java:73)
at $Proxy1.addMessages(Unknown Source)
at com.vodone.cpms.service.MessageService.addMessages(MessageService.java:26)
at com.vodone.cpms.service.MessageService.main(MessageService.java:70)
晕,可我没找到那里开始事务了啊。
我发现ibatis的batch自动提交了每次的数据库操作,所以当操作发生异常时,怎么样能实现事务的回滚,我自己加的事务又不能执行,郁闷。帖子还被评为新手贴,更打击人了。是啊自己刚用ibatis,不够强啊。
那为大侠对ibatis的batch比较熟,同事又能进行事务管理,请多指点。
小弟不盛感激!!!!
最后补充一句,我是用的mysql数据库。
分享到:
- 2007-05-18 17:50
- 浏览 5298
- 评论(3)
- 论坛回复 / 浏览 (3 / 8641)
- 查看更多
相关推荐
#### ibatis批量Update解决方案 ibatis提供了一套完善的批量更新机制,可以有效地解决上述问题。下面详细介绍如何利用ibatis实现高效的批量更新: 1. **准备工作**: - 首先确保你的ibatis版本支持批量更新功能。...
本文将深入探讨“ibatis批量存储”这一主题,基于给定的文件名,我们将依次解析Ibatis中的iterate标签、批处理、foreach标签以及如何在Ibatis中使用HashMap。 1. Iterate标签详解及应用: Ibatis的iterate标签主要...
### iBatis批量处理知识点详解 #### 一、iBatis基本实现 iBatis(现已更名为MyBatis)是一款优秀的持久层框架,它支持定制化SQL、存储过程及高级映射。iBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果...
通过以上步骤,我们就完成了Ibatis批量插入的DAO实例。在实际项目中,根据业务需求可能需要对SQL语句进行优化,如开启或关闭数据库的自动提交,或者调整批处理的大小以平衡性能和内存消耗。此外,Ibatis还支持使用`...
**Ibatis 源代码详解** ...总结,Ibatis源代码的学习不仅有助于理解框架的内部工作机制,还能提升开发者在数据库操作方面的编程能力,使得在实际项目中能够更好地利用Ibatis,提高开发效率和代码质量。
《深入解析iBatis-2源代码》 iBatis,作为一个轻量级的持久层框架,曾经在Java开发领域中占据了重要的地位。它将SQL语句与Java代码分离,提高了开发效率,降低了维护难度。本篇文章将针对从Apache网站通过SVN下载的...
### iBatis批量操作 #### 一、简介 在企业级应用开发中,数据库操作是不可或缺的一部分。为了提高效率和减少资源消耗,批量处理成为了一种常用的技术手段。本文将介绍如何利用iBatis框架进行批量添加、修改、删除...
- 批量操作:如何利用iBATIS进行批量插入、更新或删除。 - 动态SQL的最佳实践:分享编写高效动态SQL的技巧和注意事项。 通过深入学习这些文档,开发者可以熟练掌握iBATIS.NET,实现高效、灵活的数据访问,同时...
Ibatis提供了两种执行器:SimpleExecutor和BatchExecutor,分别对应单条SQL执行和批量SQL执行。 5. ParameterMap和ParameterHandler:它们负责参数的设置和处理,ParameterMap代表了SQL参数的映射关系,而...
此外,文档还特别强调了如何通过iBATIS来调用存储过程,这是数据库操作中的一个重要特性,尤其是在处理复杂逻辑或批量操作时。 **iBATIS-SqlMaps-2-Tutorial_cn.pdf** 作为教程,这份文档提供了逐步指导,帮助读者...
13. **案例分析**:通过实际案例展示iBatis在不同场景下的应用,如CRUD操作、复杂查询、批量处理等。 14. **与其他框架集成**:探讨如何将iBatis与Spring、Hibernate等其他框架集成,构建更强大的企业级应用。 这...
除了基本的查询,iBatis还支持动态SQL、批量操作、分页等功能,这些都是在实际项目中非常实用的能力。例如,动态SQL允许开发者根据不同的条件构建SQL语句,而批量操作则可以提高数据处理的效率。 ### 四、总结 ...
此外,还会介绍如何处理结果集、执行批量操作、使用缓存机制等高级特性。 **iBatis SQL Maps** - **Select**: 用于查询数据,可以返回单个对象、列表或者Map。 - **Insert**: 插入数据,支持自动获取自增主键值。 ...
- **性能优化**:批量操作的数据量不宜过大,避免一次性加载过多数据导致内存压力,同时应合理调整数据库的批处理大小设置,以平衡性能和资源消耗。 - **事务隔离级别**:根据业务需求选择合适的事务隔离级别,以...
4. 执行器(Executor):Ibatis中的执行器是SQL语句执行的入口,它根据配置决定是执行简单查询还是缓存查询,或者是批量操作。 5. 缓存机制:Ibatis支持本地缓存和二级缓存,可以提高数据读取速度,减少对数据库的...
通过提供的文件《IBatisNet开发使用小结.docx》和《iBatis[1].Net详细使用手册.docx》,你将能够找到具体的步骤和示例代码,这些实例将涵盖基本的CRUD操作(创建、读取、更新和删除),以及更高级的功能如存储过程...
此外,可能会讲解如何使用Executor执行器进行批量操作,以及如何利用TypeHandler处理自定义数据类型。 通过阅读这三份文档,你可以系统地学习到iBatis框架的各个方面,从基本的配置和使用,到高级的优化和扩展。...
iBATIS是Java平台上的一种轻量级持久层框架,它将SQL语句与Java代码分离,使得开发者可以更加灵活地处理数据库操作。 【描述】中的"可以直接与myeclipse进行关联"意味着该源码包可以方便地在MyEclipse集成开发环境...
例如,`(" close=")" conjunction=",">`这段代码会将`actionIds`数组中的每个元素以逗号分隔的形式插入到SQL语句中,用于批量更新操作。同时,为了避免数组为空或为null时引发的SQL语法错误,iBATIS还提供了`...