目前正在開發一個使用休眠和 MySQL 的應用程式。我在我的日志中看到了以下輸出,這讓我很驚訝:
Hibernate: select user0_.id as id1_3_, user0_.email as email2_3_, user0_.name as name3_3_ from test_user user0_ order by user0_.name asc limit ?, ?
Hibernate: select count(user0_.id) as col_0_0_ from test_user user0_
2022-05-11 10:49:23.650 INFO 21384 --- [ main] i.StatisticalLoggingSessionEventListener : Session Metrics {
441906 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
2709671 nanoseconds spent preparing 2 JDBC statements;
8478990 nanoseconds spent executing 2 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
16820 nanoseconds spent executing 2 partial-flushes (flushing a total of 0 entities and 0 collections)
}
我沒想到會有這個輸出,因為:
- 有 2 個查詢對應于 2 個 JDBC 陳述句
- 有 0 個批次
- 只有一個 JDBC 連接
我所期望的是以下之一:
- 1個批處理、2個陳述句和一個連接來執行批處理
- 0 個批次,2 個陳述句,2 個連接(每個陳述句一個)
怎么可能有 0 個批次并且仍然在一個連接中執行 2 個查詢?
uj5u.com熱心網友回復:
聽起來您認為批處理 == 事務,但這不是 JDBC 中使用的含義。在 Java/JDBC 中,批處理執行具有特定的含義,我假設 Hibernate 使用相同的含義:使用Statement.executeBatch()執行多個更新/洗掉/插入陳述句。這絕對不適用于這里,因為您不能在 JDBC 中批量選擇,即使對于更新/洗掉/插入,您通常也只會在有多組引數要在單個準備好的陳述句上執行時進行批處理。
在這里,Hibernate 在同一個連接上(并且在同一個事務中)執行第一條陳述句,然后執行第二條陳述句。根據需要,它可能會交錯處理兩個 select 陳述句的結果集(當不自動提交時,JDBC 允許來自不同陳述句物件的多個打開的結果集)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/473495.html
下一篇:錯誤org.springframework.beans.factory.BeanCreationException:創建類路徑資源中定義的名稱為“entityManagerFactory”的bean時
