English 中文(简体)
是否保证在交易集团结束时违反限制?
原标题:Is it guaranteed that constraint violation is flagged at the end of transactional block?

I’m using Postgres and have a transactional method defined as follows:

@Entity
public class SomeEntity {
    // …
}

@Transactional(isolation = READ_COMMITED)
public void persistUniqueAndSendEmail() {
    SomeEntity e = // …
    // Persis the entity to Postgres with a unique constraint that may fail

    // Once the entity is persisted send the notification email
}

With READ_COMMITED isolation level is it possible that the unique constraint violation is thrown when the transaction is actually committed, not the actual sql statement is executed?

如果这意味着可以发送电子邮件,但有关的改动并不持续。

最佳回答

交易到该方法结束为止。 直到交易结束为止,储蓄的冲积促使它进入。 那么,如果你在发出电子邮件之后,就会出现一种违反限制的情况。

你们可以尝试拯救 AndFlush und Save (见Difference between Save and SaveAndFlush in Spring data jpa 。 流动迫使数据库出现,这将引发任何违反限制的行为(即使交易仍在进行)。

但是,你不希望交易因某些其他原因失败,而且电子邮件被错误地发送,而且会发生很多情况。 要做的安全事情(更加安全,比较安全的方式)是确保交易圆满完成,然后将电子邮件寄给另一个称为交易服务的服务机构,或寄给AOP截获者。

这种方式对于数据库的工作来说是安全的,但对于电子邮件发送来说并不安全,因为作为,这一答案彻底表明,你确实存在2个系统的问题。 值得思考的是,在你的交易回头时,你们的代码发生了什么,电子邮件仍然发出。 如果你想重复你的交易,你就必须再次发出电子邮件。 将非交易 st像电子邮件等同交易数据库更新混为一谈,是充满风险的。

检查你的要求,发现未能发送电子邮件或发送重复电子邮件的后果,并利用这一信息决定如何协调这两个系统。

问题回答

You have a few problems here.

  1. 是的,它能够做到。 具体地说,可在现场进行制约检查。 当你作出承诺时,它取决于<>DDL,而不是联系的建立。 在PSQL中,这种制约检查是触发的,可以没收: Don(坏思想,但当你重新大规模进口时,会有所助益),在现场站立,并推迟到投入。 我们不知道你是如何组合你的亚洲开发银行的(正如你写的那样,为建立这些制约因素)。 在报表执行时,通过违约限制进行核查。

  2. Use SERIALIZABLE instead. Your database s integrity will thank you.

  3. Regardless of the situation of where constraints are checked, a commit is never guaranteed. What if you send out the email and then someone trips over the power cable? If you reverse the situation and commit, then send the email, what happens if someone trips over a power cable after the commit but before the email is sent?

你们在此 st着两系统。 你们有两件事,两件都属于原体,你们都希望两件都发生(送电子邮件和<>>)。 浏览量更新,或者不会发生。 www.un.org/Depts/DGACM/index_spanish.htm 但是,这是不可能的——2个不同的系统可以这样做。

You have many ways out of the dilemma and they re all bad. Some general aspects:

  • Remember that spaceballs movie scene with preparing to rewind! ? Do that. Log somewhere (perhaps in the database): "About to send an email", "Email sent" - where each log action is fully flushed before proceeding. Then, on boot of the system, scan your dbs and check if the last event isn t one that has returned the system to a stable state. If it has not, use what you have logged to restore state. This is exactly how journalling file systems work.

如果最后要发出电子邮件,就发生了两件事情:

  • The email was sent, but before you managed to log email sent , someone tripped over a power cord. The correct act is to mark off the entry as ... and the email has been sent .
  • The email was not sent as someone tripped over a power cord before it went out. The correct act is to send it.

如果没有单独的APIC或其他系统让你检查所发生的情况,你就能够解决这一困境。 例如,如果你通过电子邮件和ou重新发送,则可以使用其预报工具检查寄出的箱子。 最糟糕的是,在你刚好坠毁的boot子上,有人要求经营者更新该条目,准确解释该电子邮件的命运。 或者,出现一些违约情况(在怀疑时,发出两次,例如。 i. 假设电子邮件没有发出,以便恢复状态,再次发出。

这相当复杂,但是唯一的使2个不同的系统原子化的办法。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...