第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在Spring事務(wù)外執(zhí)行JPA代碼

如何在Spring事務(wù)外執(zhí)行JPA代碼

qq_遁去的一_1 2023-07-28 16:42:54
我想優(yōu)化只讀查詢,所以我想在事務(wù)之外執(zhí)行 JPQL 查詢,以便從 JPQL 返回的實體不會在當(dāng)前持久性上下文中進行管理(為了節(jié)省臟檢查和不必要的管理這些實體的成本,以節(jié)省 2 倍的大?。┰诔志眯陨舷挛闹校N以谙旅鎳L試過,@Repositorypublic class CustomPostRepository {  @Autowired  private EmployeeRepository employeeRepository;  @Transactional  public void foo() {      // Some other transactional code (read-write) here ...      final TransactionStatus transactionStatus = TransactionAspectSupport.currentTransactionStatus(); // returns current active transaction      System.out.println("transactionStatus.isNewTransaction() = " + transactionStatus.isNewTransaction()); // returns TRUE      List<Post> posts = employeeRepository.bar();     // Some other code here...  }}@Repositorypublic class CustomJpaEmployeeDao implements EmployeeRepository {    @PersistenceContext    private EntityManager em;    @Override    @Transactional(propagation = Propagation.NOT_SUPPORTED)    public List<Post> bar() {        // Ideally this method should throw Exception, as transaction is expected to be suspended (not exists here)        final TransactionStatus transactionStatus = TransactionAspectSupport.currentTransactionStatus();        final List<Post> post = em.createNamedQuery("Post.findAllForReporing", Post.class).getResultList();        System.out.println("entityManager.contains(post) = " + em.contains(post.get(0))); // returns true, which should be false        return post;    }}呼叫者,@Testpublic void jpqlTest() {    customPostRepository.foo();}我想EmployeeRepository#bar在事務(wù)之外執(zhí)行(由 C 創(chuàng)建ustomPostRepository#bar),但Propagation.NOT_SUPPORTED不起作用,并且來自 JPQL 的所有選定實體都在當(dāng)前持久性上下文中進行管理。那么,有什么方法可以避免管理這些實體或在事務(wù)之外執(zhí)行代碼呢?或者這里缺少什么?
查看完整描述

1 回答

?
慕村9548890

TA貢獻1884條經(jīng)驗 獲得超4個贊

您應(yīng)該刪除@Transactional注釋。在這種情況下,除非方法本身不拋出異常,否則不會產(chǎn)生任何影響LazyInitializationException。如果存在需要事務(wù)的情況,則必須為其創(chuàng)建一個單獨的方法/從同一個 bean 調(diào)用它,這樣就不會創(chuàng)建代理。

也不EntityManager#contains檢查進程是否在事務(wù)內(nèi)運行或?qū)嶓w是否經(jīng)過臟檢查。

如果您不想/無法更改這些方法,請使用EntityManager#detach.


查看完整回答
反對 回復(fù) 2023-07-28
  • 1 回答
  • 0 關(guān)注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號