Hibernate (b) buffering mechanism

                                         Hibernate cache mechanism

       Cache (Cache): very general concept of the computer field. It lies between the application and the persistent data storage source ( such as a file or database on the hard disk ) between, whose function is to reduce the frequency of application to directly read and write persistent data storage source, thereby improving the performance of applications running. Data in the cache is a data storage source copy of the data. Physical medium is typically a cache memory, reading and writing so fast. However, if the amount of data stored in the cache is very large, it will use hard disk as a cache medium. Cache implementations not only to consider the storage medium, but also taking into account the life cycle management of concurrent access to the cache and cache data.

A . Why use Hibernate cache:

    Hibernate is an ORM framework, would often visit the physical database. Therefore, in order to reduce the application access to the database of frequency, thereby improving the operational performance of the application.

Two . Hibernate two kinds of cache (session cache and sessionFactory cache ) :

1. Session cache.

           Session built-in cache can not be uninstalled, Session caching is to cache the transaction scope, that corresponds to the life cycle of a transaction or a database application transaction (eg buy tickets this action, stock minus one, plus a buyer, this is a transaction), a cache, each instance of the persistent class has a unique the OID , it is stored in a cache copy of the database data.

(1) persistent objects: data (e.g., memory objects) may be saved to the permanent storage memory device (e.g., disk). The main application is persistent objects stored in the memory relational database, of course, it can also be stored in a disk file, XML data files, and so on.

Persistent object has three states, namely:

1, the configuration of the object - the session is not a transient state, there is no database

2, persistent state - there is session management session in the database have

3, free state (state managed) - Session not cache, not associated with any instance of Session. Correspondingly record exists in the database. (Provided that no other instance of Session to delete this record).

Specific conversion process:

       New new out of objects such as Student stu = new Student (), is a transient object that exist in isolation in memory, its meaning is carried by the carrier of information, and data in the database does not have any relevance.

      By Session 's save () or saveOrUpdate () method can be a transient object associated with the database, and the information carried by transient objects inserted into the database by mapping profiles made, the transient object is transformed into persistent objects

      If this time using the delete () method removes the stud object, it becomes transient objects back, delete records in the database associated with the object this object database no longer has any relevance

      Use get (), load () methods to query data objects, beginning persistent objects ) , and database records and has the same id identification (Hibernate automatically id value assigned to it ).

      When a Session specified close () or clear (), evict () after persistent objects becomes free state. Free reference object is still valid, the object can continue to be modified, when it is re-associated with a new Session when on, will become a persistent object again ( changes during the free state will be persisted to the database ( dirty checking ) ) . Free objects that have identification value database ID , it can update (), saveOrUpdate (), lock () or the like, again associated with the persistence layer.

                                      

(2) Check the dirty

         Dirty data : data before and after the state change occurs

         We look at the following code:


Transaction tx=session.beginTransaction();

User user=(User)session.load(User.class,”1”);//从数据库中加载符合条件的数据

user.setName(“zx”);//改变了user对象的姓名属性,此时user对象成为了所谓的“脏数据”

tx.commit();

      When the transaction commits, the Hibernate will session in PO ( persistent object ) is detected, determining whether the state of the persistent object has changed, if changed will be changed to update the database. There is a problem here, Hibernate whether the changes before and after to determine how an entity object's state. That Hibernate is how to check out a data already dirty.

      Check the usual dirty data in the following two ways: 

         A , data monitoring objects:

        Monitoring data object is a data object interceptors by setter to implement methods to monitor, which is similar to the concept of triggers in the database, when the attribute of an object called a setter method has changed, then the interceptor will this capture action, and the change of attribute flag has been changed, the operation of the database after update to the database. The advantage of this method is to improve the synchronization of data updates, but it is also its drawback, if an entity object has many properties have changed, it will inevitably cause a large number of call interceptor callback methods, these interceptors are by Dynamic Proxy or CGLIB achieved in the implementation of execution will pay a certain price, it may cause a greater delay update operations.

        B , version of the data comparison :

        This is to save the data object persistence framework in the most recent version read data when submitting data will be presented with the saved version for comparison, if a change occurs it will find its synchronization with the New database. This method reduces the real-time synchronous update, but many properties when a data object is changed, due to the persistent presence of the layer of the frame buffer, can take advantage of cached versions match, but it reduces the delay of the update data.

       In Hibernate is employed than the version of the data to perform the method of checking dirty

Copied from : https: //blog.csdn.net/zhangzeyuaaa/article/details/18353127

       

    2.SessionFactory cache. Since SessionFactory entire process lifecycle and application of the corresponding object, so Hibernate second level cache is a cache cluster-wide or process-wide, there may be concurrency issues, and therefore need to be taken to achieve concurrent access policy transaction isolation. Popular point : based on application-level cache, you can span multiple session , that is, different session can access cached data

SessionFactory cache can be divided into two categories :

       Built-in cache : SessionFactory built-in cache and Session cache similar in terms of implementation, the former is SessionFactory data object contains a collection of attributes, which refers to the Session data contained in a collection of attributes. Stored in the built-in cache metadata and mapping a predefined SQL statement mapping metadata file is a copy of the mapping data, the predefined SQL statements in Hibernate derived mapping metadata according to an initialization phase, the SessionFactory a built-in cache is read the application can not be modified cache mapping metadata and predefined SQL statements, and therefore SessionFactory does not need to be synchronized with the built-in cache mapping file.

       External cache ( secondary cache ): SessionFactory external configurable cache is a plug. By default, SessionFactory will not enable this plug-in. External data cache copy of the database data, the external cache may be a memory or a hard disk medium. SessionFactory external cache is also referred to as Hibernate second level cache. The second level cache is optional, may be arranged on a second level cache size for each class or each set.

    What data is stored in the secondary cache for it?

    1 ) Data are rarely modified

    2 ) less important data, allowing occasional concurrent data

    3 ) data will not be concurrent access

    4 ) constant data

Three . Hibernate cache is cleared when:

    1. commit () method is called when.

    2. The cache is cleared when the query is executed, thus ensuring the results reflect the latest state of the object.

    3. explicit call session is flush () method.

4. When the object using the native time generator, will immediately clear the cache insert records into the database.

Integrated from: (1) https://blog.csdn.net/xc121566/article/details/78830253

              (2)https://www.jb51.net/article/113116.htm

 

Published 14 original articles · won praise 1 · views 5529

Guess you like

Origin blog.csdn.net/Vpn_zc/article/details/82784003