[英]persistence.xml different transaction-type attributes
在persistence.xml JPA 配置文件中,你可以有這樣一行:
<persistence-unit name="com.nz_war_1.0-SNAPSHOTPU" transaction-type="JTA">
或有時:
<persistence-unit name="com.nz_war_1.0-SNAPSHOTPU" transaction-type=”RESOURCE_LOCAL”>
我的問題是:
transaction-type="JTA"
和transaction-type=”RESOURCE_LOCAL”
什么區別?
我還注意到一些缺少事務類型的 persistence.xml 文件。 這是正確的嗎?
在 JavaEE 環境中默認為JTA ,在 JavaSE 環境中默認為RESOURCE_LOCAL 。
使用<persistence-unit transaction-type="RESOURCE_LOCAL">
您負責EntityManager
( PersistenceContext/Cache
) 創建和跟蹤
EntityManagerFactory
來獲取EntityManager
EntityManager
實例是PersistenceContext/Cache
EntityManagerFactory
只能通過@PersistenceUnit
注釋注入(不是@PersistenceContext
)@PersistenceContext
來引用RESOURCE_LOCAL
類型的單元EntityTransaction
API 來開始/提交對EntityManger
每次調用entityManagerFactory.createEntityManager()
兩次會導致兩個單獨的EntityManager
實例,因此導致兩個單獨的PersistenceContexts/Caches
。EntityManager
實例幾乎從來都不是一個好主意(不要創建第二個實例,除非您已經銷毀了第一個實例) 使用<persistence-unit transaction-type="JTA">
容器將執行EntityManager
( PersistenceContext/Cache
) 創建和跟蹤。
EntityManagerFactory
來獲取EntityManager
EntityManager
EntityManager
只能通過@PersistenceContext
注釋注入(不是@PersistenceUnit
)@PersistenceUnit
來引用 JTA 類型的單元EntityManager
是對與 JTA 事務關聯的PersistenceContext/Cache
的引用。EntityManager
因為沒有PersistenceContext/Cache
。EntityManager
引用的人都會自動擁有對同一PersistenceContext/Cache
的引用PersistenceContext/Cache
在 JTA 提交時被刷新和清除
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.