简体   繁体   English

了解EJB和Hibernate上下文中的事务

[英]Understanding transaction in a context of EJB and Hibernate

EJB is by default handling transaction management, and as I read, hibernate also handle transaction. EJB默认情况下是处理事务管理的,而正如我所读到的,休眠也可以处理事务。 When I talk about a transaction, I have the understanding of rollback a database persist operation like functionality. 当我谈论事务时,我对回滚数据库持久性操作(如功能)有所了解。

My question is how a application which use both EJB and Hibernate we selectively choose one transaction provider to support? 我的问题是,如何同时使用EJB和Hibernate的应用程序有选择地选择一个事务提供程序来支持? Also can both EJB and hibernate transaction work simultaneously? EJB和休眠事务也可以同时工作吗?

Hibernate and EJB transactions can and do work together simultaneously. Hibernate和EJB事务可以同时协同工作。 By default, when an EJB call is made, the call will be in an EJB transaction which can be committed or rolled back. 默认情况下,当进行EJB调用时,该调用将处于EJB事务中,该事务可以被提交或回滚。 If you happen to make a hibernate call within that EJB call (or in a MDB), and that transaction is rolled back, the hibernate database transaction will also roll back. 如果您恰巧在该EJB调用中(或在MDB中)进行了休眠调用,并且该事务已回滚,则休眠数据库事务也将回滚。 In this scenario, when you do a transaction commit, hibernate will also commit the database transaction. 在这种情况下,当您进行事务提交时,休眠也会提交数据库事务。

The way I think of it is this: an EJB transaction wraps around a Hibernate database transaction, and the database transaction depends on the EJB transaction to be committed. 我的想法是这样的:EJB事务环绕Hibernate数据库事务,而数据库事务取决于要提交的EJB事务。 If the EJB transaction fails, so does the database transaction. 如果EJB事务失败,则数据库事务也会失败。 If the EJB transaction commits, so does the database transaction. 如果EJB事务提交,则数据库事务也提交。

Example #1 - successful transaction: 范例1-成功交易:

EJB method
   |--> Transaction Started
   |
   |--> Call persist method on Entity Manager instance
         |
         |--> SQL insert statement added to database transaction
   ...
   |--> EJB Transaction committed
         | --> SQL insert statement committed to database

Example #2 - rolled-back transaction: 范例2-回滚交易:

EJB method
   |--> Transaction Started
   |
   |--> Call persist method on Entity Manager instance
         |
         |--> SQL insert statement added to database transaction
   ...
   X--> EJB Transaction rolls back
         X --> Database transaction rolls back (insert not performed)

Furthermore, if you are nesting EJB calls (for instance, one EJB method calls another), by default, they all live in the same transaction. 此外,如果嵌套EJB调用(例如,一个EJB方法调用另一个),则默认情况下,它们都位于同一事务中。 So the entire hierarchy of calls must complete successfully in order for all of the EJB and Hibernate calls to commit. 因此,整个调用层次结构必须成功完成才能提交所有EJB和Hibernate调用。

Example #3 - nested EJB transactions: Example#3-嵌套的EJB事务:

EJB method
|--> Transaction Started
|
|--> Call persist method on Entity Manager instance
    |--> SQL insert statement added to database transaction
    ...
    |--> Another EJB method called, *continues* same transaction
        |
        |--> EJB method successfully commits
    ...
    |--> EJB Transaction committed
        | --> SQL insert statement committed to database

Example #4 - nested EJB transaction with rollback: Example#4-具有回滚的嵌套EJB事务:

EJB method
|--> Transaction Started
|
|--> Call persist method on Entity Manager instance
    |
    |--> SQL insert statement added to database transaction
    ...
    |--> Another EJB method called, *continues* same transaction
        |
        X--> EJB method rolls back
    ...
    X--> EJB Transaction rolls back
        X --> Database transaction rolled back (insert not performed)

You can change this behavior by managing the transactions yourself, OR by demarcating your methods with @TransactionAttribute s if you are using Container Managed Transactions (ie letting the container do the hard work). 您可以通过自己管理事务来更改此行为,或者如果您使用的是容器托管事务,则可以使用@TransactionAttribute划分方法(例如,让容器进行艰苦的工作)。

This Guide has a lot of good information about transactions and hibernate. 本指南提供了大量有关事务和休眠的良好信息。

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

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