简体   繁体   English

什么是Java EE中的事务?

[英]What is a transaction in java EE?

In every source I've seen, a transaction is defined as a set of operations that succeed or fail together. 在我所见过的每一个资料中,事务都定义为一组成功或失败在一起的操作。 However, using an EntityManager in JPA, it seems to require a transaction even to persist a single entity that I have created in memory. 但是,在JPA中使用EntityManager似乎需要进行事务处理,以持久保存我在内存中创建的单个实体。 What is the meaning of transaction? 交易是什么意思? Isn't this operation already atomic? 这个操作不是已经原子了吗? How could I rollback a single entity.persist() ? 我如何回滚单个entity.persist() Is there a clear definition of the concept anywhere in the java EE documentation? 在Java EE文档的任何地方都对概念进行了明确的定义?

Thinking that EntityManager.persist() is atomic is like always using SQL with auto-commit enabled. 认为EntityManager.persist()是原子的,就像始终使用启用了自动提交的SQL一样。 A transaction is used to create an atomic operation which allows you to insert one or more rows into one or more tables as a single atomic operation, either evertyhing is inserted or nothing is inserted. 事务用于创建原子操作,该原子操作允许您将一个或多个行作为单个原子操作插入到一个或多个表中,或者插入全部或不插入任何内容。

If you didn't have transaction, and you insert a User and his two phone numbers (two tables), and something goes wrong when you insert the second phone number, you want the entire transaction to fail, rather than have inconsistent data. 如果您没有事务,并且插入一个用户和他的两个电话号码(两个表),而在插入第二个电话号码时出了点问题,则您希望整个事务失败,而不是数据不一致。

If you use an application managed EntityManager, you control the transaction progammaticrally ( EntityManager.getTransaction() ), so you control which operations (inserts/updates) should happen as an atomic operation. 如果使用由应用程序管理的EntityManager,则可以通过编程方式控制事务( EntityManager.getTransaction() ),因此您可以控制哪些操作(插入/更新)应作为原子操作进行。

If you use a container managed EntityManager (Spring, JavaEE) then the transaction is declarative (@Transactional annotation), so transactions are basically method scoped (typically propagated through multiple method calls). 如果使用容器管理的EntityManager(Spring,JavaEE),则事务是声明性的(@Transactional批注),因此事务基本上是方法范围的(通常通过多个方法调用传播)。

This is just the tip of the iceberg when it comes to transactions, there are other things like Isolation levels, and serialised transaction, but lets talks about that another time :) 这只是涉及事务的冰山一角,还有诸如隔离级别和序列化事务之类的其他内容,但让我们再谈一次:)

Isn't this operation already atomic? 这个操作不是已经原子了吗? How could I rollback a single entity.persist() ? 我如何回滚单个entity.persist()

It might or might not be atomic. 它可能是原子的,也可能不是原子的。 For instance, if your entity has a relationship to another entity with CascadeType.PERSIST , or uses a secondary table, it will require more than one SQL statement to make it persistent. 例如,如果您的实体与另一个具有CascadeType.PERSIST实体有关系,或者使用辅助表,则它将需要多个SQL语句来使其持久化。

There really isn't a one-to-one mapping between EntityManager calls and SQL statements, neither are the two required to coincide in time. EntityManager调用和SQL语句之间确实并没有一对一的映射,这两者在时间上也不需要重合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM