Broadcast events after committing transactions.

This commit is contained in:
akwizgran
2016-02-11 14:49:41 +00:00
parent de8cc50fb4
commit 146dac056d
6 changed files with 88 additions and 85 deletions

View File

@@ -49,9 +49,9 @@ public interface DatabaseComponent {
Transaction startTransaction() throws DbException;
/**
* Ends a transaction. If the transaction's
* {@link Transaction#setComplete() commit} flag is set, the
* transaction is committed, otherwise it is aborted.
* Ends a transaction. If the transaction is marked as complete, the
* transaction is committed and any events attached to the transaction are
* broadcast; otherwise the transaction is aborted.
*/
void endTransaction(Transaction txn) throws DbException;

View File

@@ -1,11 +1,19 @@
package org.briarproject.api.db;
import org.briarproject.api.event.Event;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* A wrapper around a database transaction. Transactions are not thread-safe.
*/
public class Transaction {
private final Object txn;
private List<Event> events = null;
private boolean complete = false;
public Transaction(Object txn) {
@@ -20,6 +28,23 @@ public class Transaction {
return txn;
}
/**
* Attaches an event to be broadcast when the transaction has been
* committed.
*/
public void attach(Event e) {
if (events == null) events = new ArrayList<Event>();
events.add(e);
}
/**
* Returns any events attached to the transaction.
*/
public List<Event> getEvents() {
if (events == null) return Collections.emptyList();
return events;
}
/**
* Returns true if the transaction is ready to be committed.
*/