Removed legacy binding-api concepts.
[mdsal.git] / common / mdsal-common-api / src / main / java / org / opendaylight / mdsal / common / api / TransactionChain.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.common.api;
9
10 import org.opendaylight.yangtools.concepts.Path;
11
12 /**
13  * A chain of transactions. Transactions in a chain need to be committed in
14  * sequence and each transaction should see the effects of previous committed transactions
15  * as they occurred. A chain makes no guarantees of atomicity across the chained transactions -
16  * the transactions are committed as soon as possible in the order that they were submitted.
17  *
18  * This behaviour is different from the default AsyncDataBroker, where a
19  * transaction is always created from the current global state, not taking into
20  * account any transactions previously committed by the calling thread. Due to
21  * the asynchronous nature of transaction submission this can lead to surprising
22  * results. If a thread executes the following sequence sufficiently quickly:
23  *
24  * <pre><code>
25  * AsyncWriteTransaction t1 = broker.newWriteOnlyTransaction();
26  * t1.put(id, data);
27  * t1.submit();
28  *
29  * AsyncReadTransaction t2 = broker.newReadOnlyTransaction();
30  * Optional&lt;?&gt; maybeData = t2.read(id).get();
31  * </code></pre>
32  *
33  * it may happen, that it sees maybeData.isPresent() == false, simply because
34  * t1 has not completed the processes of being applied and t2 is actually
35  * allocated from the previous state. This is obviously bad for users who create
36  * incremental state in the datastore and actually read what they write in
37  * subsequent transactions.
38  *
39  * Using a TransactionChain instead of a broker solves this particular problem,
40  * and leads to expected behavior: t2 will always see the data written in t1
41  * present.
42  */
43 public interface TransactionChain<P extends Path<P>, D> extends AutoCloseable,
44         AsyncDataTransactionFactory<P, D> {
45
46     /**
47      * Create a new read only transaction which will continue the chain.
48      *
49      * <p>
50      * The previous write transaction has to be either SUBMITTED
51      * ({@link AsyncWriteTransaction#submit submit} was invoked) or CANCELLED
52      * ({@link #close close} was invoked).
53      * <p>
54      * The returned read-only transaction presents an isolated view of the data if the previous
55      * write transaction was successful - in other words, this read-only transaction will see the
56      * state changes made by the previous write transaction in the chain. However, state which
57      * was introduced by other transactions outside this transaction chain after creation of
58      * the previous transaction is not visible.
59      *
60      * @return New transaction in the chain.
61      * @throws IllegalStateException
62      *             if the previous transaction was not SUBMITTED or CANCELLED.
63      * @throws TransactionChainClosedException
64      *             if the chain has been closed.
65      */
66     @Override
67     public AsyncReadOnlyTransaction<P, D> newReadOnlyTransaction();
68
69     /**
70      * Create a new read-write transaction which will continue the chain.
71      *
72      * <p>
73      * The previous write transaction has to be either SUBMITTED
74      * ({@link AsyncWriteTransaction#submit submit} was invoked) or CANCELLED
75      * ({@link #close close} was invoked).
76      * <p>
77      * The returned read-write transaction presents an isolated view of the data if the previous
78      * write transaction was successful - in other words, this read-write transaction will see the
79      * state changes made by the previous write transaction in the chain. However, state which
80      * was introduced by other transactions outside this transaction chain after creation of
81      * the previous transaction is not visible.
82      * <p>
83      * Committing this read-write transaction using {@link AsyncWriteTransaction#submit submit}
84      * will submit the state changes in this transaction to be visible to any subsequent
85      * transaction in this chain and also to any transaction outside this chain.
86      *
87      * @return New transaction in the chain.
88      * @throws IllegalStateException
89      *             if the previous transaction was not SUBMITTED or CANCELLED.
90      * @throws TransactionChainClosedException
91      *             if the chain has been closed.
92      */
93     @Override
94     public AsyncReadWriteTransaction<P, D> newReadWriteTransaction();
95
96     /**
97      * Create a new write-only transaction which will continue the chain.
98      *
99      * <p>
100      * The previous write transaction has to be either SUBMITTED
101      * ({@link AsyncWriteTransaction#submit submit} was invoked) or CANCELLED
102      * ({@link #close close} was invoked).
103      * <p>
104      * The returned write-only transaction presents an isolated view of the data if the previous
105      * write transaction was successful - in other words, this write-only transaction will see the
106      * state changes made by the previous write transaction in the chain. However, state which
107      * was introduced by other transactions outside this transaction chain after creation of
108      * the previous transaction is not visible.
109      * <p>
110      * Committing this write-only transaction using {@link AsyncWriteTransaction#submit submit}
111      * will submit the state changes in this transaction to be visible to any subsequent
112      * transaction in this chain and also to any transaction outside this chain.
113      *
114      * @return New transaction in the chain.
115      * @throws IllegalStateException
116      *             if the previous transaction was not SUBMITTED or CANCELLED.
117      * @throws TransactionChainClosedException
118      *             if the chain has been closed.
119      */
120     @Override
121     public AsyncWriteTransaction<P, D> newWriteOnlyTransaction();
122
123     @Override
124     void close();
125 }