/** * Called by {@link BoxStore#callInReadTx(Callable)} - does not throw so caller does not need try/finally. */ void readTxFinished(Transaction tx) { Cursor<T> cursor = activeTxCursor.get(); if (cursor != null && cursor.getTx() == tx) { activeTxCursor.remove(); cursor.close(); } }
void commitWriter(Cursor<T> cursor) { // NOP if TX is ongoing if (activeTxCursor.get() == null) { cursor.close(); cursor.getTx().commitAndClose(); } }
Cursor<T> getActiveTxCursor() { Transaction activeTx = store.activeTx.get(); if (activeTx != null) { if (activeTx.isClosed()) { throw new IllegalStateException("Active TX is closed"); } Cursor<T> cursor = activeTxCursor.get(); if (cursor == null || cursor.getTx().isClosed()) { cursor = activeTx.createCursor(entityClass); activeTxCursor.set(cursor); } return cursor; } return null; }
void releaseReader(Cursor<T> cursor) { // NOP if TX is ongoing if (activeTxCursor.get() == null) { Transaction tx = cursor.getTx(); if (tx.isClosed() || tx.isRecycled() || !tx.isReadOnly()) { throw new IllegalStateException("Illegal reader TX state"); } tx.recycle(); } }
public String getReaderDebugInfo() { Cursor<T> reader = getReader(); try { return reader + " with " + reader.getTx() + "; store's commit count: " + getStore().commitCount; } finally { releaseReader(reader); } }
void releaseWriter(Cursor<T> cursor) { // NOP if TX is ongoing if (activeTxCursor.get() == null) { Transaction tx = cursor.getTx(); if (!tx.isClosed()) { cursor.close(); tx.abort(); tx.close(); } } }
@Test public void testPutAndGetPrimitives() { Order order1 = putOrder(null, "order1"); Order order2 = putOrder(null, "order2"); Customer customer = putCustomer(); long customerId = customer.getId(); Cursor<Customer> cursorSource = InternalAccess.getWriter(customerBox); long[] orderIds = {order1.getId(), order2.getId()}; cursorSource.modifyRelations(1, customerId, orderIds, false); RelationInfo<Customer, Order> info = Customer_.ordersStandalone; int sourceEntityId = info.sourceInfo.getEntityId(); Cursor<Order> targetCursor = cursorSource.getTx().createCursor(Order.class); List<Order> related = targetCursor.getRelationEntities(sourceEntityId, info.relationId, customerId, false); assertEquals(2, related.size()); assertEquals(order1.getId(), related.get(0).getId()); assertEquals(order2.getId(), related.get(1).getId()); // Also InternalAccess.commitWriter(customerBox, cursorSource); assertEquals(2, orderBox.internalGetRelationEntities(sourceEntityId, info.relationId, customerId, false).size()); }