简体   繁体   English

两个EAR文件,相同的JPA实体管理器,同一个事务=>相同的会话?

[英]Two EAR files, same JPA entitymanager, same transaction => same session?

Lets say I have two applications, each with an individual EAR file, which call each other within the same JTA Transaction. 假设我有两个应用程序,每个应用程序都有一个单独的EAR文件,它们在同一个JTA事务中相互调用。 If both share the same entitymanager, do they get the same session or is it created new each time? 如果两者共享同一个实体管理器,它们是否会获得相同的会话,或者每次创建新会话?

An EntityManager (in JPA) is more or less equivalent to a Session (in Hibernate). EntityManager(在JPA中)或多或少等同于Session(在Hibernate中)。 In a pure JPA application, you would only use the EntityManager. 在纯JPA应用程序中,您只能使用EntityManager。 It encapsulates a Session. 它封装了一个Session。 The Session lives as long as the EntityManager lives. 只要EntityManager存在,会话就会存在。

There is no reason (and I think no way) to share an EntityManager between two applications, as they run in different JVMs (at least on the application servers I've worked with). 没有理由(我认为没办法)在两个应用程序之间共享一个EntityManager,因为它们在不同的JVM中运行(至少在我使用过的应用程序服务器上)。 What you can do is share the EntityManager setup (called a Persistence Unit). 您可以做的是共享EntityManager 设置 (称为持久性单元)。 You can do that by putting the entity classes and the XML into a JAR and using it from both applications, but how exactly this is done probably depends on your application server. 您可以通过将实体类和XML放入JAR并从两个应用程序中使用它来实现,但是如何完成此操作可能取决于您的应用程序服务器。 It will definitely have the exact same effect as just duplicating the classes and the XML for the second application. 它肯定会与仅复制第二个应用程序的类和XML完全相同。

What will happen is this: Each of the two applications will have its own persistence context. 会发生什么:两个应用程序中的每一个都有自己的持久化上下文。 That means, when you load an entity in one application, it will not be loaded in the other. 这意味着,当您在一个应用程序中加载实体时,它将不会加载到另一个应用程序中。 If you load and modify an entity in application one, then load it in application two, application two will see the unmodified entity (except if you have very strange transaction isolation settings and application one decides to flush the entity first). 如果您在应用程序1中加载和修改实体,然后在应用程序2中加载它,则应用程序2将看到未修改的实体(除非您有非常奇怪的事务隔离设置, 并且应用程序首先决定刷新实体)。

Any conflicts will only surface at the end of the JTA transaction. 任何冲突只会在JTA交易结束时浮出水面。 I don't know what will happen then, and I think it depends on your database and transaction settings. 我不知道接下来会发生什么,我认为这取决于您的数据库和事务设置。 Probably the transaction will roll back if both applications try to do different things to the same data. 如果两个应用程序尝试对同一数据执行不同的操作,则事务可能会回滚。 Each application will have its own database connection. 每个应用程序都有自己的数据库连接。 They are tied together by the JTA transaction, so that is where it is ensured that either both commit or both roll back. 它们通过JTA事务绑定在一起,因此确保两者都提交或两者都回滚。

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

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