Home Database Mysql Tutorial MySQL的JDBC OutOfMemoryError: Java heap space异常

MySQL的JDBC OutOfMemoryError: Java heap space异常

Jun 07, 2016 pm 03:52 PM
jdbc mysql outofmemoryerror

MySQL的JDBC OutOfMemoryError: Java heap space异常 http://hi.baidu.com/tlw_ray/blog/item/ab925bf4ff3312d1f3d385e2.html MySql数据库通过JDBC对大表进行查询时抛出java.lang.OutOfMemoryError: Java heap space异常。这是因为默认情况下,MySQL的JDBC驱

MySQL的JDBC OutOfMemoryError: Java heap space异常

http://hi.baidu.com/tlw_ray/blog/item/ab925bf4ff3312d1f3d385e2.html

        MySql数据库通过JDBC对大表进行查询时抛出java.lang.OutOfMemoryError: Java heap space异常。这是因为默认情况下,MySQL的JDBC驱动会一下子把所有row都读取下来,这在一般情况下是最优的,因为可以减少Client-Server的通信开销。但是这样也有一个问题,当数据库查询结果很大时,特别当不能全部放进内存时,就会产生性能问题。

本来,JDBC api里在Connection、Statement和ResultSet上都有设置fetchSize的方法,但是MySQL的JDBC驱动都不支持,无论你怎么设fetchSize,ResultSet都会一次性从Server读取数据。在MySQL的官方论坛上也有多个这样的问题,总结一下解决办法如下:

1.MySQL版本在5.0以上,MySQL的JDBC驱动更新到最新版本(至少5.0以上)
2.Statement一定是TYPE_FORWARD_ONLY的,并发级别是CONCUR_READ_ONLY(即创建Statement的默认参数)
3.以下两句语句选一即可:
    1).statement.setFetchSize(Integer.MIN_VALUE);
    2).((com.mysql.jdbc.Statement)stat).enableStreamingResults();

这样会一行一行地从Server读取数据,因此通信开销很大,但内存问题可以解决。官方的说法是不支持fetchSize不是MySQL的JDBC驱动的问题,而是MySQL本身就不支持。而商用数据库Oracle或DB2都是支持fetchSize的,从这里也可以看出两者的考量不同。

考虑到应用程序利用JDBC跨数据库的特性可以,通过判断mySql连接特殊处理:

                if(conn.getClass().getName().indexOf("mysql")>0){

                 //com.mysql.jdbc.Driver
                  stmt.setFetchSize(Integer.MIN_VALUE); 
                }

开始以为是自己的代码有问题,找了半天没找到。后来仔细看了异常才发现:

严重: Servlet.service() for servlet ExportServlet threw exception
java.lang.OutOfMemoryError: Java heap space

at com.mysql.jdbc.Buffer.getBytes(Buffer.java:198)

at com.mysql.jdbc.Buffer.readLenByteArray(Buffer.java:318)
at com.mysql.jdbc.MysqlIO.nextRow(MysqlIO.java:1366)
at com.mysql.jdbc.MysqlIO.readSingleRowSet(MysqlIO.java:2333)
at com.mysql.jdbc.MysqlIO.getResultSet(MysqlIO.java:435)
at com.mysql.jdbc.MysqlIO.readResultsForQueryOrUpdate(MysqlIO.java:2040)
at com.mysql.jdbc.MysqlIO.readAllResults(MysqlIO.java:1443)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1777)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3249)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1403)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
at com.cdnlog.servlet.ExportServlet.doPost(ExportServlet.java:77)

ExportServlet.java:77,代码这里是rs=pstmt.executeQuery();

这句话报错。然后求助网友鬼精灵,找到如上那篇文章。解决了~

谢谢鬼精灵~,哈哈,也许你看不到


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.

See all articles