我使用Singleton Approach创建了DBhandler,其中数据库的1个对象一次可用于任何事务。
在从Asynctask的doInBackground方法中获取数据库中的数据时,我得到了连接池问题SOMETIMES。 在这里,我附上了我的代码片段。请澄清我在哪里做错了:
DBHandler中的方法
public List<MIC_OrderDetails> getdata(String Loc) {
Cursor cursor = null;
List<MIC_OrderDetails> ordlst = new ArrayList<MIC_OrderDetails>();
SQLiteDatabase db = getReadableDatabase();
String q = "SELECT * FROM " + DatabaseHandler.TABLE_MIC2
+ " WHERE Location ='" + Loc + "'";
cursor = db.rawQuery(q, null);
try {
while (cursor.moveToNext()) {
MIC_OrderDetails mic_OrderDetails = new MIC_OrderDetails();
mic_OrderDetails.setItemNumber(cursor.getString(2));
mic_OrderDetails.setItemDescription(cursor.getString(3));
mic_OrderDetails.setPickSeq(cursor.getString(4));
mic_OrderDetails.setQtyonHand(cursor.getDouble(5)+"");
mic_OrderDetails.setQtyCount(cursor.getDouble(6)+"");
mic_OrderDetails.setUnit(cursor.getString(7));
ordlst.add(mic_OrderDetails);
}
} catch (Exception e) {
e.printStackTrace();
errCode = "Error 5210";
msg = "MIC_OrderDetails failed.";
errMsg = errCode + " : " + msg;
LogfileCreator.appendLog(errMsg);
db.close();
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
return ordlst;
}
从Databse获取数据:
@Override
protected List<MIC_OrderDetails> doInBackground(String... params) {
// TODO Auto-generated method stub
String result = "";
List<MIC_OrderDetails> lst = new ArrayList<MIC_OrderDetails>();
try {
dbhelper.getReadableDatabase();
lst = dbhelper.getdata(spinLoc);
dbhelper.close();
result = "success";
MIC_OrderDetails mic_OrderDetails = new MIC_OrderDetails();
mic_OrderDetails.setResult(result);
lst.add(mic_OrderDetails);
}
catch (Exception e) {
result = "error";
MIC_OrderDetails mic_OrderDetails = new MIC_OrderDetails();
mic_OrderDetails.setResult(result);
lst.add(mic_OrderDetails);
dialog.dismiss();
Log.e("Failed", e.getLocalizedMessage());
}
return lst;
}
记录错误
07-04 12:45:45.466: W/System.err(26516): java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.
07-04 12:45:45.474: W/System.err(26516): at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked(SQLiteConnectionPool.java:972)
07-04 12:45:45.474: W/System.err(26516): at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:609)
07-04 12:45:45.474: W/System.err(26516): at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:349)
07-04 12:45:45.474: W/System.err(26516): at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:894)
07-04 12:45:45.474: W/System.err(26516): at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:834)
07-04 12:45:45.474: W/System.err(26516): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
07-04 12:45:45.474: W/System.err(26516): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:143)
07-04 12:45:45.474: W/System.err(26516): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
07-04 12:45:45.474: W/System.err(26516): at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:196)
07-04 12:45:45.474: W/System.err(26516): at android.database.AbstractCursor.moveToNext(AbstractCursor.java:244)
07-04 12:45:45.474: W/System.err(26516): at com.mis.database.DatabaseHandler.getdata(DatabaseHandler.java:1345)
07-04 12:45:45.474: W/System.err(26516): at com.mis.controller.InventoryCount$InflateList.doInBackground(InventoryCount.java:4052)
07-04 12:45:45.474: W/System.err(26516): at com.mis.controller.InventoryCount$InflateList.doInBackground(InventoryCount.java:1)
07-04 12:45:45.474: W/System.err(26516): at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-04 12:45:45.474: W/System.err(26516): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-04 12:45:45.474: W/System.err(26516): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-04 12:45:45.474: W/System.err(26516): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-04 12:45:45.474: W/System.err(26516): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-04 12:45:45.474: W/System.err(26516): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-04 12:45:45.474: W/System.err(26516): at java.lang.Thread.run(Thread.java:856)