Hibernate cache []

I. Overview

  • What is cache:
    • Cache database / data file on your hard disk, put into the cache (that is, a memory space). When the re-use of use, can be obtained directly from memory.
  • The benefits of caching:
    • Efficiency improvement program is running. Caching technology is a means to an optimized Hibernate.
  • Hibernate is divided into two basic caching
    • Cache:. Session-level cache-coherent cache and session lifecycle own can not be uninstalled...
    • Secondary cache:. SessionFactory-level cache is not built-in.
  • Session interface is included in the realization of a series of Java collections, these constitute a collection of Java Session cache as long as the Session instance is not the end of the life cycle, it is stored in the cache object will not be the end of the life cycle.

Second, prove the existence of a cache of Hibernate

@Test
// 证明一级缓存的存在
public void demo3(){
    // 1.创建Session
    Session session = HibernateUtils.openSession();
    // 2.开启事务
    Transaction tx = session.beginTransaction();        
    // save方法可以向一级缓存中存放数据的.
    /*Book book = new Book();
    book.setName("JQuery开发");
    book.setAuthor("张XX");
    book.setPrice(45d);
    
    Integer id = (Integer) session.save(book);
    
    Book book2 = (Book) session.get(Book.class, id); 
    
    System.out.println(book2);*/
    
    // 分别用get执行两次查询.
    Book book1 = (Book) session.get(Book.class, 1);// 马上发生SQL去查询
    System.out.println(book1);
    
    Book book2 = (Book) session.get(Book.class, 1);// 不发生SQL,因为使用一级缓存的数据
    System.out.println(book2);
    
    // 3.提交事务
    tx.commit();
    // 4.关闭资源
    session.close();
}

Third, a snapshot of the cache area

@Test
    // 深入理解一级缓存结构:快照区:
    public void demo4(){
        // 1.创建Session
        Session session = HibernateUtils.openSession();
        // 2.开启事务
        Transaction tx = session.beginTransaction();
        
        // 获得一个持久态的对象.
        Book book = (Book) session.get(Book.class, 1);
        book.setName("Spring3开发");
        
        // 3.提交事务
        tx.commit();
        // 4.关闭资源
        session.close();
    }

  • Conclusion: When stored data to a cache, placed in a buffer and cache snapshot area, when updating the data cache when the transaction once submitted, comparison and snapshot cache area, if consistent data is not updated, if inconsistent data, automatically update the database.

Fourth, the management level cache

  • Level cache is associated with the session ending the life cycle of .session life cycle, a cache will be destroyed
  • clear()/evict()/flush()/refresh(): Management cache.
  • clear(): Clear all cache objects.
  • evict(Object obj) : Empty the cache in an object.
  • flush(): Brush cache.
  • refresh(Object obj): A snapshot of the data re-cover the area of ​​a cache of data.

Five, Hibernate cache timing of the brush

  • FlushMode constant
    • ALWAYS: Every time the query will be invoked manually flush out the brush when the transaction is committed...
    • AUTODefault: Some queries will brush out manually call flush when the transaction is committed....
    • COMMIT: When the transaction is submitted manually call flush times.
    • MANUAL: Only manually call will flush out the brush.
  • Stringency: MANUAL> COMMIT> AUTO> ALWAYS

Sixth, the operation method of persistent object

  • 1.save()
    • Keeps a record: the instantaneous state object into a persistent state object.
  • 2.update()
    • Updating a record: the detached state object into a persistent state object.
    • in Setting select-before-update on the label = "true" to go before the update query
  • 3.saveOrUpdate()
    • The status of the object to obtain a different update method different execution save.
    • If the object is a transient state of the object: perform the save operation.
    • If the object is a detached state of the object: execute the update operation.
    • Set id does not exist, an error, you can Provided on an unsaved-value = - perform the save operation, "1".
  • 4.delete()
    • The persistent state object into a transitory state.
  • 5.get()/load()
    • Obtaining a persistent state object.

Guess you like

Origin www.cnblogs.com/haoworld/p/hibernate-yi-ji-huan-cun.html