Skip to content

Commit cf78072

Browse files
greenrobot-teamgreenrobot
authored andcommitted
Transaction: document some important state methods #282
1 parent 1667b47 commit cf78072

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,9 +1409,11 @@ public long getNativeStore() {
14091409
/**
14101410
* For internal use only. This API might change or be removed with a future release.
14111411
* <p>
1412-
* Returns if the native Store was closed.
1412+
* Returns {@code true} once the native Store is about to be destroyed.
14131413
* <p>
14141414
* This is {@code true} shortly after {@link #close()} was called and {@link #isClosed()} returns {@code true}.
1415+
*
1416+
* @see #isNativeStoreDestroyed()
14151417
*/
14161418
@Internal
14171419
public boolean isNativeStoreClosed() {
@@ -1421,9 +1423,11 @@ public boolean isNativeStoreClosed() {
14211423
/**
14221424
* For internal use only. This API might change or be removed with a future release.
14231425
* <p>
1424-
* Returns if the native Store was completely destroyed.
1426+
* Returns {@code true} once the native Store was destroyed.
14251427
* <p>
1426-
* This is {@code true} shortly after {@link #close()} was called and {@link #isClosed()} returns {@code true}.
1428+
* This is {@code true} shortly after {@link #isNativeStoreClosed()} returns {@code true}.
1429+
*
1430+
* @see #isNativeStoreClosed()
14271431
*/
14281432
@Internal
14291433
public boolean isNativeStoreDestroyed() {

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ public synchronized void close() {
131131
String threadType = isOwnerThread ? "owner thread" : "non-owner thread";
132132
if (readOnly) {
133133
// Minor leak, but still print it so we can check logs: it should only happen occasionally.
134-
// We cannot rely on the store waiting for read transactions; it only waits for a limited time.
134+
// Note that BoxStore.close() won't (briefly) wait on this transaction as isActive() already returns
135+
// false (and unregisterTransaction(this) was called) at this point.
135136
System.out.println("Info: closing read transaction after store was closed (should be avoided) in " +
136137
threadType);
137138
System.out.flush();
@@ -143,15 +144,19 @@ public synchronized void close() {
143144
// This is an internal validation: if this is a write-TX,
144145
// the (native) store will always wait for it, so it must not be destroyed yet.
145146
// If this ever happens, the above assumption is wrong, and it probably prevents a SIGSEGV.
146-
throw new IllegalStateException(
147-
"Cannot destroy write transaction for an already destroyed store");
147+
// Note that BoxStore.close() won't (briefly) wait on this transaction as isActive() already
148+
// returns false (and unregisterTransaction(this) was called).
149+
throw new IllegalStateException("Cannot close write transaction for an already closed store");
148150
}
149151
nativeDestroy(transaction);
150152
}
151153
}
152154
}
153155
}
154156

157+
/**
158+
* For a write transaction commits the changes. For a read transaction throws.
159+
*/
155160
public void commit() {
156161
checkOpen();
157162
int[] entityTypeIdsAffected = nativeCommit(transaction);
@@ -163,6 +168,9 @@ public void commitAndClose() {
163168
close();
164169
}
165170

171+
/**
172+
* For a read or write transaction, aborts it.
173+
*/
166174
public void abort() {
167175
checkOpen();
168176
nativeAbort(transaction);
@@ -214,6 +222,12 @@ public BoxStore getStore() {
214222
return store;
215223
}
216224

225+
/**
226+
* A transaction is active after it was created until {@link #close()} or {@link #abort()} or for a write
227+
* transaction also until {@link #commit()} is called.
228+
*
229+
* @return If this transaction is active.
230+
*/
217231
public boolean isActive() {
218232
return !closed && nativeIsActive(transaction);
219233
}

0 commit comments

Comments
 (0)