在Java中使用mysql回滚事务 [英] roll back transaction using mysql in java

查看:147
本文介绍了在Java中使用mysql回滚事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将数据插入到三个表中

I am inserting data into three table i.e.

Transaction
TransactionEntry
Payment

首先,在 Transaction 中插入数据,然后获得 Transaction ID ,然后在 TransactionEntry 中插入数据,然后在中插入>付款

First I insert the data in Transaction , and then get Transaction ID and then insert data in TransactionEntry, after this inserting in Payment

假设在前两个表中,数据已成功插入.然后服务器崩溃.

Suppose in first two table, data inserted successfully. Then Server breakdown.

我们如何回滚所有条目?我们可以通过编程方式控制它还是数据库具有默认功能来控制这种情况?

How can we Roll Back all the entries? can we control it programmatically or database has default feature to control this type of situation?

如何实现一致性和完整性?

How to achieve consistency and integrity?

在Java中,哪个框架可以控制这种情况?

Which framwork, In java , can control this type of situation?

MySQL Java EE

推荐答案

您需要在工作单元完成后禁用自动提交和提交(或在失败时回滚).

You need to disable autocommit and commit after your unit of work is done (or rollback when it failed).

这应该类似于:

Connection con = ...; //set earlier
con.setAutoCommit(false);
...
try {
  //insert here
  ...
  con.commit();
} catch (Exception e) {
  con.rollback();
  // other exception handling
}

这篇关于在Java中使用mysql回滚事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆