Skip to content

Commit 92197ab

Browse files
Query(Builder): implement Closeable. Document finalize methods.
1 parent 23d2312 commit 92197ab

File tree

6 files changed

+33
-7
lines changed

6 files changed

+33
-7
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@ static boolean isFileOpenSync(String canonicalPath, boolean runFinalization) {
320320
}
321321
}
322322

323+
/**
324+
* Explicitly call {@link #close()} instead to avoid expensive finalization.
325+
*/
326+
@SuppressWarnings("deprecation") // finalize()
323327
@Override
324328
protected void finalize() throws Throwable {
325329
close();
@@ -433,6 +437,7 @@ public void close() {
433437
synchronized (this) {
434438
oldClosedState = closed;
435439
if (!closed) {
440+
// Closeable recommendation: mark as closed before any code that might throw.
436441
closed = true;
437442
List<Transaction> transactionsToClose;
438443
synchronized (transactions) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ protected Cursor(Transaction tx, long cursor, EntityInfo entityInfo, BoxStore bo
156156
nativeSetBoxStoreForEntities(cursor, boxStore);
157157
}
158158

159+
/**
160+
* Explicitly call {@link #close()} instead to avoid expensive finalization.
161+
*/
162+
@SuppressWarnings("deprecation") // finalize()
159163
@Override
160164
protected void finalize() throws Throwable {
161165
if (!closed) {
@@ -217,6 +221,7 @@ public long count(long maxCountOrZero) {
217221
@Override
218222
public synchronized void close() {
219223
if (!closed) {
224+
// Closeable recommendation: mark as closed before nativeDestroy could throw.
220225
closed = true;
221226
// tx is null despite check in constructor in some tests (called by finalizer):
222227
// Null check avoids NPE in finalizer and seems to stabilize Android instrumentation perf tests.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public Transaction(BoxStore store, long transaction, int initialCommitCount) {
7777
creationThrowable = TRACK_CREATION_STACK ? new Throwable() : null;
7878
}
7979

80+
/**
81+
* Explicitly call {@link #close()} instead to avoid expensive finalization.
82+
*/
83+
@SuppressWarnings("deprecation") // finalize()
8084
@Override
8185
protected void finalize() throws Throwable {
8286
close();
@@ -92,6 +96,7 @@ private void checkOpen() {
9296
@Override
9397
public synchronized void close() {
9498
if (!closed) {
99+
// Closeable recommendation: mark as closed before any code that might throw.
95100
closed = true;
96101
store.unregisterTransaction(this);
97102

objectbox-java/src/main/java/io/objectbox/internal/DebugCursor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public DebugCursor(Transaction tx, long handle) {
5151
@Override
5252
public synchronized void close() {
5353
if (!closed) {
54+
// Closeable recommendation: mark as closed before any code that might throw.
5455
closed = true;
5556
// tx is null despite check in constructor in some tests (called by finalizer):
5657
// Null check avoids NPE in finalizer and seems to stabilize Android instrumentation perf tests.
@@ -60,6 +61,10 @@ public synchronized void close() {
6061
}
6162
}
6263

64+
/**
65+
* Explicitly call {@link #close()} instead to avoid expensive finalization.
66+
*/
67+
@SuppressWarnings("deprecation") // finalize()
6368
@Override
6469
protected void finalize() throws Throwable {
6570
if (!closed) {

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.objectbox.query;
1818

19+
import java.io.Closeable;
1920
import java.util.Collections;
2021
import java.util.Comparator;
2122
import java.util.Date;
@@ -45,7 +46,7 @@
4546
* @see QueryBuilder
4647
*/
4748
@SuppressWarnings({"SameParameterValue", "UnusedReturnValue", "WeakerAccess"})
48-
public class Query<T> {
49+
public class Query<T> implements Closeable {
4950

5051
native void nativeDestroy(long handle);
5152

@@ -112,7 +113,7 @@ native void nativeSetParameter(long handle, int entityId, int propertyId, @Nulla
112113
}
113114

114115
/**
115-
* Explicitly call {@link #close()} instead.
116+
* Explicitly call {@link #close()} instead to avoid expensive finalization.
116117
*/
117118
@SuppressWarnings("deprecation") // finalize()
118119
@Override
@@ -126,8 +127,10 @@ protected void finalize() throws Throwable {
126127
*/
127128
public synchronized void close() {
128129
if (handle != 0) {
129-
nativeDestroy(handle);
130+
// Closeable recommendation: mark as "closed" before nativeDestroy could throw.
131+
long handleCopy = handle;
130132
handle = 0;
133+
nativeDestroy(handleCopy);
131134
}
132135
}
133136

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.objectbox.query;
1818

19+
import java.io.Closeable;
1920
import java.util.ArrayList;
2021
import java.util.Comparator;
2122
import java.util.Date;
@@ -46,7 +47,7 @@
4647
*/
4748
@SuppressWarnings({"WeakerAccess", "UnusedReturnValue", "unused"})
4849
@Experimental
49-
public class QueryBuilder<T> {
50+
public class QueryBuilder<T> implements Closeable {
5051

5152
public enum StringOrder {
5253
/** The default: case insensitive ASCII characters */
@@ -191,7 +192,7 @@ private QueryBuilder(long storeHandle, long subQueryBuilderHandle) {
191192
}
192193

193194
/**
194-
* Explicitly call {@link #close()} instead.
195+
* Explicitly call {@link #close()} instead to avoid expensive finalization.
195196
*/
196197
@SuppressWarnings("deprecation") // finalize()
197198
@Override
@@ -202,10 +203,12 @@ protected void finalize() throws Throwable {
202203

203204
public synchronized void close() {
204205
if (handle != 0) {
206+
// Closeable recommendation: mark as "closed" before nativeDestroy could throw.
207+
long handleCopy = handle;
208+
handle = 0;
205209
if (!isSubQuery) {
206-
nativeDestroy(handle);
210+
nativeDestroy(handleCopy);
207211
}
208-
handle = 0;
209212
}
210213
}
211214

0 commit comments

Comments
 (0)