Compared with Hibernate, IBatis, Dbutils, and so on, the theoretical JDBC performance is more than them. JDBC provides a more granular data access strategy at the bottom, which is not available in frameworks such as Hibernate.
In some high-performance data operations, the more advanced the framework is more unsuitable to use. Here is a summary of some of the optimization experiences I have used in the development of JDBC.
1, the choice of pure Java JDBC driver.
2. Use connection pooling-use a "pool" to manage JDBC connections and carefully debug the parameters of the pool configuration, there are many database connection pools available today.
How to configure the appropriate parameters, the need is testing, not feeling.
3, Reuse connection--maximize the use of each database connection, get the do not easily "discarded."
Sometimes in a process, the database will be manipulated many times, and just need a connection is enough, do not have to use one to get a connection, after the shutdown or into the pool. This will increase the cost of "pool" management, do not think that you use the "pool" can be casually apply for and return the connection, there is a price. If it is a large loop block to manipulate the database, should pay attention to this problem!
4. Reuse statement--for some predefined SQL, set to static constants and reuse the PreparedStatement objects produced by predefined SQL whenever possible. For SQL that uses a pattern multiple times, predefined SQL can be used to gain better performance.
5, the use of batch processing SQL.
6, the optimization result set resultset--query, the returned result set has a different type, priority to select Read-only result sets, not scrollable properties.
This is a very easy place to have problems:
Java.sql.ResultSet
static int CLOSE_CURSORS_AT_COMMIT
该常量指示调用 Connection.commit 方法时应该关闭 ResultSet 对象。
static int CONCUR_READ_ONLY
该常量指示不可以更新的 ResultSet 对象的并发模式。
static int CONCUR_UPDATABLE
该常量指示可以更新的 ResultSet 对象的并发模式。
static int FETCH_FORWARD
该常量指示将按正向(即从第一个到最后一个)处理结果集中的行。
static int FETCH_REVERSE
该常量指示将按反向(即从最后一个到第一个)处理结果集中的行处理。
static int FETCH_UNKNOWN
该常量指示结果集中的行的处理顺序未知。
static int HOLD_CURSORS_OVER_COMMIT
该常量指示调用 Connection.commit 方法时不应关闭 ResultSet 对象。
static int TYPE_FORWARD_ONLY
该常量指示指针只能向前移动的 ResultSet 对象的类型。
static int TYPE_SCROLL_INSENSITIVE
该常量指示可滚动但通常不受其他的更改影响的 ResultSet 对象的类型。
static int TYPE_SCROLL_SENSITIVE
该常量指示可滚动并且通常受其他的更改影响的 ResultSet 对象的类型。
Under Description:
Result sets are of two types: read-only and can be changed, read-only, save memory, and the result set of a query cannot be changed. If the result set changes values and saves after the query, you can change the result set by using the.
There are also two types of cursors for the result set: if it is not necessary to allow the cursor to scroll freely, select the type of cursor to move in single direction.
For concurrent operations: If you do not need to consider thread safety, choose to ignore concurrent result set types, or select the type of concurrency security.
In addition, to control the size of the results, almost all the database has a query record number of control strategy, can be massive data processing batches, a batch, so as not to kill the system.
7, things optimization-if the database does not support things, do not write rollback code, if you do not consider things, do not control the transaction.
8, security optimization-Manage your connection objects, in the abnormal time can "into the pool" or shut down. Therefore, the code that connection releases should be written in the finally block of exception handling.
9, exception handling optimization-do not easily swallowed SqlException, for DAO, service level data access, generally in the DAO ran out of the exception, in the service to handle the exception. But DAO can also handle exception, and do escape throw, do not throw runtimeexeption, because this is the JVM throws, do not need you to throw, because runtimeexception often cause system hangs.
10, Code high-level optimization--on the basis of the above, optimize the encapsulation of your data access, as much as possible to keep the code simple and maintenance, if you still feel that performance is not good, it should be from the whole system to consider the optimization, such as the cache server, clustering, load balancing, optimize the database server, etc., to obtain better system energy.
Source: http://lavasoft.blog.51cto.com/62575/225828