SSH [SSH] Hibernate framework of: Hibernate's cache

Hibernate cache

Hibernate cache is a way to optimize the program Hibernate provided from the database query to the data stored in memory to avoid frequent access to the database when the cache data needs of database queries do not need to go to do so not only can improve access speed, but also to avoid the consumption of computer performance because of frequent access to the database caused.
Hibernate provides two levels of cache: a cache and L2 cache.
Cache : Session cache is level cache, Session consistent with where their life cycle, Hibernate's cache is automatically opened, not closed;
secondary cache : secondary cache is SessionFatory level cache, its life where consistent with the period SessionFactory, Hibernate secondary cache is not turned on by default, you can be configured to turn it on.
A cache of the Session influence methods :

  • close (): close the current Session, the corresponding cache will be cleared;
  • clear (): Empty all caches in the current Session;
  • evict (Object obj): specify which object to empty the cache.

Testing a cache exists :

package cn.jingpengchong.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import cn.jingpengchong.pojo.Customer;

public class Test {
	
	public static void main(String[] args) {
		//1、加载配置文件
		Configuration configure = new Configuration().configure();
		
		//2、创建一个SessionFactory
		SessionFactory factory = configure.buildSessionFactory();
		
		//3、根据SessionFactory获得一个Session对象
		Session session = factory.openSession();
		
		//4、手动开启事务
		Transaction transaction = session.beginTransaction();
		
		//5、执行增删改查等操作
		Customer customer1 = session.get(Customer.class, 3l);
		System.out.println(customer1);
		Customer customer2 = session.get(Customer.class, 3l);
		System.out.println(customer2);
		System.out.println("是否为同一个对象:" + (customer1 == customer2));
		
		//6、提交事务
		transaction.commit();
		
		//7、释放资源
		session.close();
		factory.close();
	}
}

If no cache, we respect the results of the program should be straightforward, but because of the cache with the following results:
Here Insert Picture Description
Analysis Principle database automatically updates the persistent state of an object :
the persistent state objects mentioned in the blog post will automatically update the database the reason is because the first-level cache hibernate, hibernate when the first query is actually the objects found in the buffer zone and put on a snapshot. Modified data is cached area and found the cache, and Snapshots area is not the same, so it will perform updates when the transaction is committed; if not modify the data, when the transaction is committed and snapshot buffer zone is still the same, it will not update the database.

Published 128 original articles · won praise 17 · views 2726

Guess you like

Origin blog.csdn.net/qq_43705275/article/details/104229004