SQLiteã使ãå ´åã®æ³¨æç¹
ãã¦ãé·ããã¨æ¾ç½®ãã¦ããã¯ã¦ãªãã¤ã¢ãªã¼ã®æ¹ã§ãããã¾ã¨ãæ¸ãããæ¹ããããã®ã¯ããã£ã±ããã¡ãã«æ¸ãã¨ãããã¨ã§ã
Androidã§SQLiteã使ãã±ã¼ã¹ã¯å¤ã ããã¨æãã¾ãããæè¨ããã¦ããªã注æç¹ãããã®ã§å¿åé²ãã¦ãã
SQLiteDatabase#closeã¯æ示ã§å¼ã¶ãªãCursor#closeã¯æ示ã§å¼ã¹
ããã¯SQLiteã®ä½ãã®è©±ã§ãããSQLiteã§ã¯ãã«ãã¹ã¬ããã«å¯¾ãã¦ã³ãã¯ã·ã§ã³ãªã¼ãã³ããã¯ãã¼ãºã¾ã§ã¯ä¿éãããã¨ããä½ãã«ãªã£ã¦ãã¾ãã
è¦ã¯ã³ãã¯ã·ã§ã³åä½ã§ã¹ã¬ããã»ã¼ãã§ãããã¨ãããã¨ã
Androidã§SQLiteã使ã£ã¦æ¤ç´¢ç³»ã®å¦çãããã®ã«ãããããWebã¢ããªçãªä½ãã§èããã¨ãæ´æ°ç³»å¦çã§ã¯CUDå¦çã®ãã¨ã«SQLiteDatabase#closeã¨ããã¡ã§ãããandroid.database.sqlite.SQLiteException ãçºçããã±ã¼ã¹ãããã¾ãã
ã©ããã£ãã±ã¼ã¹ãã¨ããã¨ã
- æ¤ç´¢ç³»ã§SQLiteDatabase#queryã§Cursoråå¾
- 並è¡ãã¦æ´æ°ç³»ã§SQLiteDatabase#close
ã¨ããã±ã¼ã¹ã
Cursorãåå¾ãã¦ããã¨ãã«ãã¡ããã©æ´æ°ç³»ã§closeããã¦ããã¨ãSQLITE_MISUSE (21) ã¨ããã¨ã©ã¼ã³ã¼ãâSQLiteExceptionãè¿ãã¾ãã
ã±ã£ã¨è¦ãNullPointerExceptionãããï¼ãªæãããããã¨æãã¾ããããã¡ãã¨ãã¤ãã£ã層ã¾ã§ã³ã¼ã«ããã¾ãã
ãªãã§ãã¨ããã¨ãSQLiteDatabase#closeãå¼ã¶ã¨ã
- Javaå´ã®ãªãã¸ã§ã¯ãåç §ã解æ¾
- JNIçµç±ã§ãã¤ãã£ãã³ã¼ã«ããdbcloseå¦ç
- SQLiteDatabase#mNativeHandleã¨ããã¡ã³ãå¤æ°ãJNIã³ã¼ãã§0åæå
ãã¾ããæåéããdbcloseå¦çãè¡ãããã§ãã
ããã«ãããSQLiteDatabase#closeãå¼ãã ãªãã¸ã§ã¯ãã¯closeå¦çããã¾ããããããããããã¯ããã¾ã§ã¡ã³ãå¤æ°ã§ãã
ãããããã®ãªãã¸ã§ã¯ãèªä½ã¯ç ´æ£ããã¾ããã
ããã¨ä¸¦è¡ãã¦Cursorãåã£ã¦ããã¨ãåé¡ãçºçãCursorãã¡ã³ãå¤æ°ã§SQLiteDatabaseãæã¡ã¾ãã
æ¬æ¥ãSQLiteHelper#getReadableDatabase, getWritableDatabaseã¯SQLiteDatabase#isOpenãtrueã ã¨ãªã«ããã¾ããããfalseã ã¨æ°ããåå¾ãã¾ãã
ã¤ã¾ãããã¤ãã£ãã³ã¼ã«ãããããã§ããã®ã¨ããmNativeHandleã¯dbopené¢æ°ä¸ã§æ°ãããã¤ã³ã¿ã¢ãã¬ã¹ã§æ´æ°ããã¾ãã
ãããªãã¨ãSQLiteDatabase#isOpenã§ã¯trueãè¿ãã®ã«ããã¤ã³ã¿ã¢ãã¬ã¹ãç°ãªãâSQLITE_MISUSEè¿ãâSQLiteExceptionã¨ãªããããã»ã¹ãçµäºããªãéãä¾å¤ãåºç¶ãããã¨ããç¶æ³ã«ãªãã¾ãã
ã¿ã¤ãã³ã°ã§çºçããããã«ãªãããã§ããã1度çºçããã¨ãããããªãã¾ãã
解決ã¯SQLiteDatabase#closeãå¼ã°ãªããã¨ãSQLiteOpenHelper#closeãçµå±ãSQLiteDatabase#closeã«ããã®ã§åãã§ãã
ããããcloseå¦çã¯ã©ã¼ããã®ï¼ã£ã¦æ°ã«ãªãã¾ãããããã¯SQLiteDatabase#finalizeããã£ã¦ããã¾ãã
ã¤ã¾ãã¨ãããæçµçã«ã¯DalvikVMãç ´æ£æã«ããã®ã§æ°ã«ãããªãã¨ãããã¨ã«ãªãã¾ãã
ãã ããCursor#closeã¯å¼ãã§ããå¿
è¦ãããã¾ããå¼ãã§ãªãå ´åãç ´æ£æã«ERRORãã°ãã§ã¾ãã
Readå°ç¨ã§Cursorã使ãéãã¯ã¾ã ããã®ã§ããããªã½ã¼ã¹ãæ±ãã®ã§ãã¡ãã¨éãã¾ãããã