【发布时间】:2011-10-27 05:58:41
【问题描述】:
您好,我正在尝试清理我的代码以删除指出我的数据库连接已打开但未正确关闭的错误。
如果我有一个 db.open(),那么在 db 查询周围有几个 try catch 语句,那么 try catch 之后的 db.close() 将在 try catch 中使用相同的 db 连接,还是 java 离散地运行 try catch 语句,因此 db 连接不可用。
简而言之,我应该有这样的代码:
db.open();
try {
// here
db.getThings()
} catch () {
// there
}
try {
// here 2
db.getMoreThings()
} catch () {
// there 2
}
db.close();
或者这个:
try {
// here
db.open();
db.getThings()
db.close();
} catch () {
// there
}
try {
// here 2
db.open();
db.getMoreThings()
db.close();
} catch () {
// there 2
}
我认为打开一个数据库连接的第一个解决方案是更好的解决方案。但是我遇到了不关闭数据库连接的问题,我正在考虑我的设计存在根本问题。
我也尝试在 onResume() 中打开连接,然后在 onPause() 中关闭,但仍然有问题。
【问题讨论】:
标签: java android database connection try-catch