插入失败后,我可以继续进行数据库事务吗? [英] Can I continue with a database transaction after a failed insert?

查看:256
本文介绍了插入失败后,我可以继续进行数据库事务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用程序是高度并发的。它正在取出MQ数据并将其保存到数据库中。有多个消息可能包含相同的数据。因此,一条消息可能包含营业时间,第二条消息可能包含营业时间和地点。

因此,第一条消息通过,我保存营业时间数据。然后,第二笔交易的营业时间与实际数据相同。现在我需要保存位置数据,但运行时间是重复的。由于并发性,我无法检查数据库的运行时间(如果检查,它可能不存在,那么在我保存它时,它可能已被另一个线程插入)。我也想快点。 (这个加载器负载很多)。

当前一条消息获得一个Transaction(Propagation.REQUIRED)(使用Spring和Hibernate)。我在每种数据插入后都做一次冲洗。所以我保存位置数据,并做一个刷新。那么我可以节省运行时间并冲洗。



如果我只是收到错误消息(在flush()后面抛出并且吃掉它,其他数据(位置数据)是否仍然会保存?数据库插入失败后,事务仍然可以使用吗?因此,如果第一次保存失败,我希望第二次插入工作。



我一直在寻找Propagation.REQUIRES_NEW,但遇到了一些麻烦。另外,我担心会减慢速度。我需要3个交易(在这之前有一些逻辑)。



大部分关于Rollback的信息都谈到了,但我不想回滚。我只想继续前进。我可以吗?

解决方案

不,您无法捕捉异常并尝试继续。以下是有关异常处理的章节hibernate文档说:
$ b


如果Session引发一个异常,包括任何SQLException,
立即回滚数据库事务,调用Session.close()
并放弃Session实例。 Session的某些方法不会
使会话处于一致状态。
没有抛出异常Hibernate可以被视为可恢复。



Our application is highly concurrent. It is taking MQ data and saving it to a database. There are multiple messages that might contain the same data. So one message might contain operating hours and the second message might contain operating hours and location.

So the first message comes through and I save the operating hours data. Then the second transaction comes in with the same exact data for operating hours. Now I need the location data to be saved, but the operating hours is a duplicate. I can't make a check for operating hours on the database because of concurrency (If I check, it might so it is not existing, then by the time I save it, it might have been inserted by another thread). I also want to be fast. (lots of load on this loader.)

Currently a message gets one Transaction (Propagation.REQUIRED) (using Spring and Hibernate). I do a flush after each kind of data insert. So I save the location data, and do a flush. then I save the operating hours and flush.

If I just catch the error message (it is thrown after the flush()) and eat it, will the other data saved (the location data) still be saved? Is the Transaction still OK to use after a database insert failure? So if the first save failed, I want the second insert to work.

I have been looking at Propagation.REQUIRES_NEW but have had some trouble. Also, I am concerned about slowing this down. I would need 3 transactions (there is a bit of logic before all this.)

Most of the info on the we talks about Rollbacks, but I don't want to rollback. I just want to move on. Can I?

解决方案

No, you can't catch the exception and try to continue. Here's what the chapter about exception handling of the hibernate documentation says:

If the Session throws an exception, including any SQLException, immediately rollback the database transaction, call Session.close() and discard the Session instance. Certain methods of Session will not leave the session in a consistent state. No exception thrown by Hibernate can be treated as recoverable.

这篇关于插入失败后,我可以继续进行数据库事务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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