JDBC Performance Small Sticker

Source: Internet
Author: User
Tags connection pooling

This article collects a number of ways to improve JDBC performance. The performance of Java applications or EE Web applications is important, especially for the performance impact of database backend applications. I don't know if you've ever experienced Java, EE Web application very slow case not (processing a simple request takes several seconds for database access, paging, sorting, etc.). The following tips may improve the performance of Java applications. They are very simple and can also be applied to other programming languages if the database is used as the backend storage.


These JDBC performance stickers are not necessarily cool or some of you have never heard of, although very basic but in practice many programmers often ignore them, of course, you may refer to the title Database performance hints.


JDBC Performance tip one: Using caching


Find out how many database calls you have in your application, and then minimize them, whether you believe in performance or not. Most of the times the culprit is the code that accesses the database. Because connecting to a database requires a ready connection (connections), a roundtrip network transport, and processing of the backend of the database system. If you can cache data down this is the best way to reduce database calls, even if your application has completely dynamic data, a short cache can save a lot of data hard round-trip transmission. Accelerating Java applications can at least reduce 20-50% database calls. If you want to find a database call, then simply log each DB call in the DAO layer to the logs, and it will tell you how long it takes to log each thread into and out of the database for a better time.


JDBC Performance tip Two: Using Database indexes


Check if the database column (columns) has an index, and if you're making a query that takes longer than you expected, the first thing to do is to check whether the column in the WHERE clause you are querying is indexed. Programmers often make this mistake, there is a huge difference between indexing and no indexing in making queries. This tip can speed up to at least 100% of the performance, of course, the appropriate index is more important, too many indexes will slow down the insertion of data and update operations. So use the index carefully, like ID, category (category), Class (Class) and other fields on the index is often used.


JDBC Performance tip three: Using PreparedStatement


PreparedStatement (preprocessed statements) are faster than normal statement objects when executing queries using PreparedStatement or stored procedures (Stored Procedure). Because the database can be used to preprocess query statements to query cache plan. Therefore always use the preprocessing statements in parameterized form * * such as SELECT * from table where id=, instead of using the SELECT * from table where id= ' +id ', although the latter is still a preprocessing statement but not parameterized. There is no performance advantage with the second query, and more references can be found to see why JDBC uses preparedstatement instead of statement.


Http://www.importnew.com/5006.html


JDBC Performance tip Four: Using a database connection pool


Connection pooling is used to hold database connections (Connection), and creating a database connection is a slow process and can take a long time. So if you want to create a connection for each request, then obviously the response time will be longer. Use connection pooling to create an appropriate number of connections based on the upstream traffic and the number of concurrent requests. Even if the connection pool creates a connection in the initial request and the cache connection is slow, the overall cost can be reduced.


JDBC Performance tip five: use JDBC batch update


Using the JDBC Bulk Update operation can significantly improve the performance of Java database applications. You should always use bulk processing to perform insert and update operations. Do bulk queries by using statment or PreparedStatement. Use ExecuteBatch () method to do batch query.


JDBC Performance tip six: Canceling autocommit


Set Setautocommit (FALSE) at query time, the default JDBC Connection autocommit mode is open, meaning that each standalone SQL statement will be executed in its own transaction. However, you can group SQL statements into a single logical transaction so that either commit () or rollback () is committed or rolled back. Try running the same number of queries comparing the performance differences between using autocommit and not using autocommit.


These Java database application tips look very simple, many advanced Java programmers are proficient in production-level code, but I also see that many Java programmers do not care about this until they find that Java applications become very slow. So it is necessary for the novice to remember that it uses it. You can also use these Java performance tips as a reference to the code review mechanism when you write Java applications that use databases as backend storage.

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.