Further improve the performance of JDBC applications (5)

zhaozj2021-02-08  254

Bootcool@263.net

Five: Through the object reference, reuse result set

When the program developer races the brain to improve the program performance, we must first want to use buffer technology in the program. In a large number of C / S mode-based applications, we can use it from two different angles. From the perspective of the client, we can buffer the data to the local machine, avoiding the same network operation; from the server side, we can also buffer those frequently used, thus shortening the response time of the server on the same request. Reasonably uses buffer technology to effectively improve program performance.

This often occurs, and one or more object instances in Java programming are repeated during program operation. More specifically, in JDBC programming, users often use a few query statements to query, each execution query returns the same result set, this time we have to consider: If the result set of repeated use should be buffered ? How to use these result sets? How can users guarantee these result sets when users update data?

Here, we will specifically discuss how to achieve buffering of results set in JDBC programming. First, we must clarify the following concepts.

(1): If you want to use the class in Java program, you have to instantiate the class into an object. After the class instantiates the object, it is necessary to assign a corresponding memory space to store the instance. This memory can be operated by subject references.

(2): In the JDBC, execute the result set returned by the query operation, in fact, a connection to the database, and the required data can be touched from the database from the database through the cursor operation. We use the chart to further explain:

(3): The buffering of the result set is actually buffering the results set object specified in the Java heap, and it is more precisely that the reference to buffer the object should be buffered.

Based on the above principles, we decided to use the HashTable in the Java Util package to achieve buffering of the result set objects. We use the query statement itself as the logo of the result set object, which is the HashTable key field, and the Value field stores a vector object. This vector stores the reference "pointer" (temporarily called pointer) of ResultSet; Statement or PreparedStatement's reference "pointer"; the number of records of the result set; if the result set features rolling characteristics (new features of JDBC2.0 API) The position of the current cursor can also be stored. The content stored can be adjusted according to the actual situation, but the reference "pointer" and Statement's reference "pointer" must be saved. The specific structure is shown below:

However, we have found that when the memory space is large, and the object that needs to be caught is not too much, using HashTable is indeed the most cost-effective approach. However, as the number of objects that need to be caching is increasing, the memory space occupied by HashTable is getting bigger and bigger. In order to reduce the consumption of memory and the purpose of cache objects can be achieved. We decided to set up a maximum, replacing the least commonly used ResultSet when the number of cache objects exceeds the upper limit. The figure below is a strengthened cache structure:

(Note: This data structure and the LRU algorithm have some introduction in the Database Pilot package in JBuilder6.0.)

Of course, in order to reduce system consumption, the replacement algorithm used is not too complicated. Alternatively, ArrayList and HashMap can be used instead of Vector and HashTable, which improves a little efficiency. But ArrayList and HashMap are unsafe threads, while Vector and Hashtable are just the opposite. When we build a cache that needs to support multi-thread access, it is difficult to control it when we use ArrayList and HashMap. So we sacrifice the efficiency here to improve the stability of the program. Finally, you have to explain that when an object instance is removed from the cache. Should be followed as follows:

1. Use the close () method to close the resultSet.

2. Use the close () method to close the statement that creates the result set (statement, preparedstatement).

3. Remove the corresponding Key and its value in the HastTable.

4. Remove Key from the Vector.

转载请注明原文地址:https://www.9cbs.com/read-2654.html

New Post(0)