论坛首页 入门技术论坛

关于ibatis批量操作的代码

浏览 8641 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-05-18  
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  
TransactionManager could not start a new transaction. A transaction is already started.
0 请登录后投票
   发表时间:2007-05-18  
提示信息已经很明显了,请检查事务已经在什么别的地方开启?
0 请登录后投票
   发表时间:2007-05-21  
我想问一下Mysql数据库的事务管理怎么实现,谢谢!
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics