........
catch(java.sql.SQLException e) {
    try {
        sqlMap.rollbackTransaction();
    }catch(java.sql.SQLException ex) {    }
    e.printStackTrace();
}
finally {
    try {
        sqlMap.endTransaction()
    } finally {
        sqlMap.endTransaction()
    }
}

解决方案 »

  1.   

      ibatis都学了,为什么不学spring中的SqlMapClientTemplate,为什么不再学下spring的声明式事务呢。
      

  2.   

    谢谢大家!!我一直是用Spring来做声明式事务的,所以没手动写过ibatis的事务处理,这次有个项目对性能要求比较苛刻,所以我就一省再省,把Spring给砍掉了。
      

  3.   

    没有为类型 SqlMapClient 定义方法 rollbackTransaction()!!!!!!!
    =====================================================================
    救命啊,大家请留步!!!我的包是ibatis-2.3.4.726.jar,最新的,没有rollbackTransaction()这个方法呀~~怎么回事?????
      

  4.   

    Ends a transaction and rolls back if necessary.  If the transaction has
    been started, but not committed, it will be rolled back upon calling
    endTransaction().看来endTransaction比较智能
    sqlMap.startTransaction(); 
    sqlMap.insert("insertTask", entity);  //插入第一条记录 
    sqlMap.endTransaction(); 
    楼主试试这样,看看到底会不会rollback
    如果成功,就说明代码有问题,没有抛出异常;否则就是包有问题
      

  5.   

    endTransactionpublic void endTransaction()
                        throws java.sql.SQLException    Description copied from interface: SqlMapTransactionManager
        Ends a transaction and rolls back if necessary. If the transaction has been started, but not committed, it will be rolled back upon calling endTransaction(). 
    说明如果没有commit就执行到了endTransaction的话,就是回滚,否则就是提交
    楼主的代码再加上finally的话就没有问题了,楼主确认是否数据库没有发生异常呀,贴出来看看
    SqlMapClient sqlMap = getSqlMapClient();;
    try {
        sqlMap.startTransaction();
        sqlMap.insert("insertTask", entity);  //插入第一条记录
        entity.setContent(null);  //故意设置不能为空的字段值为null
        sqlMap.insert("insertTask", entity);
        sqlMap.commitTransaction();
    }catch(java.sql.SQLException e) {
        try {
        sqlMap.endTransaction();
        }catch(java.sql.SQLException ex) {    }
        e.printStackTrace();
    } finally {
        try {
            sqlMap.endTransaction()
        } finally {
            sqlMap.endTransaction()
        }
    }
      

  6.   

    还是不行呀,我代码改成这样了,还是被写入一条记录!
    SqlMapClient sqlMap = getSqlMapClient();; 
    try { 
        sqlMap.startTransaction(); 
        sqlMap.insert("insertTask", entity);  //插入第一条记录 
        entity.setContent(null);  //故意设置不能为空的字段值为null 
        sqlMap.insert("insertTask", entity); 
        sqlMap.commitTransaction(); 
    }catch(java.sql.SQLException e) { 
        e.printStackTrace(); 
    }finally {
        try { 
            sqlMap.endTransaction(); 
        }catch(java.sql.SQLException ex) {
        } 
    }
    由于第二条记录我把不能为空的字段改为空,所以出现了异常,但就应该没有执行到commit呀:
    com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'content' cannot be null
      

  7.   

    真是邪门,我把sqlMap.commitTransaction()注释掉,也是被提交上去郁闷。
      

  8.   


    你用的是Mysql,
    桌面引擎应该是MyISAM吧?
    MyISAM下没有事务处理的概念,commit不发生作用。
      

  9.   

    CREATE TABLE `task` (
      `taskId` int(11) NOT NULL auto_increment,
      `content` text NOT NULL,
      `answer` text,
      `description` text,
      `addDateTime` varchar(20) NOT NULL,
      PRIMARY KEY  (`taskId`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    建表时指定为InnoDB也不行吧???
      

  10.   

    出现同样问题,根本没有rollback