Hibernate cache exploration

Hibernate cache exploration

                                                                                                        - I've never really trusted my memory, so I write them all down

       The ease of use of hibernate has benefited many developers, and I like her very much; but in some places, it is easy to fall into the pit when using it without in-depth exploration; for example, when inserting or updating in large quantities, there may be memory overflow. . Let's take a look at the first level cache of hibernate

 

Hibernate first level cache

       Hibernate's first-level cache is also called Session cache, session cache. As the name suggests, the first-level cache is based on each session. For example, if I update 100 tables in a session, then these 100 entities will exist in the first-level cache. Only after this transaction is completed, the session destruction will release the cache. If the operation data is too large, it may cause memory overflow.

       Hibernate first-level cache is forced to open and cannot be canceled. But two methods evict() and clear() are provided. The evict() method clears the specified object cache, and clear() means clears all caches.

       When performing mass updates or inserts, the connection is basically obtained directly and then operated through JDBC, and the performance is improved. Hibernate 3.3.2 and above use this method to obtain the connection, SessionFactoryUtils.getDataSource(getSessionFactory()).getConnection() , which can then be operated via JDBC. Of course, stored procedures perform best.

* The life cycle of the first-level cache is the same as that of the Session. When the Session is destroyed, it is also destroyed.

 

Hibernate second level cache

       The first-level cache can only be used in the current session. In many cases, all sessions share the cache. The second-level cache is used to implement this scheme. The second-level cache is at the SessionFactory level. Compared with the first-level cache, which is enabled by default, the second-level cache is a pluggable cache and needs to be manually enabled . The default is Ehcache, other HashTables, OSCache, etc.

*The second level cache is at the application level, and the first level cache is at the session level

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326231050&siteId=291194637