簡體   English   中英

Hibernate vs Spring 連接獲取策略

[英]Hibernate vs Spring connection acquisition strategy

我試圖了解何時在使用 hibernate 作為 ORM 和 Spring 進行事務管理的應用程序中獲取數據庫連接。

這篇文章中,它說:

如果事務管理器決定創建一個新事務,那么它將:

創建一個新的實體管理器

將實體管理器綁定到當前線程

從數據庫連接池中獲取一個連接

將連接綁定到當前線程

但是這里說:

Hibernate 推遲數據庫連接獲取,直到當前事務必須執行其第一個 SQL 語句(由讀取或寫入操作觸發)。 這種優化允許 Hibernate 減少物理事務間隔,從而增加從池中獲得連接的機會。

如果 Spring 決定之前獲取它,我無法理解 Hibernate 如何延遲連接獲取。

I can't understand how can Hibernate delay the connection aquisition if Spring decides to aquire it before.

該決定由事務管理器做出,因此可能由JpaTransactionManager/HibernateTransactionManager

Hibernate 5.2.0 引入新屬性hibernate.connection.provider_disables_autocommit

有了這個 Hibernate 可以延遲數據庫連接獲取,直到有 JDBC 語句要執行。 否則進入@Transactional服務方法時獲取數據庫連接。

So to allow Spring wires this property into the JPATransactionManager during the setup of spring context you have to set this property in your application.properties/yaml --> spring.jpa.properties.hibernate.connection.provider_disables_autocommit

執行此操作所需的最后一件事->在連接池提供程序上將自動提交設置為 false(默認情況下 HikariCP 自 Spring 啟動 2) spring.datasource.hikari.auto-commit=false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM