In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
1. Knowledge of second-level cache
The cache provided by Hibernate: there are first-level cache and second-level cache. The purpose is to reduce the number of visits to the database and improve the efficiency of program execution!
First-level cache: Session-based cache, cache content is only valid in the current session, session is closed, cache content is invalid!
Features: the scope of action is small! The cache time is short. The caching effect is not obvious.
Second-tier cache:
Hibernate provides application-level caching that can span multiple session, that is, cached data can be accessed by different session. This cache is also called secondary cache.
The secondary cache provided by Hibernate has a default implementation and is a pluggable cache framework! If you want to use secondary cache, you only need to configure it in hibernate.cfg.xml; if you don't want to use it, remove it directly without affecting the code.
If users feel that the framework provided by hibernate is not easy to use, they can switch to another caching framework or implement their own caching framework.
The following configuration is located in% hibernate%/project/etc/hibernate.properties
# # Second-level Cache # # disable the second-level cache secondary cache is not enabled by default Need to manually open # hibernate.cache.use_second_level_cache false## enable the query cache#hibernate.cache.use_query_cache true to open query cache # # choose a cache implementation implementation of the secondary cache framework # hibernate.cache.provider_class org.hibernate.cache.EhCacheProvider#hibernate.cache.provider_class org.hibernate.cache.EmptyCacheProviderhibernate.cache.provider_class org.hibernate.cache.HashtableCacheProvider#hibernate.cache.provider_class org.hibernate.cache.TreeCacheProvider#hibernate.cache. Provider_class org.hibernate.cache.OSCacheProvider#hibernate.cache.provider_class org.hibernate.cache.SwarmCacheProvider
Cache concurrency strategy
Objects placed in the secondary cache are read-only; non-strict read and write; objects placed in the secondary cache can be read and written; (transaction-based strategy)
2. Use secondary cache
Second-level cache, using steps
1) enable secondary cache
True
2) specify the cache framework
Org.hibernate.cache.HashtableCacheProvider
3) specify which classes are added to the secondary cache
4) Test the secondary cache!
Sample code and configuration
Hibernate.cfg.xml
Com.mysql.jdbc.Driver jdbc:mysql:///test root root org.hibernate.dialect.MySQL5Dialect true false update True org.hibernate.cache.HashtableCacheProvider true
Department.java
Package com.rk.hibernate.cache;import java.util.Set;public class Department {private int deptId; private String deptName; private Set emps; private int version; public int getVersion () {return version;} public void setVersion (int version) {this.version = version } public int getDeptId () {return deptId;} public void setDeptId (int deptId) {this.deptId = deptId;} public String getDeptName () {return deptName;} public void setDeptName (String deptName) {this.deptName = deptName } public Set getEmps () {return emps;} public void setEmps (Set emps) {this.emps = emps;} @ Override public String toString () {return "Department [deptId=" + deptId + ", deptName=" + deptName + "]";}}
Department.hbm.xml
Employee.java
Package com.rk.hibernate.cache;public class Employee {private int empId; private String empName; private int salary; private Department dept; private int version; public int getVersion () {return version;} public void setVersion (int version) {this.version = version } public int getEmpId () {return empId;} public void setEmpId (int empId) {this.empId = empId;} public String getEmpName () {return empName;} public void setEmpName (String empName) {this.empName = empName } public int getSalary () {return salary;} public void setSalary (int salary) {this.salary = salary;} public Department getDept () {return dept;} public void setDept (Department dept) {this.dept = dept @ Override public String toString () {return "Employee [empId=" + empId + ", empName=" + empName + ", salary=" + salary + "]";}}
Employee.hbm.xml
App.java
Package com.rk.hibernate.cache;import java.util.Iterator;import java.util.List;import java.util.Set;import org.hibernate.Query;import org.hibernate.SQLQuery;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Test;public class App {private static SessionFactory sf Static {sf = new Configuration () .configure () .addClass (Department.class) .addClass (Employee.class) .buildSessionFactory () } / / 1. Test the use of secondary cache / / idea: sesion itself provides a first-level cache, it is mandatory, can not be turned off / / secondary cache, is optional, through the configuration file can be turned on, can be closed. / / if the secondary cache is not enabled, two session queries the data of the same id and send two SQL statements / / if the secondary cache is enabled, the two session query the data of the same id, and a SQL statement is sent / / check the number of SQL executed @ Test public void testSecondLevelCache () {/ the first query by opening and closing the secondary cache. The first session Session session1 = sf.openSession () Session1.beginTransaction (); Department dept1 = (Department) session1.get (Department.class, 3); Set emps1 = dept1.getEmps (); System.out.println (dept1); System.out.println (emps1); session1.getTransaction (). Commit (); session1.close () System.out.println ("- -"); / / second query, second session Session session2 = sf.openSession (); session2.beginTransaction () Department dept2 = (Department) session2.get (Department.class, 3); Set emps2 = dept2.getEmps (); System.out.println (dept2); System.out.println (emps2); session2.getTransaction (). Commit (); session2.close () } @ Test public void testQueryCache () {/ / first query, the first session Session session1 = sf.openSession (); session1.beginTransaction () / / HQL query [setCacheable specifies to find it from the secondary cache or put it into the secondary cache] Query Q1 = session1.createQuery ("from Department") .setCacheable (true); List list1 = q1.list (); System.out.println (list1); session1.getTransaction () .commit (); session1.close () System.out.println ("- -"); / / second query, second session Session session2 = sf.openSession (); session2.beginTransaction () / / HQL query [setCacheable specifies to find it from the secondary cache or put it into the secondary cache] Query Q2 = session2.createQuery ("from Department") .setCacheable (true); List list2 = q2.list (); System.out.println (list2); session2.getTransaction () .commit (); session2.close () }}
When we turn on the secondary cache, such as in the testSecondLevelCache () method above, the first time the data is read from Session, it is stored in the secondary cache; the second time we open Session, and the program makes the same query, there is no need to send the SQL statement, because it will read the data from the secondary cache.
One thing to note: if there is no data in the secondary cache, the first time you turn on Session to read the data, call the session.clear () method, and then read the data, the SQL statement will be sent twice.
@ Test public void test1 () {/ / first query, the first session Session session1 = sf.openSession (); session1.beginTransaction (); Department dept1 = (Department) session1.get (Department.class, 2); System.out.println (dept1); System.out.println (dept1.getEmps ()) Session1.clear (); dept1 = (Department) session1.get (Department.class, 2); System.out.println (dept1); System.out.println (dept1.getEmps ()); session1.getTransaction (). Commit (); session1.close ();}
The results are as follows:
Hibernate: select department0_.id as id0_0_, department0_.dept_version as dept2_0_0_, department0_.name as name0_0_ from T_Department department0_ where department0_.id=?Department [deptId=2, deptName=woqu] Hibernate: select emps0_.deptId as deptId0_1_, emps0_.id as id1_, emps0_.id as id1_0_, emps0_.emp_version as emp2_1_0_, emps0_.name as name1_0_, emps0_.salary as salary1_0_ Emps0_.deptId as deptId1_0_ from T_Employee emps0_ where emps0_.deptId=? [Employee [empId=3, empName=TO_T_, salary=4]] Hibernate: select department0_.id as id0_0_, department0_.dept_version as dept2_0_0_, department0_.name as name0_0_ from T_Department department0_ where department0_.id=?Department [deptId=2, deptName=woqu] Hibernate: select emps0_.deptId as deptId0_1_, emps0_.id as id1_, emps0_.id as id1_0_ Emps0_.emp_version as emp2_1_0_, emps0_.name as name1_0_, emps0_.salary as salary1_0_, emps0_.deptId as deptId1_0_ from T_Employee emps0_ where emps0_.deptId=? [Employee [empId=3, empName=TO_T_, salary=4]]
If the data already exists in the secondary cache, the second time you open Session, call session.clear (), and then read the data, no SQL statement will be sent.
Test public void test1 () {Session session2 = sf.openSession (); session2.beginTransaction (); Department dept2 = (Department) session2.get (Department.class, 2); System.out.println (dept2); System.out.println (dept2.getEmps ()) Session2.getTransaction (). Commit (); session2.close (); System.out.println ("-") / / the first query, the first session Session session1 = sf.openSession (); session1.beginTransaction (); Department dept1 = (Department) session1.get (Department.class, 2); System.out.println (dept1) System.out.println (dept1.getEmps ()); session1.clear (); dept1 = (Department) session1.get (Department.class, 2); System.out.println (dept1); System.out.println (dept1.getEmps ()) Session1.getTransaction (). Commit (); session1.close (); System.out.println ("-");}
The results are as follows:
Hibernate: select department0_.id as id0_0_, department0_.dept_version as dept2_0_0_, department0_.name as name0_0_ from T_Department department0_ where department0_.id=?Department [deptId=2, deptName=woqu] Hibernate: select emps0_.deptId as deptId0_1_, emps0_.id as id1_, emps0_.id as id1_0_, emps0_.emp_version as emp2_1_0_, emps0_.name as name1_0_, emps0_.salary as salary1_0_ Emps0_.deptId as deptId1_0_ from T_Employee emps0_ where emps0_.deptId=? [Employee [empId=3, empName=TO_T_, salary=4]]-- Department [deptId=2, deptName=woqu] [Employee [empId=3, empName=TO_T_, salary=4]] Department [deptId=2, deptName=woqu] [Employee [empId=3, empName=TO_T_] Salary=4]]--
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.