回滚不';我不能用java工作
我有一个简单的mysql插入的方法,当我尝试回滚插入操作时,如下所示,在一个错误上,但它不是在错误上回滚,请帮助我回滚不';我不能用java工作,java,mysql,jdbc,transactions,Java,Mysql,Jdbc,Transactions,我有一个简单的mysql插入的方法,当我尝试回滚插入操作时,如下所示,在一个错误上,但它不是在错误上回滚,请帮助我 public void addFamer(FamerDTO famer) throws Exception { Connection con = JDBCConnectionPool.getInstance().checkOut(); con.setAutoCommit(false); try { Str
public void addFamer(FamerDTO famer) throws Exception {
Connection con = JDBCConnectionPool.getInstance().checkOut();
con.setAutoCommit(false);
try {
String generalFamerDataSQL = "INSERT INTO famers(famer_code, name_wt_initials, full_name, gender, "
+ "nic_or_passport_no, sc_possition, phone_home, phone_mobile, phone_office) VALUES(?,?,?,?,?,?,?,?,?)";
PreparedStatement insertFamerPS = con.prepareStatement(generalFamerDataSQL, PreparedStatement.RETURN_GENERATED_KEYS);
insertFamerPS.setString(1, famer.getFamerCode());
insertFamerPS.setString(2, famer.getNameWithInitials());
insertFamerPS.setString(3, famer.getNameInFull());
insertFamerPS.setString(4, famer.getGender());
insertFamerPS.setString(5, famer.getNICorPassportNo());
insertFamerPS.setString(6, famer.getSocietyPosission());
insertFamerPS.setString(7, famer.getHomePhone());
insertFamerPS.setString(8, famer.getMobilePhone());
insertFamerPS.setString(9, famer.getOfficePhone());
insertFamerPS.execute();
String famerRelations = "INSERT INTO org_reg_blk_soc_fmr(org_id, region_id, famer_id, block_id, soc_id) "
+ "VALUES (?,?,?,?,?)";
PreparedStatement famerRelationsPS = con.prepareStatement(famerRelations);
famerRelationsPS.setInt(1, famer.getOrganization().getOrg_id());
famerRelationsPS.setInt(2, famer.getRegion().getRegion_id());
famerRelationsPS.setInt(3, famerID);
famerRelationsPS.setInt(4, famer.getBlock().getBlockId());
famerRelationsPS.setInt(6, famer.getSociety().getSoc_id()); //intentionally made an error here to test, put index as 6 for 5
famerRelationsPS.execute();
con.commit();
} catch (Exception e) {
if (con != null) {
logger.info("Rolling back!");
con.rollback();
}
logger.error(e.getLocalizedMessage());
} finally {
con.setAutoCommit(true);
JDBCConnectionPool.getInstance().checkIn(con);
}
}
一旦使用所需参数调用此方法,因为第二个insert语句中有错误,我希望回滚第一个insert操作。但是,尽管显示了错误,第一条insert语句会将一条记录添加到数据库中。只是为了检查-您使用的表类型是什么?上次我使用MySQL时,MyISAM表不支持事务,这意味着您必须使用另一种表类型,例如InnoDB。(删除-1),因为首先要问/look.Thanx很多Rory,我的MySQL表是MyISAM,所以我把它们改成了NYNDB,然后它就写了。“哈沙,如果你解决了你的问题,你应该考虑接受这个答案:”是的,吉姆,但是我不得不等4分钟才能接受它,我最近忘记了。你试过在插入之前明确地启动一个新的事务吗?