我按照如何使用 Dao.setAutoCommit() 中的说明进行操作?尝试禁用自动提交,以便我可以批量运行提交。
每当我实际尝试运行代码时,我的程序都会跟踪我在 DAO 上调用了多少次 create 和类似操作,当它大于批量大小时,将调用 dao.commit()... 和然后我会收到此错误(SaveJsonParser 是带有代码的类的名称):
06-08 17:18:50.866: E/AndroidRuntime(30993): java.lang.Runtime
Exception: An error occured while executing doInBackground()
06-08 17:18:50.866: E/AndroidRuntime(30993): at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-08 17:18:50.866: E/AndroidRuntime(30993): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
06-08 17:18:50.866: E/AndroidRuntime(30993): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
06-08 17:18:50.866: E/AndroidRuntime(30993): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
06-08 17:18:50.866: E/AndroidRuntime(30993): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
06-08 17:18:50.866: E/AndroidRuntime(30993): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
06-08 17:18:50.866: E/AndroidRuntime(30993): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
06-08 17:18:50.866: E/AndroidRuntime(30993): at java.lang.Thread.run(Thread.java:1019)
06-08 17:18:50.866: E/AndroidRuntime(30993): Caused by: java.lang.IllegalStateException: no transaction pending
06-08 17:18:50.866: E/AndroidRuntime(30993): at android.database.sqlite.SQLiteDatabase.setTransactionSuccessful(SQLiteDatabase.java:673)
06-08 17:18:50.866: E/AndroidRuntime(30993): at com.j256.ormlite.android.AndroidDatabaseConnection.commit(AndroidDatabaseConnection.java:77)
06-08 17:18:50.866: E/AndroidRuntime(30993): at com.j256.ormlite.dao.BaseDaoImpl.commit(BaseDaoImpl.java:811)
06-08 17:18:50.866: E/AndroidRuntime(30993): at com.visitsanantonio.save.data.SaveJsonParser.commitCategories(SaveJsonParser.java:412)
06-08 17:18:50.866: E/AndroidRuntime(30993): at com.visitsanantonio.save.data.SaveJsonParser.parseDeal(SaveJsonParser.java:278)
06-08 17:18:50.866: E/AndroidRuntime(30993): at com.visitsanantonio.save.data.SaveJsonParser.parseNewUpdatedDeals(SaveJsonParser.java:233)
06-08 17:18:50.866: E/AndroidRuntime(30993): at com.visitsanantonio.save.data.SaveJsonParser$JsonParsingTask.doInBackground(SaveJsonParser.java:479)
06-08 17:18:50.866: E/AndroidRuntime(30993): at com.visitsanantonio.save.data.SaveJsonParser$JsonParsingTask.doInBackground(SaveJsonParser.java:1)
06-08 17:18:50.866: E/AndroidRuntime(30993): at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-08 17:18:50.866: E/AndroidRuntime(30993): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
06-08 17:18:50.866: E/AndroidRuntime(30993): ... 4 more
经过一些实验,看起来我的代码实际上并没有禁用 autoCommit,正如在后台线程中运行的经过清理、截断的示例所示:
conn = dao.startThreadConnection();
Log.e(TAG, "dao auto commit: " + dao.isAutoCommit(conn));
dao.setAutoCommit(conn, false);
Log.e(TAG, "dao auto commit: " + dao.isAutoCommit(conn));
检查日志,我看到两个日志行都显示为“true”。我实际上并没有改变它是否自动提交。我究竟做错了什么?