Bug 3195: Cleanup on error paths and error handling
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / LocalTransactionChain.java
index 39d0133d02f5f71eab1d509079f7fd0c688ba903..56466f78408fe24602564f9c62e14a8f5e31add5 100644 (file)
@@ -58,17 +58,7 @@ final class LocalTransactionChain extends AbstractSnapshotBackedTransactionChain
 
     @Override
     protected DOMStoreThreePhaseCommitCohort createCohort(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction, final DataTreeModification modification) {
-        return new LocalThreePhaseCommitCohort(parent.getActorContext(), leader, transaction, modification) {
-            @Override
-            protected void transactionAborted(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
-                onTransactionFailed(transaction, ABORTED);
-            }
-
-            @Override
-            protected void transactionCommitted(final SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
-                onTransactionCommited(transaction);
-            }
-        };
+        return new LocalChainThreePhaseCommitCohort(transaction, modification);
     }
 
     @Override
@@ -85,4 +75,39 @@ final class LocalTransactionChain extends AbstractSnapshotBackedTransactionChain
     public DOMStoreWriteTransaction newWriteOnlyTransaction(TransactionIdentifier identifier) {
         return super.newWriteOnlyTransaction(identifier);
     }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public LocalThreePhaseCommitCohort onTransactionReady(DOMStoreWriteTransaction tx) {
+        try {
+            return (LocalThreePhaseCommitCohort) tx.ready();
+        } catch (Exception e) {
+            // Unfortunately we need to cast to SnapshotBackedWriteTransaction here as it's required by
+            // LocalThreePhaseCommitCohort and the base class.
+            return new LocalChainThreePhaseCommitCohort((SnapshotBackedWriteTransaction<TransactionIdentifier>)tx, e);
+        }
+    }
+
+    private class LocalChainThreePhaseCommitCohort extends LocalThreePhaseCommitCohort {
+
+        protected LocalChainThreePhaseCommitCohort(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
+                DataTreeModification modification) {
+            super(parent.getActorContext(), leader, transaction, modification);
+        }
+
+        protected LocalChainThreePhaseCommitCohort(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction,
+                Exception operationError) {
+            super(parent.getActorContext(), leader, transaction, operationError);
+        }
+
+        @Override
+        protected void transactionAborted(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
+            onTransactionFailed(transaction, ABORTED);
+        }
+
+        @Override
+        protected void transactionCommitted(SnapshotBackedWriteTransaction<TransactionIdentifier> transaction) {
+            onTransactionCommited(transaction);
+        }
+    }
 }