changtuipangxie 发表于 2013-2-5 02:10:44

iBatis的数据回滚

在一次操作中要同时向三张表中插入数据,并且这三张表是相互关联的,也就是说如果发现某次添加操作并未完成对三张表的插入,那么我需要做rollback操作。这样我用到了如下方法:
public void bussinessBatchData(List list) throws SQLException {
        SqlMapClient sqlMapClient = this.getSqlMapClient();
        Connection c = null;
        try {
            c = sqlMapClient.getDataSource().getConnection();
            c.setAutoCommit(false);
            sqlMapClient.setUserConnection(c);
            for (Iterator it = list.iterator(); it.hasNext();) {
                BussinessBatch bussinessBatch = (BussinessBatch) it.next();
                String operate = bussinessBatch.getOperate();
                if ("INSERT".equals(operate)) {
                   sqlMapClient.insert(bussinessBatch.getString(), bussinessBatch
                            .getObject());
                } else if ("DELETE".equals(operate)) {
                    sqlMapClient.delete(bussinessBatch.getString(), bussinessBatch
                            .getObject());
                } else if ("UPDATE".equals(operate)) {
                    sqlMapClient.update(bussinessBatch.getString(), bussinessBatch
                            .getObject());
                } else {
                    // do nothing!
                }
            }
            c.commit();
        } catch (SQLException e) {
            try {
                c.rollback();
            } catch (Exception et) {
            }
            throw e;
        } finally {
            try {
                c.setAutoCommit(true);
                c.close();
            } catch (Exception et) {
            }
           
        }
    }
 
其中list中含有sql的操作类型(insert/delete/update),sql语句(配在iBatis配置文件),以及sql语句操作所需要的参数,可是问题产生了,我用这个方法,操作一次没有问题,可再接着再进行一次操作(添加),就会报异常:
2008-12-09 09:45:24 org.springframework.transaction.interceptor.TransactionInterceptor DEBUG Completing transaction for after exception: com.ibatis.common.jdbc.exception.NestedSQLException:  
--- The error occurred in com/aa/bb/sqlmapdao/sql/ut/UT.xml. 
--- The error occurred while applying a parameter map. 
--- Check the UT.insert-InlineParameterMap. 
--- Check the parameter mapping for the 'Id' property. 
--- Cause: java.sql.SQLException: org.apache.commons.dbcp.DelegatingPreparedStatement is closed.
不知道问题出在哪里,至于说让我检查Id这个属性,我觉得是没有问题的,可能问题出在上边的代码了。可又不知道如何修改,唉,愁!
 
页: [1]
查看完整版本: iBatis的数据回滚