Repository: incubator-tephra
Updated Branches:
  refs/heads/master d1f2146b6 -> 20bc04689


(TEPHRA-185) Add a way to pass a custom timeout to TransactionContext

This closes #14 from GitHub.

Signed-off-by: anew <a...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/incubator-tephra/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tephra/commit/20bc0468
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tephra/tree/20bc0468
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tephra/diff/20bc0468

Branch: refs/heads/master
Commit: 20bc046892e1646bf496dab2d281b6af15779765
Parents: d1f2146
Author: anew <a...@apache.org>
Authored: Wed Sep 21 18:10:14 2016 -0700
Committer: anew <a...@apache.org>
Committed: Wed Sep 21 19:09:25 2016 -0700

----------------------------------------------------------------------
 .../org/apache/tephra/TransactionContext.java   | 31 ++++++++++++++++++++
 1 file changed, 31 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tephra/blob/20bc0468/tephra-core/src/main/java/org/apache/tephra/TransactionContext.java
----------------------------------------------------------------------
diff --git 
a/tephra-core/src/main/java/org/apache/tephra/TransactionContext.java 
b/tephra-core/src/main/java/org/apache/tephra/TransactionContext.java
index 22a59c6..7ca8f06 100644
--- a/tephra-core/src/main/java/org/apache/tephra/TransactionContext.java
+++ b/tephra-core/src/main/java/org/apache/tephra/TransactionContext.java
@@ -89,6 +89,36 @@ public class TransactionContext {
    */
   public void start() throws TransactionFailureException {
     currentTx = txClient.startShort();
+    startAllTxAwares();
+  }
+
+  /**
+   * Starts a new transaction.  Calling this will initiate a new transaction 
using the {@link TransactionSystemClient},
+   * and pass the returned transaction to {@link 
TransactionAware#startTx(Transaction)} for each registered
+   * TransactionAware.  If an exception is encountered, the transaction will 
be aborted and a
+   * {@code TransactionFailureException} wrapping the root cause will be 
thrown.
+   *
+   * @param timeout the transaction timeout for the transaction
+   *
+   * @throws TransactionFailureException if an exception occurs starting the 
transaction with any registered
+   *     TransactionAware
+   */
+  public void start(int timeout) throws TransactionFailureException {
+    currentTx = txClient.startShort(timeout);
+    startAllTxAwares();
+  }
+
+  /**
+   * This is a helper for {@link #start()} and {@link #start(int)}.
+   *
+   * Passes the current transaction to {@link 
TransactionAware#startTx(Transaction)} for each registered
+   * TransactionAware. If an exception is encountered, the transaction will be 
aborted and a
+   * {@code TransactionFailureException} wrapping the root cause will be 
thrown.
+   *
+   * @throws TransactionFailureException if an exception occurs starting the 
transaction with any registered
+   *     TransactionAware
+   */
+  private void startAllTxAwares() throws TransactionFailureException {
     for (TransactionAware txAware : txAwares) {
       try {
         txAware.startTx(currentTx);
@@ -97,6 +127,7 @@ public class TransactionContext {
                                        txAware.getTransactionAwareName(), 
currentTx.getTransactionId());
         LOG.warn(message, e);
         txClient.abort(currentTx);
+        currentTx = null;
         throw new TransactionFailureException(message, e);
       }
     }

Reply via email to