Skip to content

Commit 2ac2d9b

Browse files
committed
enforce Cursor.renew() to be tied to the same TX
1 parent 3dd4bf8 commit 2ac2d9b

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Cursor<T> getReader() {
7474
throw new IllegalStateException("Illegal reader TX state");
7575
}
7676
tx.renew();
77-
cursor.renew(tx);
77+
cursor.renew();
7878
} else {
7979
cursor = store.beginReadTx().createCursor(entityClass);
8080
threadLocalReader.set(cursor);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected static native long collect004000(long cursor, long keyIfComplete, int
116116

117117
static native void nativeSetBoxStoreForEntities(long cursor, Object boxStore);
118118

119-
protected Transaction tx;
119+
protected final Transaction tx;
120120
protected final long cursor;
121121
protected final EntityInfo entityInfo;
122122
protected final BoxStore boxStoreForEntities;
@@ -264,9 +264,11 @@ protected <TARGET> Cursor<TARGET> getRelationTargetCursor(Class<TARGET> targetCl
264264
return tx.createCursor(targetClass);
265265
}
266266

267-
public void renew(Transaction tx) {
267+
/**
268+
* To be used in combination with {@link Transaction#renew()}.
269+
* */
270+
public void renew() {
268271
nativeRenew(cursor, tx.internalHandle());
269-
this.tx = tx;
270272
}
271273

272274
@Internal

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,16 @@ public void reset() {
131131
nativeReset(transaction);
132132
}
133133

134-
/** For read transactions. */
134+
/**
135+
* For read transactions, this releases important native resources that hold on versions of potential old data.
136+
* To continue, use {@link #renew()}.
137+
*/
135138
public void recycle() {
136139
checkOpen();
137140
nativeRecycle(transaction);
138141
}
139142

140-
/** Efficient for read transactions. */
143+
/** Renews a previously recycled transaction (see {@link #recycle()}). Efficient for read transactions. */
141144
public void renew() {
142145
checkOpen();
143146
initialCommitCount = store.commitCount;

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,9 @@ public void testRenew() throws IOException {
316316

317317
Transaction transaction = store.beginReadTx();
318318
Cursor<TestEntity> cursor = transaction.createCursor(TestEntity.class);
319-
transaction.close();
320-
transaction = store.beginReadTx();
321-
cursor.renew(transaction);
322-
assertSame(transaction, cursor.getTx());
319+
transaction.recycle();
320+
transaction.renew();
321+
cursor.renew();
323322
assertEquals("orange", cursor.get(1).getSimpleString());
324323

325324
cursor.close();

0 commit comments

Comments
 (0)