首頁  >  文章  >  Java  >  Hibernate二級快取是如何運作的?

Hibernate二級快取是如何運作的?

PHPz
PHPz轉載
2023-09-14 19:45:021209瀏覽

Hibernate二級快取是如何運作的?

快取有助於減少執行查詢時的資料庫網路呼叫。

一級快取與會話連結。它是隱式實現的。一級緩存存在 直到會話物件存在為止。一旦會話物件終止/關閉,將會有 沒有緩存物件。二級緩存適用於多個會話物件。它是連結的 與會話工廠。二級緩存物件可供所有會話使用 單會話工廠。當特定會話發生時,這些快取物件將被終止 工廠已關閉。

實作二級快取

我們需要新增以下相依性才能使用二級快取。

<!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache -->
<dependency>
   <groupId>net.sf.ehcache</groupId>
   <artifactId>ehcache</artifactId>
   <version>2.10.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-ehcache</artifactId>
   <version>5.4.32.Final</version>
</dependency>

注意- hibernate ehcache 版本號碼必須與 hibernate 版本號碼相同。

現在,我們需要添加 hibernate 設定文件,這將使 hibernate 能夠連接到 提供的資料庫並使用二級快取。

<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
   "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <!-- JDBC Database connection settings -->
      <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
      <property name="connection.url">jdbc:mysql://localhost:3306/demo?useSSL=false</property>
      <property name="connection.username">root</property>
      <property name="connection.password">root</property>
      <!-- JDBC connection pool settings ... using built-in test pool -->
      <property name="connection.pool_size">4</property>
      <!-- Echo the SQL to stdout -->
      <property name="show_sql">true</property>
      //caching properties
      <property name="cache.use_second_level_cache">true</property>
      <property name="cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
      <!-- Select our SQL dialect -->
      <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
      <!-- Drop and re-create the database schema on startup -->
      <property name="hbm2ddl.auto">create-drop</property>
      <!-- name of annotated entity class -->
      <mapping class="academy.company.Parent"/>
   </session-factory>
</hibernate-configuration>

範例

預設情況下,java 中的所有實體都是不快取的。因此,為了啟用實體的緩存,我們在實體類別 Parent 中使用 @Cacheable 和 @Cache 註解 -

import org.hibernate.annotations.CacheConcurrencyStrategy;
import javax.persistence.*;

@Entity
@Table( name = " Employee")
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class Parent {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   Long id;
   @Column(name = "first_name")
   String firstName;
   @Column(name = "last_name")
   String lastName;
}
Now, let’s check whether second level cache works:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Main {
   public static void main(String[] args) {
      SessionFactory sessionFactory = new Configuration()
         .configure("academy/company/hibernate.cfg.xml")
         .buildSessionFactory();
      Session session1 = sessionFactory.openSession();
      Parent parent1 = session1.get(Parent.class, 4);
      System.out.println(parent1.id + " " + parent1.firstName + " " + parent1.lastName);
      session1.close();
      
      Session session2 = sessionFactory.openSession();
      Parent parent2 = session2.get(Parent.class, 4);
      System.out.println(parent2.id + " " + parent2.firstName + " " + parent2.lastName);
      session2.close();
   }
}

輸出

Hibernate: select parent0.id as id1, parent0.first_name as first_name1, parent0.last_name as last_name1 from Parent parent0 where parent0.id=?
1 Subash Chopra
1 Subash Chopra

從控制台我們可以清楚地看到hibernate在session1期間只執行了一次查詢。現在,當 session2 存取相同的查詢時,它不會對資料庫進行網路呼叫來執行它。相反,由於我們使用二級緩存,它將從 session1 獲取緩存物件。

以上是Hibernate二級快取是如何運作的?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除