Skip to content

Commit 2864d99

Browse files
greenrobotgreenrobot-team
authored andcommitted
Making closed flag in Store and Query volatile (#818)
This way checkOpen() is more accurate in multithreaded scenarios. Note that putting "synchronized" on checkOpen() would not help much, as the main race is between calling checkOpen() and actually using the handle. Alternatively, we could lock during the entire time the handle is in use, but this might already be overkill as the current behavior should uncover bad usage patterns already without sacrificing performance and risking new locking issues. (cherry picked from commit 38be697f774e395359b8717d4991f2fa707bd18c)
1 parent fd2119a commit 2864d99

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

objectbox-java/src/main/java/io/objectbox/BoxStore.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ public static boolean isSyncServerAvailable() {
232232
/** Set when running inside TX */
233233
final ThreadLocal<Transaction> activeTx = new ThreadLocal<>();
234234

235-
private boolean closed;
235+
// volatile so checkOpen() is more up-to-date (no need for synchronized; it's a race anyway)
236+
volatile private boolean closed;
236237

237238
final Object txCommitCountLock = new Object();
238239

objectbox-java/src/main/java/io/objectbox/query/Query.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ native void nativeSetParameter(long handle, int entityId, int propertyId, @Nulla
110110
private final int queryAttempts;
111111
private static final int INITIAL_RETRY_BACK_OFF_IN_MS = 10;
112112

113-
long handle;
113+
// volatile so checkOpen() is more up-to-date (no need for synchronized; it's a race anyway)
114+
volatile long handle;
114115

115116
Query(Box<T> box, long queryHandle, @Nullable List<EagerRelation<T, ?>> eagerRelations, @Nullable QueryFilter<T> filter,
116117
@Nullable Comparator<T> comparator) {

0 commit comments

Comments
 (0)