Skip to content

Commit f5a1450

Browse files
author
hideki
committed
Fixed try-catch-finally blocks in postChangeNotifications() method.
Also changed `open` variable to `AtomicBoolean` from `boolean`
1 parent 92cccf6 commit f5a1450

1 file changed

Lines changed: 33 additions & 36 deletions

File tree

src/main/java/com/couchbase/lite/Database.java

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public class Database implements StoreDelegate {
8989
private Store store = null;
9090
private String path;
9191
private String name;
92-
private boolean open = false;
92+
final private AtomicBoolean open = new AtomicBoolean(false);
9393

9494
private Map<String, View> views;
9595
private Map<String, String> viewDocTypes;
@@ -333,7 +333,7 @@ public Replication createPushReplication(URL remote) {
333333
*/
334334
@InterfaceAudience.Public
335335
public void delete() throws CouchbaseLiteException {
336-
if (open) {
336+
if (open.get()) {
337337
if (!close()) {
338338
throw new CouchbaseLiteException("The database was open, and could not be closed",
339339
Status.INTERNAL_SERVER_ERROR);
@@ -1080,7 +1080,7 @@ public synchronized void open() throws CouchbaseLiteException {
10801080

10811081
@InterfaceAudience.Private
10821082
public synchronized void open(DatabaseOptions options) throws CouchbaseLiteException {
1083-
if (open)
1083+
if (open.get())
10841084
return;
10851085

10861086
Log.v(TAG, "Opening %s", this);
@@ -1199,7 +1199,7 @@ public synchronized void open(DatabaseOptions options) throws CouchbaseLiteExcep
11991199
Status.INTERNAL_SERVER_ERROR);
12001200
}
12011201

1202-
open = true;
1202+
open.set(true);
12031203

12041204
if (upgrade) {
12051205
Log.i(TAG, "Upgrading to %s ...", storageType);
@@ -1251,7 +1251,7 @@ SymmetricKey createSymmetricKey(Object keyOrPassword) throws CouchbaseLiteExcept
12511251

12521252
@InterfaceAudience.Public
12531253
public boolean close() {
1254-
if (!open) {
1254+
if (!open.get()) {
12551255
// Ensure that the database is forgotten:
12561256
manager.forgetDatabase(this);
12571257
return false;
@@ -1305,7 +1305,7 @@ public boolean close() {
13051305
// Forget database:
13061306
manager.forgetDatabase(this);
13071307

1308-
open = false;
1308+
open.set(false);
13091309
return true;
13101310
}
13111311

@@ -1848,7 +1848,7 @@ public long getStartTime() {
18481848
*/
18491849
@InterfaceAudience.Private
18501850
public boolean isOpen() {
1851-
return open;
1851+
return open.get();
18521852
}
18531853

18541854
@InterfaceAudience.Private
@@ -2201,45 +2201,42 @@ private boolean postChangeNotifications() {
22012201
return false;
22022202
postingChangeNotifications = true;
22032203
}
2204-
22052204
try {
22062205
boolean posted = false;
2207-
22082206
// This is a 'while' instead of an 'if' because when we finish posting notifications, there
22092207
// might be new ones that have arrived as a result of notification handlers making document
22102208
// changes of their own (the replicator manager will do this.) So we need to check again.
2211-
while (!store.inTransaction() && isOpen() && changesToNotify.size() > 0) {
2212-
try {
2213-
List<DocumentChange> outgoingChanges = new ArrayList<DocumentChange>();
2214-
synchronized (changesToNotify) {
2215-
outgoingChanges.addAll(changesToNotify);
2216-
changesToNotify.clear();
2217-
}
2218-
2219-
// TODO: postPublicChangeNotification in CBLDatabase+Internal.m should replace
2220-
// following lines of code.
2221-
2222-
boolean isExternal = false;
2223-
for (DocumentChange change : outgoingChanges) {
2224-
Document doc = cachedDocumentWithID(change.getDocumentId());
2225-
if (doc != null)
2226-
doc.revisionAdded(change, true);
2227-
if (change.getSource() != null)
2228-
isExternal = true;
2229-
}
2209+
while (!store.inTransaction() && open.get() && changesToNotify.size() > 0) {
2210+
List<DocumentChange> outgoingChanges = new ArrayList<DocumentChange>();
2211+
synchronized (changesToNotify) {
2212+
outgoingChanges.addAll(changesToNotify);
2213+
changesToNotify.clear();
2214+
}
22302215

2231-
final ChangeEvent changeEvent = new ChangeEvent(this, isExternal, outgoingChanges);
2232-
synchronized (changeListeners) {
2233-
for (ChangeListener changeListener : changeListeners)
2234-
changeListener.changed(changeEvent);
2235-
}
2216+
// TODO: postPublicChangeNotification in CBLDatabase+Internal.m should replace
2217+
// following lines of code.
2218+
boolean isExternal = false;
2219+
for (DocumentChange change : outgoingChanges) {
2220+
Document doc = cachedDocumentWithID(change.getDocumentId());
2221+
if (doc != null)
2222+
doc.revisionAdded(change, true);
2223+
if (change.getSource() != null)
2224+
isExternal = true;
2225+
}
22362226

2237-
posted = true;
2238-
} catch (Exception e) {
2239-
Log.e(Database.TAG, this + " got exception posting change notifications", e);
2227+
final ChangeEvent changeEvent = new ChangeEvent(this, isExternal, outgoingChanges);
2228+
synchronized (changeListeners) {
2229+
for (ChangeListener changeListener : changeListeners)
2230+
changeListener.changed(changeEvent);
22402231
}
2232+
posted = true;
22412233
}
22422234
return posted;
2235+
} catch (Exception e) {
2236+
// In general, non of methods that are used in this method throws Exception.
2237+
// This catch block is just in case RuntimeExcepiton is thrown.
2238+
Log.e(TAG, "%s got exception posting change notifications", e, this);
2239+
return false;
22432240
} finally {
22442241
synchronized (lockPostingChangeNotifications) {
22452242
postingChangeNotifications = false;

0 commit comments

Comments
 (0)