{"id":22040,"date":"2019-02-27T09:00:08","date_gmt":"2019-02-27T00:00:08","guid":{"rendered":"https:\/\/www.techscore.com\/blog\/?p=22040"},"modified":"2019-02-27T08:47:01","modified_gmt":"2019-02-26T23:47:01","slug":"jdbc-setfetchsize-%e3%81%a7%e3%81%af%e3%81%be%e3%81%a3%e3%81%9f%e8%a9%b1","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2019\/02\/27\/jdbc-setfetchsize-%e3%81%a7%e3%81%af%e3%81%be%e3%81%a3%e3%81%9f%e8%a9%b1\/","title":{"rendered":"JDBC setFetchSize() \u3067\u306f\u307e\u3063\u305f\u8a71"},"content":{"rendered":"<p>JDBC\u306esetFetchSize\u30e1\u30bd\u30c3\u30c9\u306f\u3054\u5b58\u77e5\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n<p>\u901a\u5e38\u3001\u30af\u30a8\u30ea\u306e\u7d50\u679c\u306fResultSet\u306b\u3059\u3079\u3066\u30ed\u30fc\u30c9\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u305f\u3081\u3001\u5927\u91cf\u306e\u30ec\u30b3\u30fc\u30c9\u3092\u53d6\u5f97\u3059\u308b\u3088\u3046\u306a\u30af\u30a8\u30ea\u3067\u306fOutOfMemoryError\u304c\u767a\u751f\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u3088\u3046\u306a\u5834\u5408\u306b\u6709\u52b9\u306a\u306e\u304csetFetchSize\u3067\u3059\u3002<\/p>\n<p>\u305f\u3068\u3048\u3070\u3001\u30af\u30a8\u30ea\u767a\u884c\u306e\u524d\u306bsetFetchSize(1000)\u3068\u3059\u308b\u3068\u30011000\u4ef6\u305a\u3064ResultSet\u306b\u3068\u308a\u3053\u307e\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u3001OutOfMemoryError\u3092\u56de\u907f\u3059\u308b\u3053\u3068\u304c\u53ef\u80fd\u3067\u3059\u3002<br \/>\n\uff08\u3082\u3061\u308d\u3093\u3001\u642d\u8f09\u30e1\u30e2\u30ea\u91cf\u3001\uff11\u30ec\u30b3\u30fc\u30c9\u3042\u305f\u308a\u306e\u30b5\u30a4\u30ba\u306b\u3088\u308a\u307e\u3059\uff01\uff09<\/p>\n<p>\u3057\u304b\u3057\u3001\u3053\u3053\u306b\u7f60\u304c\u3042\u308a\u307e\u3057\u305f\u3002\u3002\u3002<br \/>\nsetFetchSize\u3092\u4f7f\u7528\u3057\u3066\u3044\u308b\u306b\u3082\u304b\u304b\u308f\u3089\u305a\u3001\u5168\u4ef6\u304cResultSet\u306b\u30ed\u30fc\u30c9\u3055\u308c\u3066\u3057\u307e\u3046\u5834\u5408\u304c\u3042\u308b\u306e\u3067\u3059\u3002<\/p>\n<p>JDBC\u30c9\u30e9\u30a4\u30d0\u3054\u3068\u306bsetFetchSize\u306e\u6319\u52d5\u304c\u304b\u306a\u308a\u9055\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u306f\u307e\u308a\u3069\u3053\u308d\u3067\u3059\u3002<br \/>\n\u4eca\u56de\u306fPostgreSQL\/MySQL\/MariaDB\u306e\u5404\u30c9\u30e9\u30a4\u30d0\u3067\u306e\u6319\u52d5\u306b\u3064\u3044\u3066\u7d39\u4ecb\u3057\u3066\u3044\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u4f3c\u305f\u3088\u3046\u306a\u540d\u524d\u3067 setMaxRows() \u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u3053\u308c\u306f\u30af\u30a8\u30ea\u306e\u7d50\u679c\u4ef6\u6570\u306e\u4e0a\u9650\u3092\u8a2d\u5b9a\u3059\u308b\u3082\u306e\uff08LIMIT\u53e5\u3092\u6307\u5b9a\u3059\u308b\u3088\u3046\u306a\u3082\u306e\uff09\u3067\u3001\u5168\u304f\u9055\u3046\u7528\u9014\u306a\u306e\u3067\u9593\u9055\u308f\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<br \/>\n\u3053\u308c\u306f\u5730\u5473\u306b\u3084\u3063\u3066\u3057\u307e\u3044\u304c\u3061\u3002\u3002\u3002<\/p>\n<h2>PostgreSQL\u306e\u5834\u5408<\/h2>\n<p>\uff08\u4ee5\u4e0b\u306f PostgreSQL\u30c9\u30e9\u30a4\u30d0 \u30d0\u30fc\u30b8\u30e7\u30f3 42.2.5 \u3067\u52d5\u4f5c\u78ba\u8a8d\u3057\u3066\u3044\u307e\u3059\uff09<\/p>\n<p>PostgreSQL\u306e\u5834\u5408\u3001setFetchSize() \u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u6697\u9ed9\u7684\u306b\u30ab\u30fc\u30bd\u30eb\u304c\u4f5c\u6210\u3055\u308c\u3001ResultSet#next()\u6642\u306b\u9069\u5b9c\u3001\u4f5c\u6210\u3057\u305f\u30ab\u30fc\u30bd\u30eb\u3092\u4f7f\u7528\u3057\u3066\u30c7\u30fc\u30bf\u304c\u30ed\u30fc\u30c9\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u305f\u3060\u3057\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u5185\u3067\u306e\u307f\u4e0a\u8a18\u6319\u52d5\u3068\u306a\u308a\u307e\u3059\u3002\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u5916\u306e\u5834\u5408\u3001\u3068\u304f\u306b\u30a8\u30e9\u30fc\u306a\u3069\u306a\u304f\u30af\u30a8\u30ea\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u304c\u3001\u5168\u4ef6\u30ed\u30fc\u30c9\u3055\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u3002<br \/>\n\u4ef6\u6570\u3001\u642d\u8f09\u30e1\u30e2\u30ea\u91cf\u306b\u3088\u3063\u3066OutOfMemoryError\u3068\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u30b3\u30fc\u30c9\u4f8b\uff08Spring JDBC\u3092\u5229\u7528\uff09<\/p>\n<pre class=\"lang:sh decode:true \">    @Autowired\n    private JdbcTemplate jdbcTemplate;\n\n    @Transactional\n    public void execute() throws Exception {\n        AtomicInteger numRow = new AtomicInteger(1);\n\n        jdbcTemplate.setFetchSize(1000);\n\n        jdbcTemplate.query(\"SELECT * FROM foobar\", (resultSet) -&gt; {\n            System.out.println(numRow.getAndIncrement() + \" \u884c\u76ee ID: \" + resultSet.getInt(1));\n        });\n    }\n<\/pre>\n<p>@Transactional\u3092\u5916\u3057\u305f\u5834\u5408\u3001\u5168\u4ef6\u30ed\u30fc\u30c9\u3068\u306a\u308a\u3001OutOfMemoryError\u3068\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u3082\u305d\u3082\u3067\u3059\u304c\u3001PostgreSQL\u306f\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u5916\u3067\u306f\u30ab\u30fc\u30bd\u30eb\u3092\u5b9a\u7fa9\u3067\u304d\u307e\u305b\u3093\u3002<\/p>\n<pre>\nfoobar=# DECLARE cursor_foo CURSOR FOR SELECT * FROM foobar;\nERROR:  DECLARE CURSOR can only be used in transaction blocks\n<\/pre>\n<h2>MySQL Connector\/J \u306e\u5834\u5408<\/h2>\n<p>\uff08\u4ee5\u4e0b\u306f mysql-connector\u30d0\u30fc\u30b8\u30e7\u30f3 8.0.15 \u3067\u52d5\u4f5c\u78ba\u8a8d\u3057\u3066\u3044\u307e\u3059\uff09<\/p>\n<p>PostgreSQL\u306e\u30c9\u30e9\u30a4\u30d0\u3067\u306f setFetchSize() \u306f\u30ab\u30fc\u30bd\u30eb\u3067\u5b9f\u88c5\u3055\u308c\u3066\u3044\u307e\u3057\u305f\u304c\u3001MySQL\u306e\u30c9\u30e9\u30a4\u30d0\u3067\u306f \u30ab\u30fc\u30bd\u30eb or \u30b9\u30c8\u30ea\u30fc\u30df\u30f3\u30b0\u306e\u3069\u3061\u3089\u304b\u3092\u9078\u629e\u3057\u3066\u5229\u7528\u53ef\u80fd\u3067\u3059\u3002<\/p>\n<h3>\u30ab\u30fc\u30bd\u30eb\u30e2\u30fc\u30c9<\/h3>\n<p>JDBC\u306e\u63a5\u7d9a\u30d1\u30e9\u30e1\u30fc\u30bf\u306b useCursorFetch=true \u3092\u8a2d\u5b9a\u3059\u308b\u3053\u3068\u3067\u5229\u7528\u53ef\u80fd\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>jdbc:mysql:\/\/127.0.0.1:3306\/foobar?useCursorFetch=true\n<\/pre>\n<p>setFetchSize() \u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067PostgreSQL\u3068\u540c\u69d8\u306b\u6697\u9ed9\u7684\u306b\u30ab\u30fc\u30bd\u30eb\u3092\u5229\u7528\u3057\u3066\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u307e\u3059\u304c\u3001PostgreSQL\u3068\u306f\u9055\u3044\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u5185\u5916\u3092\u554f\u308f\u305a\u30ab\u30fc\u30bd\u30eb\u304c\u6709\u52b9\u306b\u52d5\u4f5c\u3057\u307e\u3059\u3002\u3044\u3044\u3067\u3059\u306d\uff01<\/p>\n<p>useCursorFetch=true \u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u306a\u3044\u5834\u5408\u3001\u3068\u304f\u306b\u30a8\u30e9\u30fc\u306a\u3069\u306a\u304f\u5168\u4ef6\u304cResultSet\u306b\u30ed\u30fc\u30c9\u3055\u308c\u3066\u3057\u307e\u3044\u307e\u3059\u306e\u3067\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h3>\u30b9\u30c8\u30ea\u30fc\u30df\u30f3\u30b0\u30e2\u30fc\u30c9<\/h3>\n<p>\u30b9\u30c8\u30ea\u30fc\u30df\u30f3\u30b0\u30e2\u30fc\u30c9\u3068\u306f\u3001\u30ab\u30fc\u30bd\u30eb\u306e\u3088\u3046\u306aDBMS\u306e\u6a5f\u80fd\u3092\u4f7f\u7528\u305b\u305a\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b5\u30fc\u30d0\u304b\u3089\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u4e00\u5ea6\u306b\u53d7\u4fe1\u3057\u304d\u3089\u305a\u3001\u3086\u308b\u3084\u304b\u306b\u53d7\u4fe1\u3057\u3066\u3044\u304f\u3053\u3068\u3067 setFetchSize() \u3092\u5b9f\u73fe\u3059\u308b\u3082\u306e\u3067\u3059\u3002<\/p>\n<p>\u30b9\u30c8\u30ea\u30fc\u30df\u30f3\u30b0\u30e2\u30fc\u30c9\u306fJDBC\u63a5\u7d9a\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u8a2d\u5b9a\u306f\u4e0d\u8981\u3067\u3001setFetchSize(Integer.MIN_VALUE) \u3068\u3059\u308b\u3053\u3068\u3067\u5229\u7528\u53ef\u80fd\u3067\u3059\u3002<br \/>\n\uff08JDBC\u63a5\u7d9a\u30d1\u30e9\u30e1\u30fc\u30bf\u306b useCursorFetch=true \u3092\u8a2d\u5b9a\u3057\u3066\u3044\u308b\u5834\u5408\u3067\u3082\u3053\u3061\u3089\u304c\u512a\u5148\u3055\u308c\u3066\u30b9\u30c8\u30ea\u30fc\u30df\u30f3\u30b0\u30e2\u30fc\u30c9\u3068\u306a\u308a\u307e\u3059\u3002\uff09<\/p>\n<p>\u30b3\u30fc\u30c9\u4f8b<\/p>\n<pre class=\"lang:sh decode:true \">    @Autowired\n    private JdbcTemplate jdbcTemplate;\n\n    @Transactional\n    public void execute() throws Exception {\n        AtomicInteger numRow = new AtomicInteger(1);\n\n        jdbcTemplate.setFetchSize(Integer.MIN_VALUE);\n\n        jdbcTemplate.query(\"SELECT * FROM foobar\", (resultSet) -&gt; {\n            System.out.println(numRow.getAndIncrement() + \" \u884c\u76ee ID: \" + resultSet.getInt(1));\n        });\n    }\n<\/pre>\n<p>\u3053\u3061\u3089\u3082\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u5185\u5916\u554f\u308f\u305a\u6709\u52b9\u306b\u52d5\u4f5c\u3057\u307e\u3059\u304c\u3001\u30ab\u30fc\u30bd\u30eb\u3068\u9055\u3063\u3066\u3044\u304f\u3064\u304b\u6ce8\u610f\u70b9\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h4>\uff11\uff0e\u4e26\u884c\u3057\u3066\u30af\u30a8\u30ea\u3092\u5b9f\u884c\u3067\u304d\u306a\u3044\u3002<\/h4>\n<p>\u30af\u30a8\u30ea\u306e\u5b9f\u884c\u7d50\u679c\u3092\u53d7\u4fe1\u4e2d\u3068\u3044\u3046\u30b9\u30c6\u30fc\u30bf\u30b9\u306a\u306e\u3067\u3001\u5225\u306e\u30af\u30a8\u30ea\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002<br \/>\n\u65b0\u305f\u306b\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3092\u958b\u3044\u305f\u3046\u3048\u3067\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3067\u306f \"SELECT 1\" \u306e\u767a\u884c\u6642\u306b\u4f8b\u5916\u304c\u767a\u751f\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true \">    @Autowired\n    private JdbcTemplate jdbcTemplate;\n\n    @Transactional\n    public void execute() throws Exception {\n        jdbcTemplate.setFetchSize(Integer.MIN_VALUE);\n\n        jdbcTemplate.query(\"SELECT * FROM foobar\", (resultSet) -&gt; {\n            jdbcTemplate.execute(\"SELECT 1\");\n            System.out.println(numRow.getAndIncrement() + \" \u884c\u76ee ID: \" + resultSet.getInt(1));\n        });\n    }\n<\/pre>\n<pre class=\"lang:sh decode:true \">Caused by: java.sql.SQLException: Streaming result set com.mysql.cj.protocol.a.result.ResultsetRowsStreaming@157632c9 is still active. No statements may be issued when any streaming result sets are open and in use on a given connection. Ensure that you have called .close() on any active streaming result sets before attempting more queries.\n        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.15.jar!\/:8.0.15]\n<\/pre>\n<h4>\uff12\uff0e\u30b5\u30fc\u30d0\u30bf\u30a4\u30e0\u30a2\u30a6\u30c8\u304c\u5b58\u5728\u3059\u308b<\/h4>\n<p>MySQL\/MariaDB\u306b\u306f net_write_timeout \u3068\u3044\u3046\u8a2d\u5b9a\u5024\u304c\u3042\u308a\u307e\u3059\u3002\u30b5\u30fc\u30d0\u304c\u6700\u5f8c\u306b\u30ec\u30b9\u30dd\u30f3\u30b9\u3092\u66f8\u304d\u8fbc\u3093\u3067\u304b\u3089 net_write_timeout \u79d2\u304c\u7d4c\u904e\u3059\u308b\u3068\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u304c\u5207\u3089\u308c\u307e\u3059\u3002<br \/>\n\u3067\u3059\u306e\u3067\u3001ResultSet\u304b\u3089\u306e\u8aad\u307f\u8fbc\u307f\u4e2d\u306b\u6642\u9593\u306e\u304b\u304b\u308b\u51e6\u7406\u304c\u3042\u308b\u5834\u5408\u306b\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<br \/>\n\u305f\u3060\u3057\u3001setFetchSize(Integer.MIN_VALUE) \u3068\u3057\u305f\u5834\u5408\u3001net_write_timeout\u5024\u304c 600 (10\u5206) \u306b\u6697\u9ed9\u7684\u306b\u5909\u66f4\u3055\u308c\u308b\u3088\u3046\u306a\u306e\u3067\u3001\u901a\u5e38\u306f\u554f\u984c\u306a\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u30c6\u30b9\u30c8\u3068\u3057\u3066\uff11\u4e07\u884c\u8aad\u307f\u8fbc\u3093\u3060\u3068\u3053\u308d\u3067601\u79d2\u9593\u30b9\u30ea\u30fc\u30d7\u3057\u3066\u307f\u308b\u3068\u4f8b\u5916\u304c\u767a\u751f\u3057\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:sh decode:true \">    @Autowired\n    private JdbcTemplate jdbcTemplate;\n\n    public void execute() throws Exception {\n        AtomicInteger numRow = new AtomicInteger(1);\n\n        jdbcTemplate.setFetchSize(Integer.MIN_VALUE);\n\n        jdbcTemplate.query(\"SELECT * FROM foobar\", (resultSet) -&gt; {\n            if (numRow.get() == 10000) {\n                try {\n                    Thread.sleep(601000);\n                } catch (InterruptedException e) {\n                    e.printStackTrace();\n                }\n            }\n            System.out.println(numRow.getAndIncrement() + \" \u884c\u76ee ID: \" + resultSet.getInt(1));\n        });\n    }\n<\/pre>\n<pre class=\"lang:sh decode:true \">java.net.SocketException: Connection reset\n        at java.net.SocketInputStream.read(SocketInputStream.java:189)\n<\/pre>\n<h2>MariaDB Connector\/J \u306e\u5834\u5408<\/h2>\n<p>\uff08\u4ee5\u4e0b\u306f mariadb-java-client\u30d0\u30fc\u30b8\u30e7\u30f3 2.4.0 \u3067\u52d5\u4f5c\u78ba\u8a8d\u3057\u3066\u3044\u307e\u3059\uff09<\/p>\n<p>MySQL\u306e\u30c9\u30e9\u30a4\u30d0\u3068\u7570\u306a\u308a\u3001useCursorFetch\u30d1\u30e9\u30e1\u30fc\u30bf\u306f\u5b58\u5728\u305b\u305a\u3001\u30b9\u30c8\u30ea\u30fc\u30df\u30f3\u30b0\u30e2\u30fc\u30c9\u306e\u307f\u5229\u7528\u53ef\u80fd\u3067\u3059\u3002<br \/>\n\u30b9\u30c8\u30ea\u30fc\u30df\u30f3\u30b0\u30e2\u30fc\u30c9\u3092\u5229\u7528\u3059\u308b\u306b\u306f\u3001setFetchSize()\u306b\u4efb\u610f\u306e\u5024\u3092\u6307\u5b9a\u3059\u308b\u3060\u3051\u3067\u69cb\u3044\u307e\u305b\u3093\u3002<br \/>\n\uff08setFetchSize(Integer.MIN_VALUE)\u3068\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u306f\u3042\u308a\u307e\u305b\u3093\uff09<\/p>\n<p>\u30b3\u30fc\u30c9\u4f8b<\/p>\n<pre class=\"lang:sh decode:true \">    @Autowired\n    private JdbcTemplate jdbcTemplate;\n\n    @Transactional\n    public void execute() throws Exception {\n        AtomicInteger numRow = new AtomicInteger(1);\n\n        jdbcTemplate.setFetchSize(1000);\n\n        jdbcTemplate.query(\"SELECT * FROM foobar\", (resultSet) -&gt; {\n            System.out.println(numRow.getAndIncrement() + \" \u884c\u76ee ID: \" + resultSet.getInt(1));\n        });\n    }\n<\/pre>\n<p>\u6ce8\u610f\u70b9\u3068\u3057\u3066\u3001MySQL\u306e\u30c9\u30e9\u30a4\u30d0\u3068\u30b9\u30c8\u30ea\u30fc\u30df\u30f3\u30b0\u30e2\u30fc\u30c9\u306e\u6319\u52d5\u304c\u7570\u306a\u308a\u307e\u3059\u3002<\/p>\n<h4>\uff11\uff0e\u4e26\u5217\u3067\u30af\u30a8\u30ea\u3092\u5b9f\u884c\u3067\u304d\u308b\u3002\u3002\u3002\u304c\u3057\u304b\u3057\uff01<\/h4>\n<p>\u4e26\u5217\u3067\u30af\u30a8\u30ea\u3092\u767a\u884c\u3057\u305f\u5834\u5408\u3001\u30af\u30a8\u30ea\u767a\u884c\u524d\u306b\u5148\u884c\u306e\u30af\u30a8\u30ea\u3092\u5b8c\u4e86\u3055\u305b\u308b\uff08\u5373\u5ea7\u306b\u5168\u4ef6\u30ed\u30fc\u30c9\u3057\u7d42\u3048\u308b\uff09\u305f\u3081\u3001\u4e26\u5217\u3067\u30af\u30a8\u30ea\u3092\u5b9f\u884c\u53ef\u80fd\u3067\u3059\u3002<br \/>\n\u304c\u3001setFetchSize()\u306e\u610f\u5473\u304c\u306a\u3044\u3067\u3059\u306d\u3002<\/p>\n<p>\u4e0b\u8a18\u306e\u4f8b\u3067\u306f \"SELECT 1\" \u3092\u767a\u884c\u6642\u306b \"SELECT * FROM foobar\" \u306e\u7d50\u679c\u304c\u5168\u4ef6\u30ed\u30fc\u30c9\u3055\u308c\u3001OutOfMemoryError\u3068\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:sh decode:true \">    @Autowired\n    private JdbcTemplate jdbcTemplate;\n\n    @Transactional\n    public void execute() throws Exception {\n        AtomicInteger numRow = new AtomicInteger(1);\n\n        jdbcTemplate.setFetchSize(1000);\n\n        jdbcTemplate.query(\"SELECT * FROM foobar\", (resultSet) -&gt; {\n            jdbcTemplate.execute(\"SELECT 1\");\n            System.out.println(numRow.getAndIncrement() + \" \u884c\u76ee ID: \" + resultSet.getInt(1));\n        });\n    }\n<\/pre>\n<h4>\uff12\uff0enet_write_timeout\u8a2d\u5b9a\u5024\u306f\u5909\u66f4\u3055\u308c\u306a\u3044<\/h4>\n<p>MySQL\u306e\u30c9\u30e9\u30a4\u30d0\u3067\u306f\u30af\u30a8\u30ea\u5b9f\u884c\u524d\u306b 600 \u306b\u5909\u66f4\u3055\u308c\u307e\u3057\u305f\u304c\u3001MariaDB\u306e\u30c9\u30e9\u30a4\u30d0\u3067\u306f\u5909\u66f4\u3055\u308c\u305a\u3001<br \/>\n\u30b5\u30fc\u30d0\u306e\u8a2d\u5b9a\u306b\u5f93\u3044\u307e\u3059\u3002\u30b5\u30fc\u30d0\u306e\u8a2d\u5b9a\u306b\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u30b5\u30fc\u30d0\u306e\u30b0\u30ed\u30fc\u30d0\u30eb\u8a2d\u5b9a\u304c10\u79d2\u306e\u5834\u5408\u300110\u79d2\u3067\u63a5\u7d9a\u3092\u5207\u3089\u308c\u307e\u3059\u3002<br \/>\n\u30c6\u30b9\u30c8\u3068\u3057\u3066\uff11\u4e07\u884c\u8aad\u307f\u8fbc\u3093\u3060\u3068\u3053\u308d\u306711\u79d2\u9593\u30b9\u30ea\u30fc\u30d7\u3057\u3066\u307f\u308b\u3068\u4f8b\u5916\u304c\u767a\u751f\u3057\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:sh decode:true \">MariaDB [test]&gt; show global variables like 'net_write_timeout';\n+-------------------+-------+\n| Variable_name     | Value |\n+-------------------+-------+\n| net_write_timeout | 10    |\n+-------------------+-------+\n1 row in set (0.001 sec)\n<\/pre>\n<pre class=\"lang:sh decode:true \">    @Autowired\n    private JdbcTemplate jdbcTemplate;\n\n    public void execute() throws Exception {\n        AtomicInteger numRow = new AtomicInteger(1);\n\n        jdbcTemplate.setFetchSize(1000);\n\n        jdbcTemplate.query(\"SELECT * FROM foobar\", (resultSet) -&gt; {\n                if(numRow.get() == 10000) {\n                    try {\n                        Thread.sleep(11000);\n                    } catch (InterruptedException e) {\n                        e.printStackTrace();\n                    }\n                }\n                System.out.println(numRow.getAndIncrement() + \" \u884c\u76ee ID: \" + resultSet.getInt(1));\n        });\n    }\n<\/pre>\n<pre>java.sql.SQLNonTransientConnectionException: (conn=104) Server has closed the connection. If result set contain huge amount of data, Server expects client to read off the result set relatively fast. In this case, please consider increasing net_wait_timeout session variable \/ processing your result set faster (check Streaming result sets documentation for more information)\n<\/pre>\n<h2>\u5b9f\u88c5\u306e\u5dee\u304c\u306f\u3052\u3057\u3044\uff01<\/h2>\n<p>\u3044\u304b\u304c\u3060\u3063\u305f\u3067\u3057\u3087\u3046\u304b\uff1f\u5b9f\u969b\u306b\u5404\u30c9\u30e9\u30a4\u30d0\u306e\u6319\u52d5\u3092\u307f\u305f\u7d50\u679c\u3001\u3053\u3053\u307e\u3067\u5dee\u304c\u3042\u308b\u3068\u306f\u8abf\u67fb\u524d\u306b\u306f\u601d\u3063\u3066\u3082\u3044\u307e\u305b\u3093\u3067\u3057\u305f\u3002<br \/>\n\u77e5\u3089\u305a\u306b\u4f7f\u3063\u3066\u3044\u308b\u3068\u3053\u308f\u3044\u3067\u3059\u306d\u3002\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JDBC\u306esetFetchSize\u30e1\u30bd\u30c3\u30c9\u306f\u3054\u5b58\u77e5\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n<p>\u901a\u5e38\u3001\u30af\u30a8\u30ea\u306e\u7d50\u679c\u306fResultSet\u306b\u3059\u3079\u3066\u30ed\u30fc\u30c9\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u305f\u3081\u3001\u5927\u91cf\u306e\u30ec\u30b3\u30fc\u30c9\u3092\u53d6\u5f97\u3059\u308b\u3088\u3046\u306a\u30af\u30a8\u30ea\u3067\u306fOutOfMemoryError\u304c\u767a\u751f\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2019\/02\/27\/jdbc-setfetchsize-%e3%81%a7%e3%81%af%e3%81%be%e3%81%a3%e3%81%9f%e8%a9%b1\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[261,62,18],"tags":[57,323,63,206],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/22040"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=22040"}],"version-history":[{"count":20,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/22040\/revisions"}],"predecessor-version":[{"id":22061,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/22040\/revisions\/22061"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=22040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=22040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=22040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}