![](/img/trans.png)
[英]Connection lock using infinispan and hibernate on distributed cache l2
[英]Spring Data / Hibernate - don't automatically open JDBC connection before checking L2 cache
我們使用 Spring 數據 JPA 並成功配置了 L2 實體和查詢緩存。 但仍然困擾我的是 Hibernate 總是在檢查 L2 緩存之前打開 JDBC 連接。 由於數據庫非常繁忙,這有時會導致問題(打開的連接過多)。
有沒有辦法讓 Hibernate 在 L2 緩存未命中后僅打開 JDBC 連接?
澄清一下,這里是 session 指標:
i.StatisticalLoggingSessionEventListener : Session Metrics {
1716707 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
0 nanoseconds spent preparing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
236767 nanoseconds spent performing 1 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}
因此,即使絕對沒有對數據庫執行查詢,JDBC 連接仍然打開。
感謝 Christian Beikov 給出的指示,我發現 jdbc 連接對於圍繞操作的事務是必需的。
為默認 Spring 數據 JPA 存儲庫實現中的方法啟用的是@Transactional
。 覆蓋這些方法之一並留下注釋會導致所有內容僅在緩存上工作,而不涉及任何 jdbc 連接。
盡管刪除@Transactional
似乎可以解決您的問題,但實際上並非如此。 您想為此延遲獲取連接,您需要做一些額外的配置
spring.jpa.open-in-view=false
這將禁用 Open Session In Viewspring.datasource.hikari.auto-commit=false
,這將優化 Hibernate 不設置這個(你的應用程序的性能)這應該會阻止 Hibernate 在實際需要時實際獲得物理連接。 即使在事務內部(因為它現在不需要檢查連接以進行自動提交)。
我假設您正在使用 Spring 引導,如果不是至少將數據源的autoCommit
屬性設置為false
應該已經解決了這個問題(如果您使用的是最近的 Spring(引導)版本。)。
另請參閱 Vlad Mihalcea 的這個解釋(JPA/Hibernate 相關內容的 goto 源:))。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.