Skip to content

Commit 68d5ffc

Browse files
committed
Cursor & Transaction: always log error if not closed
1 parent faf6cbc commit 68d5ffc

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
@Internal
3030
@NotThreadSafe
3131
public abstract class Cursor<T> implements Closeable {
32-
static final boolean WARN_FINALIZER = false;
32+
/** May be set by tests */
33+
@Internal
34+
static boolean TRACK_CREATION_STACK;
3335

3436
protected static final int PUT_FLAG_FIRST = 1;
3537
protected static final int PUT_FLAG_COMPLETE = 1 << 1;
@@ -136,19 +138,23 @@ protected Cursor(Transaction tx, long cursor, EntityInfo entityInfo, BoxStore bo
136138
property.verifyId(id);
137139
}
138140
}
139-
creationThrowable = WARN_FINALIZER ? new Throwable() : null;
141+
creationThrowable = TRACK_CREATION_STACK ? new Throwable() : null;
140142

141143
nativeSetBoxStoreForEntities(cursor, boxStore);
142144
}
143145

144146
@Override
145147
protected void finalize() throws Throwable {
146-
if (WARN_FINALIZER && !closed && creationThrowable != null) {
147-
System.err.println("Cursor was not closed. It was initially created here:");
148-
creationThrowable.printStackTrace();
148+
if (!closed) {
149+
System.err.println("Cursor was not closed.");
150+
if (creationThrowable != null) {
151+
System.err.println("Cursor was initially created here:");
152+
creationThrowable.printStackTrace();
153+
}
154+
System.err.flush();
155+
close();
156+
super.finalize();
149157
}
150-
close();
151-
super.finalize();
152158
}
153159

154160
protected abstract long getId(T entity);
@@ -223,8 +229,8 @@ public List<T> find(Property property, String value) {
223229
}
224230

225231
/**
226-
* @deprecated TODO only used in tests, remove in the future
227232
* @return key or 0 if not found
233+
* @deprecated TODO only used in tests, remove in the future
228234
*/
229235
long lookupKeyUsingIndex(int propertyId, String value) {
230236
return nativeLookupKeyUsingIndex(cursor, propertyId, value);

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
@Internal
2727
@NotThreadSafe
2828
public class Transaction implements Closeable {
29-
static final boolean WARN_FINALIZER = false;
29+
/** May be set by tests */
30+
@Internal
31+
static boolean TRACK_CREATION_STACK;
3032

3133
private final long transaction;
3234
private final BoxStore store;
@@ -68,14 +70,18 @@ public Transaction(BoxStore store, long transaction, int initialCommitCount) {
6870
this.initialCommitCount = initialCommitCount;
6971
readOnly = nativeIsReadOnly(transaction);
7072

71-
creationThrowable = WARN_FINALIZER ? new Throwable() : null;
73+
creationThrowable = TRACK_CREATION_STACK ? new Throwable() : null;
7274
}
7375

7476
@Override
7577
protected void finalize() throws Throwable {
76-
if (WARN_FINALIZER && !closed && creationThrowable != null) {
77-
System.err.println("Transaction was not closed. It was initially created here:");
78-
creationThrowable.printStackTrace();
78+
if (!closed) {
79+
System.err.println("Transaction was not closed (initial commit count: " + initialCommitCount + ").");
80+
if (creationThrowable != null) {
81+
System.err.println("Transaction was initially created here:");
82+
creationThrowable.printStackTrace();
83+
}
84+
System.err.flush();
7985
}
8086
close();
8187
super.finalize();

tests/objectbox-java-test/src/main/java/io/objectbox/AbstractObjectBoxTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@
2929

3030
import io.objectbox.ModelBuilder.EntityBuilder;
3131
import io.objectbox.ModelBuilder.PropertyBuilder;
32-
import io.objectbox.internal.CursorFactory;
33-
import io.objectbox.internal.IdGetter;
3432
import io.objectbox.model.PropertyFlags;
3533
import io.objectbox.model.PropertyType;
3634

37-
3835
import static org.junit.Assert.assertEquals;
3936
import static org.junit.Assert.assertTrue;
4037

@@ -52,6 +49,9 @@ public abstract class AbstractObjectBoxTest {
5249

5350
@Before
5451
public void setUp() throws IOException {
52+
Cursor.TRACK_CREATION_STACK = true;
53+
Transaction.TRACK_CREATION_STACK = true;
54+
5555
// This works with Android without needing any context
5656
File tempFile = File.createTempFile("object-store-test", "");
5757
tempFile.delete();

0 commit comments

Comments
 (0)