日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Java EE 7之前版本替代JPA 2.1的非同步持久性上下文

發(fā)布時間:2023/12/3 66 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java EE 7之前版本替代JPA 2.1的非同步持久性上下文 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Java EE 7中的非同步持久性上下文

JPA 2.1引入了非同步持久性上下文的概念,該概念允許對JPA實體管理器的刷新進行細粒度控制,即通過顯式調(diào)用EntityManager#joinTransaction 。 以前,這默認情況下是JTA事務的結(jié)束,例如,在典型的Stateless EJB中,實體管理器會在方法結(jié)束時(默認情況下開始和結(jié)束事務)將其狀態(tài)刷新到DB。 您可以在這里和這里閱讀有關(guān)此內(nèi)容的更多信息。

在Java EE 7之前的時代(EE 5和EE 6)也有可能

可以對Java EE 5和6進行調(diào)整,以實現(xiàn)與Java EE 7中的非同步持久性上下文所獲得的結(jié)果相同的結(jié)果。

想象一個用例,其中按順序(使用流程之類的向?qū)?#xff09;來編輯客戶詳細信息,例如屏幕1中的地址信息,屏幕2中的聯(lián)系信息等。您希望在客戶輸入是,但不希望將整個狀態(tài)推送到數(shù)據(jù)庫,直到該過程完成,即用戶輸入了所有類別的信息

package com.abhirockzz.conversationalee;import com.abhirockzz.conversationalee.entity.Customer; import java.util.Date; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.ejb.Remove; import javax.ejb.Stateful; import javax.ejb.TransactionAttribute; import javax.ejb.TransactionAttributeType; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContextType;@Stateful @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public class CustomerEditorFacade{@PersistenceContext(type = PersistenceContextType.EXTENDED)EntityManager em;@Inject //this won't work in Java EE 5Principal authenticatedUser;private Customer customer;@PostConstructpublic void init(){System.out.println("CustomerEditorFacade created at " + new Date().toString()); }@PreDestroypublic void destroy(){System.out.println("CustomerEditorFacade destroyed at " + new Date().toString()); }//step 1public void updateCity(String custID, String city){String custID = authenticatedUser.getName(); //assume we have an authenticated principal which is the same as the customer ID in the DatabaseCustomer customerFromDB = em.find(Customer.class, Integer.valueOf(custID)); //obtain a 'managed' entitycustomerFromDB.setCity(city); //no need to call em.persistcustomer = customerFromDB; //just switch references//Customer state will NOT be pushed to DB}//step 2public void updateEmail(String email){customer.setEmail(email); //not pushed to DB yet}@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)public void save(){//dummy method to trigger transaction and flush EM state to DB}@Removepublic void finish(){//optional method to provide a way to evict this bean once used//not required if this is session scoped}}

代碼注釋是自我解釋(希望如此)

干杯!

翻譯自: https://www.javacodegeeks.com/2015/12/pre-java-ee-7-alternative-jpa-2-1-unsynchronized-persistence-context.html

總結(jié)

以上是生活随笔為你收集整理的Java EE 7之前版本替代JPA 2.1的非同步持久性上下文的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。