设置connection.setAutoCommit(false)后,为什么不调用commit()也能提交?

hhx420 2007-06-10 11:15:55
各位:我想试验java数据库事务,在设置connection.setAutoCommit(false)后,为什么不调用commit()也能提交?并且在其中一个更新数据操作有异常时,rollback()也不起作用,我使用的是mysql数据库(5.x).
...全文
839 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
eric06s 2007-06-20
  • 打赏
  • 举报
回复
public void setAutoCommit(boolean flag) {
try {
if (conn != null) {
conn.setAutoCommit(flag);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void commit() {
try {
if (conn != null) {
conn.commit();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
try{
// 批处理 start2
Connection conn = dbm.conn();
Statement stmt = conn.createStatement();

String getListSize = request.getParameter("listSize");
String[] id = request.getParameterValues("id");
String[] userName = request.getParameterValues("userName");
String[] passWord = request.getParameterValues("passWord");
int listSize = java.lang.Integer.parseInt(getListSize);
// 事务处理1
dbm.setAutoCommit(false);
// 事务处理1结束
for (int i = 0; i < listSize; i++) {
String sql = "UPDATE userInfo set userName='" + userName[i]
+ "',passWord='" + passWord[i] + "' where id='" + id[i]
+ "'";
stmt.addBatch(sql);
}
//执行并返回对数据库影响的行数
int[] counts=stmt.executeBatch();
// for (int i = 0; i < counts.length; i++) {
// System.out.println("counts[]=" + counts[i]);
// }
// 事务处理2
dbm.commit();
dbm.setAutoCommit(true);
// 事务处理2结束
// 批处理end2

}
joejoe1991 2007-06-20
  • 打赏
  • 举报
回复
我想问一下 事务是不是只针对那些修改语句说的啊?
一堆查询语句是不是不算说是事务?
kaoloveting 2007-06-20
  • 打赏
  • 举报
回复
mysql是不是那个版本一下的不支持事务?
fengmingjie 2007-06-20
  • 打赏
  • 举报
回复
dbm.commit();
dbm.setAutoCommit(true);
注释掉,看是否还能进行提交,把自动提交设置成false是不自动提交事务的
fiore 2007-06-10
  • 打赏
  • 举报
回复
没遇到过这个问题,贴出来看看
believefym 2007-06-10
  • 打赏
  • 举报
回复
没有commit也能提交,不可能吧

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧