........
catch(java.sql.SQLException e) {
try {
sqlMap.rollbackTransaction();
}catch(java.sql.SQLException ex) { }
e.printStackTrace();
}
finally {
try {
sqlMap.endTransaction()
} finally {
sqlMap.endTransaction()
}
}
catch(java.sql.SQLException e) {
try {
sqlMap.rollbackTransaction();
}catch(java.sql.SQLException ex) { }
e.printStackTrace();
}
finally {
try {
sqlMap.endTransaction()
} finally {
sqlMap.endTransaction()
}
}
=====================================================================
救命啊,大家请留步!!!我的包是ibatis-2.3.4.726.jar,最新的,没有rollbackTransaction()这个方法呀~~怎么回事?????
been started, but not committed, it will be rolled back upon calling
endTransaction().看来endTransaction比较智能
sqlMap.startTransaction();
sqlMap.insert("insertTask", entity); //插入第一条记录
sqlMap.endTransaction();
楼主试试这样,看看到底会不会rollback
如果成功,就说明代码有问题,没有抛出异常;否则就是包有问题
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()
}
}
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
你用的是Mysql,
桌面引擎应该是MyISAM吧?
MyISAM下没有事务处理的概念,commit不发生作用。
`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也不行吧???