Hibernate iterator JCs Analysis

Source: Internet
Author: User

 

JCS is an object cache that caches Java objects to Improve the access efficiency of frequently accessed Java objects. JCs accesses an object based on the unique identifier of the object. For example, it can be accessed Based on the hashcode of the object. For hibernate, JCs can be used to cache the query results, so that when the same data is accessed next time, it does not need to be retrieved from the database and retrieved directly from JCs, speeding up the query speed. When hibernate uses the list or iterator method to read data for the first time, JCS is empty. In this case, both the list method and the iterator method will fill the queried Persistent Object in JCs, for example: select C from cat as C and select C. ID, C. the name from cat as C hql statement does not construct Po, so it does not fill in JCs. Okay. Now the data in JCs has been filled in, but how can this problem be obtained? As I mentioned above, it is accessed according to the unique identifier of the object. For a po Persistent object, the unique identifier is the primary key. Therefore, Hibernate must first obtain the primary key list and then judge it one by one based on the primary key list, check whether the Persistent object is in JCs or in the database. if the object is in JCs, it is obtained based on the primary key. If the object is in the database, an SQL query is sent. Now let's analyze why iterator can use JCs, but list cannot. As mentioned above, before using JCs, you must first obtain the primary key of the Persistent Object to retrieve the Persistent object from JCs. How can we obtain the primary key list? It must be obtained from the database. This step cannot be buffered. Hibernate iterator queries are divided into two steps: ==> select ID from cat ==> select * from cat where id =? ==> Select * from cat where id = ?... ==> Select * from cat where id =? Step 1: retrieve the primary key list from the database. Step 2: retrieve data one by one based on the primary key. Of course, we can now see that if JCS is not used in the iterator mode, it would take n + 1 SQL query to retrieve N records from the database, which is terrible, therefore, if JCS is not used, You must retrieve a large amount of data at a time. Avoid using iterator. The first SQL statement of iterator is to retrieve the primary key list, which consumes a very small amount of time. If JCS is used, it is inevitable to send an SQL statement for each query: select ID from cat to retrieve the primary key list. What then? Then iterator will not be so silly. He will first go to JCS to find the Persistent Object Based on the primary key. If yes, use it directly. If no, so I had to get it from the database and then fill it in JCs. Therefore, it can be seen that JCS is a bit similar to a simple object database in the memory. The iterator's first SQL command to obtain the primary key list must be obtained in the database. After obtaining the primary key, iterator will first try to open the JCS lock, open it and go in directly. If it cannot be opened, it will have to open the database lock. The Hibernate list method is a simple encapsulation of JDBC. All the data is retrieved from an SQL statement. It does not take the primary key and then the data as the iterator does. Therefore, the list cannot use JCs. However, list can also fill the data retrieved from the database into JCs. Best way: Use list for the first access and quickly fill in JCs. Use iterator for later access to make full use of JCs. Mikeho write: How does JCs maintain synchronization with the database? Robbin write: JCs. Default. elementattributes. maxlifeseconds = 240 (maximum buffer time) timeout to invalidate. In addition, you can also clear JCs cache in the program.

 

JCS is an object cache that caches Java objects to Improve the access efficiency of frequently accessed Java objects. JCs accesses an object based on the unique identifier of the object. For example, it can be accessed Based on the hashcode of the object. For hibernate, JCs can be used to cache the query results, so that when the same data is accessed next time, it does not need to be retrieved from the database and retrieved directly from JCs, speeding up the query speed. When hibernate uses the list or iterator method to read data for the first time, JCS is empty. In this case, both the list method and the iterator method will fill the queried Persistent Object in JCs, for example: select C from cat as C and select C. ID, C. the name from cat as C hql statement does not construct Po, so it does not fill in JCs. Okay. Now the data in JCs has been filled in, but how can this problem be obtained? As I mentioned above, it is accessed according to the unique identifier of the object. For a po Persistent object, the unique identifier is the primary key. Therefore, Hibernate must first obtain the primary key list and then judge it one by one based on the primary key list, check whether the Persistent object is in JCs or in the database. if the object is in JCs, it is obtained based on the primary key. If the object is in the database, an SQL query is sent. Now let's analyze why iterator can use JCs, but list cannot. As mentioned above, before using JCs, you must first obtain the primary key of the Persistent Object to retrieve the Persistent object from JCs. How can we obtain the primary key list? It must be obtained from the database. This step cannot be buffered. Hibernate iterator queries are divided into two steps: ==> select ID from cat ==> select * from cat where id =? ==> Select * from cat where id =?... ==> Select * from cat where id =? Step 1: retrieve the primary key list from the database. Step 2: retrieve data one by one based on the primary key. Of course, we can now see that if JCS is not used in the iterator mode, it would take n + 1 SQL query to retrieve N records from the database, which is terrible, therefore, if JCS is not used, You must retrieve a large amount of data at a time. Avoid using iterator. The first SQL statement of iterator is to retrieve the primary key list, which consumes a very small amount of time. If JCS is used, it is inevitable to send an SQL statement for each query: select ID from cat to retrieve the primary key list. What then? Then iterator will not be so silly. He will first go to JCS to find the Persistent Object Based on the primary key. If yes, use it directly. If no, so I had to get it from the database and then fill it in JCs. Therefore, it can be seen that JCS is a bit similar to a simple object database in the memory. The iterator's first SQL command to obtain the primary key list must be obtained in the database. After obtaining the primary key, iterator will first try to open the JCS lock, open it and go in directly. If it cannot be opened, it will have to open the database lock. The Hibernate list method is a simple encapsulation of JDBC. All the data is retrieved from an SQL statement. It does not take the primary key and then the data as the iterator does. Therefore, the list cannot use JCs. However, list can also fill the data retrieved from the database into JCs. Best way: Use list for the first access and quickly fill in JCs. Use iterator for later access to make full use of JCs. Mikeho write: How does JCs maintain synchronization with the database? Robbin write: JCs. Default. elementattributes. maxlifeseconds = 240 (maximum buffer time) timeout to invalidate. In addition, you can also clear JCs cache in the program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.