How to avoid JDBC memory overflow issues

Source: Internet
Author: User

Method One: Add two parameters after connecting the MySQL URL: usecursorfetch=true&defaultfetchsize=100

Complete form such as: jdbc:mysql://192.168.1.252:3306/lims?useunicode=true&charsetencoding=utf8&usecursorfetch= true&defaultfetchsize=100

This method is only valid for MySQL.

Method Two: Using the way of paging query, without using the connection pool, the same connection object can be used, and the PreparedStatement object and ResultSet object are closed after each operation. Then, at the next operation, reopen the PreparedStatement object and the ResultSet object. (Note that these two objects must be closed here)

SQL Server and Oracle:

Methods: In the way of paging query, without using connection pool, the same connection object can be used, and the PreparedStatement object and ResultSet object are closed after each operation. Then, at the next operation, reopen the PreparedStatement object and the ResultSet object.

Internal principle and discussion:

When using the Java JDBC Operations database, the default data caching approach is to cache data from database queries into local memory and then read data from memory (which is also why JDBC result sets support moving up and down, However, Oracle-provided JDBC implementations do not support this move up and down by default, so if the data obtained from the database is cached in local memory, the memory overflow can occur if the memory is exceeded.

Like the MySQL method one, using the URL plus the parameters of the way, this is actually using the MySQL cursor, the data will not be cached in the local memory, but the ResultSet object to the record in the database, even if the hundreds of millions of records, as long as the network constantly, there will be no abnormal situation.

Like MySQL method Two, still use the way of data caching, but in this case the amount of cached data is not too good, as long as the PreparedStatement object and ResultSet object closed, the cached data disappears, so there will be no memory overflow.

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.