MariaDBã®JDBCãã©ã¤ãã¯useCursorFetchãã§ããªã
MariaDB 5.5
MariaDB Java Client 1.1.5
ä»å¹´ã®å¤é ããMariaDBã使ã£ã¦ããã
ä»ã®ã¨ããã¯ã©ã¤ã¢ã³ãã¯ä¸»ã«Javaããã°ã©ã ã§ããã
ãã®æ°ã¶æéãMariaDBã«ã¯é©ãããã¦ã°ããã ã
ééãããã¹ã¦ã®ç¶æ³ãè¨äºã«ãããã¨ã¯ç¾ç¶ã§ãã¦ããªãããå°åºãã«ãªã£ã¦ãã¾ãã«ããããã®ãã¡æéãè¦ã¤ãã¦è¨è¼ããã¤ããã§ããã
ä¾ãã°ããããªãã¨ããã£ãã
ã»ãã¼ãã«åãªã©ã®èå¥åã®å¤§æåå°æåããåºå¥ããã
ã»ããããWindowsã¨OS Xã§ã¯èå¥åã®å¤§æåå°æåããåºå¥ããªãã
ã»VARCHARåã®å¤§æåå°æåãããã©ã«ãã§ãåºå¥ããªãã
ã»DATETIMEåã®ããã©ã«ãå¤ã«NOW()ãæå®ã§ããªã
ã»TIMESTAMPåã®æ±ããããããã
ã»è¡ããã¯ã®æåãæ®éãããªã
ãªã©ãªã©ã
ä¸ã«æãããã¹ã¦ã®ãã¨ã¯ãMariaDBã¨ããããããMySQLã®ä»æ§ã§ãããã¯ã»ã§ãããç§ã¯MySQLã«ãMariaDBã«ã詳ããããã§ã¯ãªãããä»ã¾ã§MariaDBã使ãä¸ã§ä¸æè°ã«æãããã¨ã¯ã調ã¹ã¦ã¿ãçµæããã¹ã¦MySQLã¨åããã¨ã ã£ãã®ã ã
ããããæè¿ã«ãªã£ã¦ãJDBCãã©ã¤ãã«é¢ãã¦ã¯ã両è ã«ããªãã®éãããããã¨ãå®æãã¦ãããç¸éã¨ããããããããã¯ããMariaDBã®JDBCãã©ã¤ãã¯ãMySQLã®JDBCãã©ã¤ãããå£ã£ã¦ãããã¨è¨ããããªãã»ã©ã ã
ä½ãèµ·ããã®ãã¨ããã°ãäºã®å§ã¾ãã¯ãç§ãä½æããJavaããã°ã©ã ãOutOfMemoryã§è½ã¡ããã¨ã ã£ãã
è½ã¡ãç®æã¯ãStatement#executeQueryã®å¼ã³åºãå ã§ããã
ããã§OutOfMemoryããã¨ãããã¨ã¯ã»ã»ã»
ãã®ã¨ãæ¢ã«å«ãªäºæããã¦ããã
ä»æ§æ¸ãè¦ããã¨ã«ãã£ã¦ãå«ãªäºæãç¾å®ã§ãããã¨ãããããèªèããã
http://dev.mysql.com/doc/refman/5.5/en/connector-j-reference-implementation-notes.html
http://dev.mysql.com/doc/refman/5.1/ja/connector-j-reference-implementation-notes.html
ï¼ä¸ã®ãªã³ã¯ã¯5.1ã ãæ¥æ¬èªè¨³ï¼
ã»ResultSet
ããã©ã«ãã«ãããResultSets ã¯å®å ¨ã«æåºãããã¡ã¢ãªã«ä¿åããã¾ããã»ã¨ãã©ã®å ´åã«ããã¦ãããã¯æãå¹æçãªæä½æ¹æ³ã§ãããMySQL ã®è¨è¨ã«ããããããã¯ã¼ã¯ ãããã³ã«ã¯ããç°¡åã«å®è£ ã§ãã¾ããå¤å¤§ãªè¡ã大ããªå¤ãæ㤠ResultSets ãæ±ã£ã¦ãã¦ãè¦æ±ãããã¡ã¢ãªã®ããã« JVM ã«ååãªã¹ãã¼ã¹ãå²ãæ¯ããªãå ´åã¯ããã©ã¤ãã«çµæãä¸è¡ãã¨ã«ã¹ããªã¼ã ãæ»ãããæ示ãããã¨ãã§ãã¾ãããã®æ©è½ãæå¹ã«ããã«ã¯ã次ã®æ¹æ³ã§ Statement ã¤ã³ã¹ã¿ã³ã¹ãä½æããå¿ è¦ãããã¾ãã
stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY); stmt.setFetchSize(Integer.MIN_VALUE);ãã§ããã®ãµã¤ãºã Integer.MIN_VALUE ã®ãåé²å°ç¨ãèªã¿åãå°ç¨ã®ã³ã³ããã¼ã·ã§ã³ã¯ãè¡ãã¨ã«çµæã»ãããã¹ããªã¼ã ãããããã©ã¤ãã«æ示ããä¿¡å·ã¨ãã¦æ©è½ãã¾ãããã®å¾ããã®ã¹ãã¼ãã¡ã³ãã§ä½æãããçµæã»ããã¯è¡ãã¨ã«æåºããã¾ãã
ï¼ä»¥ä¸ç¥ï¼
ãªãã¨ãããã¨ã ï¼
確ãã«ãJDBCã®ä»æ§ã§ã¯ãçµæã»ããã®å 容ãã¡ã¢ãªã«å±éãããã®ãããµã¼ãã¼å´ã§ã«ã¼ã½ã«ãä¿æãããã®ãã¾ã§ã¯è¦å®ãã¦ããªããããããã¾ãããããã©ã«ãã§ãã¹ã¦ã®çµæã»ãããã¡ã¢ãªã«å±éãããªãã¦ãã¨ããã¦ããã¨ã¯æã£ã¦ãã¿ãªãã£ããä»æ§æ¸ã«ã¯ãç¶ãã¦ãã¹ããªã¼ã çµæã»ããã使ããã¨ã«ãã£ã¦è§£æ±ºã§ãããããªãã¨ãæ¸ãã¦ããã
çµè«ããè¨ãã¨ããã®ããæ¹ã¯ãã¡ã ã
ã¾ãã
stmt.setFetchSize(Integer.MIN_VALUE);
ããã¯JDBCã®è¦æ ¼ã«éåãã¦ãããJDBCã§ã¯ãsetFetchSizeã«è² ã®å¤ã渡ãããå ´åã¯ãSQLExceptionãthrowããã¨ããã¦ããããããããã¨ããã¦ãã¾ãã¨ããã¼ã¿ãã¼ã¹ã®å®è£ ã«ä¾åããJavaããã°ã©ã ã«ãªã£ã¦ãã¾ãã
ããããç¾å®åé¡ã¨ãã¦ãOutOfMemoryãåé¿ããªããã°ãªããªãã
ç°¡åã«å¯¾å¿ã§ãããªãã°ãããã§ããããã¨ãæããä¸å¿è©¦ãã¦ã¿ããã¨ã«ããã
ãã®ã¹ããªã¼ã çµæã»ããæ©è½ã«ã¯ã注æç¹ãæ¸ãã¦ããã
ãã®ã¢ããã¼ãã«ã¯æ³¨æç¹ãããã¤ãããã¾ããæ¥ç¶ã«ã¯ã¨ãªãçºè¡ããåã«ãçµæã»ããã®ãã¹ã¦ã®è¡ãèªã¾ãªããã°ãªããããããªããã°ä¾å¤ãæå ¥ããã¾ãã
ãã®æ³¨æã®æå³ã¯ã¤ã¾ããStatementã¨ResultSetã®å¯¾ããã¹ãããããã¨ãã§ããªããã¨ããæå³ã§ããã1ã³ãã¯ã·ã§ã³å ã§éããã¹ããªã¼ã ã®æ°ã¯1ã¤ã§ãããã¹ããªã¼ã ãéãããã¦ããªãç¶æ ã§å¥ã®ã¯ã¨ãªãçºè¡ãããã¨ã¯ã§ããªãã
Statement stmt1 = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); stmt1.setFetchSize(Integer.MIN_VALUE); ResultSet rset1 = stmt1.executeQuery("select * from some_table"); while (rset1.next()) { String sql = "select * from other_table where id=1"; Statement stmt2 = conn.createStatement(); ResultSet rset2 = stmt2.executeQuery(sql); // SQLException }
ããã©ããããselect for updateã§è¤æ°è¡ãåå¾ãã¦ãwhileå ã§1è¡ãã¤updateããå¾ã«ãã¾ã¨ãã¦commitããããªãã¦ãã¨ãã§ããªããªãã
ããã¯å¤§å¤å°ã£ãåé¡ã ã
ä¾ãã°ãããããã°ã©ã å ã®è¤æ°ã®ã¯ã©ã¹ãã1ã¤ã®Connectionãå ±æãã¦ãé層çã«å¼ã³åºããã¦ããå¦çã®ãã1ç¹ã«ããã¦ã
ä¸ä½ãä»ã¹ããªã¼ã ãã§ããä¸ãªãã ãã©ãããããå¼ã³åºãã¡ã½ããã¯ãåãã³ãã¯ã·ã§ã³ã§SQLãçºè¡ããã®ã ããããã
ä¸ä½ãã¯ã¦ããã®Connectionã¯ãèªåã®ä¸ä½ã§ã¹ããªã¼ã ãã§ããããã¦ããã ããããã
ãªãã¦ãã¨ããæ°ã«ããã¹ãã§ã¯ãªããããããªãã®ã¯ãªãã¸ã§ã¯ãæåã§ã¯ãªããã¾ãã¦ããã¯ã©ã¹ãã¨ã«Connectionãåé¢ãã¦ãåã ã«åå¾ãããããªãã¦ãã¨ã¯è«å¤ã ããããªãã¨ããããããã©ã³ã¶ã¯ã·ã§ã³ãæå¹ã«ä½¿ããªããªãä¸ãã³ãã¯ã·ã§ã³æ°ã®è¦ç©ããããã§ããªããªãã ããã
ãã£ã¦ãã¤ã¾ããå ¬å¼ããã¥ã¡ã³ãã«æ¸ããã¦ãããã¹ããªã¼ã çµæã»ãããªããã®ã¯ã使ãç©ã«ãªããªãã
ãã£ã¨ã¾ã¨ããªè§£æ±ºæ¹æ³ã¯ãªããã¨æ¢ããã¨ãããuseCursorFetchãªããªãã·ã§ã³ãåå¨ããã
http://dev.mysql.com/doc/refman/5.5/en/connector-j-reference-configuration-properties.html
ãã®æ©è½ã使ãã«ã¯ãJDBCã®æ¥ç¶URLã«ãuseCursorFetch=trueãå«ããã°è¯ãããã ãããã¦ããµã¼ãã¼ããã¯ã©ã¤ã¢ã³ãã®çµæã»ããã«1度ã«æã£ã¦ããããã©ã«ãè¡æ°ï¼defaultFetchSizeï¼ãæå®ã§ããããã®å¤ã¯ãsetFetchSizeã«æ¸¡ãã¹ããæ¬æ¥ã®1以ä¸ã®å¤ãæå®ãããã¨ã«ãã£ã¦ãå®è¡æã«å¤æ´ãããã¨ãå¯è½ã ã
jdbc:mysql://localhost:3306/dbname?useCursorFetch=true&defaultFetchSize=100
å®éã®æåã¨ãã¦ã¯ã
ã»DBãµã¼ãã¼ã¯ãçµæã»ããããå¯è½ãªãã°MEMORYãã¼ãã«ã«å±éããã«ã¼ã½ã«ãä¿æãã
ã»ã¡ã¢ãªã足ããªããã°ãMyISAMãã¼ãã«ã«å±éããã«ã¼ã½ã«ãä¿æãã
ã»ã¯ã©ã¤ã¢ã³ãããã®è¦æ±ã«å¿ãã¦ã1度ã«ãFetchSizeã§æå®ãããåã®è¡æ°ãè¿éãã
ã»ã¯ã©ã¤ã¢ã³ãã¯åå¾ããFetchSizeåã®çµæã»ãããã¡ã¢ãªã«ä¿æãã
ã¨ãªãããã ã
ããã¯å è¿°ã® setFetchSize(Integer.MIN_VALUE) ããã¯ãããªãããã¢ãªè§£æ±ºçã«æããã
ãããã決çã§ããã¨ææ°è¾¼ãã§è©¦ãã¦ã¿ããã®ã®ãã©ãããç§ã®ç°å¢ã§ã¯ãuseCursorFetchãå¹ãã¦ããªãããã«è¦ãããJDBCã®ãªãã·ã§ã³ãè²ã 試ããæãå¥ãMariaDBã®JDBCãã©ã¤ãã®ã½ã¼ã¹ãè½ã¨ãã¦ãã¦è§£èªãã¦ã¿ããçµæãuseCursorFetchãªã©ã¨ãããªãã·ã§ã³ã¯ãMariaDBã®JDBCãã©ã¤ãã§ã¯ãåå¾ããããã¦ããªãã£ãã
ãããããã ã
useCursorFetchãªãã·ã§ã³ã¯ç¢ºãã«ããã®åå¨ãMySQLã®ããã¥ã¡ã³ãã«ã¯è¨è¼ããã¦ããããMariaDBã®JDBCãã©ã¡ã¼ã¿ä¸è¦§ã«ã¯è¨è¼ããã¦ããªãã
https://mariadb.com/kb/en/about-the-mariadb-java-client/
çµæçã«ç§ãæ½ãã対å¦ã¯ã
ã»ä¸åº¦ã®ã¯ã¨ãªã§åå¾ããè¡æ°ãOutOfMemoryã«ãªããªãããã«ãå°ããã¤ãã¼ã¿ãåå¾ããã¨ããããã°ã©ã ç対å¦ã¨ã
ã»ãªã¯ã¨ã¹ãå¦çæã«å¤§éã¬ã³ã¼ãã®æ¤ç´¢ãå¿
è¦ã«ãªããªãããã«ããããããã°ã©ã ã¨ã®é£æºãæ¹åããã¨ããè¨è¨ç対å¦
ã§ãã£ãã
ä¸å¿ãç¾ç¶ã¯ãMariaDBãµã¼ãã¼ã«å¯¾ãã¦ãMySQLã®JDBCãã©ã¤ãã使ããããã ããä»å¾ã©ããªããã¨ãããããã©ã¼ãã³ã¹é¢ã§ããMariaDBã®JDBCãã©ã¤ãã¯ãMySQLã®JDBCãã©ã¤ããããé ãããã ããã®è¾ºãå«ãã¦ãMariaDBã®JDBCãã©ã¤ãã«ã¯ãæ¹åãæå¾ ãããã