geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From djen...@apache.org
Subject svn commit: rev 54937 - in geronimo/trunk/modules/transaction/src: java/org/apache/geronimo/transaction java/org/apache/geronimo/transaction/manager test/org/apache/geronimo/transaction test/org/apache/geronimo/transaction/context test/org/apache/geronimo/transaction/manager
Date Sat, 16 Oct 2004 19:41:31 GMT
Author: djencks
Date: Sat Oct 16 12:41:29 2004
New Revision: 54937

Modified:
   geronimo/trunk/modules/transaction/src/java/org/apache/geronimo/transaction/GeronimoTransactionManager.java
   geronimo/trunk/modules/transaction/src/java/org/apache/geronimo/transaction/manager/TransactionManagerImpl.java
   geronimo/trunk/modules/transaction/src/test/org/apache/geronimo/transaction/TransactionManagerProxyTest.java
   geronimo/trunk/modules/transaction/src/test/org/apache/geronimo/transaction/context/TransactionContextManagerTest.java
   geronimo/trunk/modules/transaction/src/test/org/apache/geronimo/transaction/manager/XATransactionTester.java
Log:
very basic support for transaction timeout

Modified: geronimo/trunk/modules/transaction/src/java/org/apache/geronimo/transaction/GeronimoTransactionManager.java
==============================================================================
--- geronimo/trunk/modules/transaction/src/java/org/apache/geronimo/transaction/GeronimoTransactionManager.java
(original)
+++ geronimo/trunk/modules/transaction/src/java/org/apache/geronimo/transaction/GeronimoTransactionManager.java
Sat Oct 16 12:41:29 2004
@@ -18,8 +18,7 @@
 package org.apache.geronimo.transaction;
 
 import java.util.Collection;
-
-import javax.transaction.Transaction;
+import javax.transaction.SystemException;
 import javax.transaction.TransactionManager;
 
 import org.apache.geronimo.gbean.GBeanInfo;
@@ -43,17 +42,17 @@
  * */
 public class GeronimoTransactionManager extends TransactionManagerProxy {
 
-    public GeronimoTransactionManager(TransactionLog transactionLog, Collection resourceManagers)
{
-        super(getConstructorParams(transactionLog, (ReferenceCollection)resourceManagers));
+    public GeronimoTransactionManager(int defaultTransactionTimeoutSeconds, TransactionLog
transactionLog, Collection resourceManagers) throws SystemException {
+        super(getConstructorParams(defaultTransactionTimeoutSeconds, transactionLog, (ReferenceCollection)resourceManagers));
     }
 
-    private static TransactionManagerProxy.ConstructorParams getConstructorParams(TransactionLog
transactionLog, ReferenceCollection resourceManagers) {
+    private static TransactionManagerProxy.ConstructorParams getConstructorParams(int defaultTransactionTimeoutSeconds,
TransactionLog transactionLog, ReferenceCollection resourceManagers) throws SystemException
{
         TransactionManagerProxy.ConstructorParams params = new TransactionManagerProxy.ConstructorParams();
         XidFactory xidFactory = new XidFactoryImpl("WHAT DO WE CALL IT?".getBytes());
         if (transactionLog == null) {
             transactionLog = new UnrecoverableLog();
         }
-        TransactionManager delegate = new TransactionManagerImpl(transactionLog, xidFactory);
+        TransactionManager delegate = new TransactionManagerImpl(defaultTransactionTimeoutSeconds,
transactionLog, xidFactory);
         Recovery recovery = new RecoveryImpl(transactionLog, xidFactory);
         params.delegate = delegate;
         params.xidImporter = (XidImporter) delegate;
@@ -67,13 +66,14 @@
     static {
         GBeanInfoFactory infoFactory = new GBeanInfoFactory(GeronimoTransactionManager.class);
 
+        infoFactory.addAttribute("defaultTransactionTimeoutSeconds", int.class, true);
         infoFactory.addReference("TransactionLog", TransactionLog.class);
         infoFactory.addReference("ResourceManagers", ResourceManager.class);
 
         infoFactory.addInterface(TransactionManager.class);
         infoFactory.addInterface(XidImporter.class);
 
-        infoFactory.setConstructor(new String[]{"TransactionLog", "ResourceManagers"});
+        infoFactory.setConstructor(new String[]{"defaultTransactionTimeoutSeconds", "TransactionLog",
"ResourceManagers"});
 
         GBEAN_INFO = infoFactory.getBeanInfo();
     }

Modified: geronimo/trunk/modules/transaction/src/java/org/apache/geronimo/transaction/manager/TransactionManagerImpl.java
==============================================================================
--- geronimo/trunk/modules/transaction/src/java/org/apache/geronimo/transaction/manager/TransactionManagerImpl.java
(original)
+++ geronimo/trunk/modules/transaction/src/java/org/apache/geronimo/transaction/manager/TransactionManagerImpl.java
Sat Oct 16 12:41:29 2004
@@ -17,6 +17,8 @@
 
 package org.apache.geronimo.transaction.manager;
 
+import java.util.Timer;
+import java.util.TimerTask;
 import javax.transaction.HeuristicMixedException;
 import javax.transaction.HeuristicRollbackException;
 import javax.transaction.InvalidTransactionException;
@@ -29,28 +31,36 @@
 import javax.transaction.xa.XAException;
 import javax.transaction.xa.Xid;
 
-import org.apache.geronimo.gbean.GBeanInfo;
-import org.apache.geronimo.gbean.GBeanInfoFactory;
 import org.apache.geronimo.transaction.log.UnrecoverableLog;
 
 /**
  * Simple implementation of a transaction manager.
- * TODO timeout functionality
+ * TODO transactionTimeoutMilliseconds functionality
+ * TODO shut down timer gracefully
  *
  * @version $Rev$ $Date$
  */
 public class TransactionManagerImpl implements TransactionManager, XidImporter {
     private final TransactionLog txnLog;
     private final XidFactory xidFactory;
-    private volatile int timeout;
+    private final int defaultTransactionTimeoutMilliseconds;
+    private volatile int transactionTimeoutMilliseconds;
     private final ThreadLocal threadTx = new ThreadLocal();
+    private final Timer timeoutTimer = new Timer(true);
 
     public TransactionManagerImpl() {
+        defaultTransactionTimeoutMilliseconds = 10 * 1000;
+        transactionTimeoutMilliseconds = defaultTransactionTimeoutMilliseconds;
         txnLog = new UnrecoverableLog();
         xidFactory = new XidFactoryImpl();
     }
 
-    public TransactionManagerImpl(TransactionLog txnLog, XidFactory xidFactory) {
+    public TransactionManagerImpl(int defaultTransactionTimeoutSeconds, TransactionLog txnLog,
XidFactory xidFactory) throws SystemException {
+        if (defaultTransactionTimeoutSeconds <= 0) {
+            throw new IllegalArgumentException("defaultTransactionTimeoutSeconds must be
positive: attempted value: " + defaultTransactionTimeoutSeconds);
+        }
+        this.defaultTransactionTimeoutMilliseconds = defaultTransactionTimeoutSeconds * 1000;
+        setTransactionTimeout(defaultTransactionTimeoutSeconds);
         this.txnLog = txnLog;
         this.xidFactory = xidFactory;
     }
@@ -60,7 +70,14 @@
     }
 
     public void setTransactionTimeout(int seconds) throws SystemException {
-        timeout = seconds;
+        if (seconds < 0) {
+            throw new SystemException("transaction timeout must be positive or 0 to reset
to default");
+        }
+        if (seconds == 0) {
+            transactionTimeoutMilliseconds = defaultTransactionTimeoutMilliseconds;
+        } else {
+            transactionTimeoutMilliseconds = seconds * 1000;
+        }
     }
 
     public int getStatus() throws SystemException {
@@ -73,6 +90,8 @@
             throw new NotSupportedException("Nested Transactions are not supported");
         }
         TransactionImpl tx = new TransactionImpl(xidFactory, txnLog);
+        TimerTask timeout = new TransactionTimeout(tx);
+        timeoutTimer.schedule(timeout, transactionTimeoutMilliseconds);
         threadTx.set(tx);
     }
 
@@ -124,12 +143,9 @@
         }
     }
 
+    //XidImporter implementation
     public Transaction importXid(Xid xid) throws XAException, SystemException {
-//        if (getStatus() != Status.STATUS_NO_TRANSACTION) {
-//            throw new XAException("Transaction already active in this thread");
-//        }
         TransactionImpl tx = new TransactionImpl(xid, xidFactory, txnLog);
-//        threadTx.set(tx);
         return tx;
     }
 
@@ -150,7 +166,7 @@
             }
         } else {
             try {
-                ((TransactionImpl)tx).preparedCommit();
+                ((TransactionImpl) tx).preparedCommit();
             } catch (SystemException e) {
                 throw new XAException();
             }
@@ -162,7 +178,7 @@
 
     public int prepare(Transaction tx) throws XAException {
         try {
-            return ((TransactionImpl)tx).prepare();
+            return ((TransactionImpl) tx).prepare();
         } catch (SystemException e) {
             throw new XAException();
         } catch (RollbackException e) {
@@ -181,6 +197,25 @@
     }
 
     public void setTransactionTimeout(long milliseconds) {
+    }
+
+    private static class TransactionTimeout extends TimerTask {
+
+        private final TransactionImpl tx;
+
+        public TransactionTimeout(TransactionImpl tx) {
+            this.tx = tx;
+        }
+
+        public void run() {
+            try {
+                tx.setRollbackOnly();
+            } catch (SystemException e) {
+                //??
+            } catch (IllegalStateException ise) {
+                //transaction was committed
+            }
+        }
     }
 
 }

Modified: geronimo/trunk/modules/transaction/src/test/org/apache/geronimo/transaction/TransactionManagerProxyTest.java
==============================================================================
--- geronimo/trunk/modules/transaction/src/test/org/apache/geronimo/transaction/TransactionManagerProxyTest.java
(original)
+++ geronimo/trunk/modules/transaction/src/test/org/apache/geronimo/transaction/TransactionManagerProxyTest.java
Sat Oct 16 12:41:29 2004
@@ -41,11 +41,8 @@
 import org.apache.geronimo.transaction.manager.XidFactoryImpl;
 
 /**
- *
- *
  * @version $Rev$ $Date$
- *
- * */
+ */
 public class TransactionManagerProxyTest extends TestCase {
 
     MockResourceManager rm1 = new MockResourceManager(true);
@@ -58,10 +55,20 @@
     TransactionLog transactionLog = new MockLog();
 
     XidFactory xidFactory = new XidFactoryImpl("tm1".getBytes());
-    TransactionManagerImpl transactionManager = new TransactionManagerImpl(transactionLog,
xidFactory);
+    TransactionManagerImpl transactionManager;
     Recovery recovery = new RecoveryImpl(transactionLog, xidFactory);
     ReferenceCollection resourceManagers = new TestReferenceCollection();
-    TransactionManagerProxy tm = new TransactionManagerProxy(transactionManager, transactionManager,
recovery, resourceManagers);
+    TransactionManagerProxy tm;
+
+    protected void setUp() throws Exception {
+        transactionManager = new TransactionManagerImpl(10, transactionLog, xidFactory);
+        tm = new TransactionManagerProxy(transactionManager, transactionManager, recovery,
resourceManagers);
+    }
+
+    protected void tearDown() throws Exception {
+        transactionManager = null;
+        tm = null;
+    }
 
     public void testNoResourcesCommit() throws Exception {
         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());

Modified: geronimo/trunk/modules/transaction/src/test/org/apache/geronimo/transaction/context/TransactionContextManagerTest.java
==============================================================================
--- geronimo/trunk/modules/transaction/src/test/org/apache/geronimo/transaction/context/TransactionContextManagerTest.java
(original)
+++ geronimo/trunk/modules/transaction/src/test/org/apache/geronimo/transaction/context/TransactionContextManagerTest.java
Sat Oct 16 12:41:29 2004
@@ -38,7 +38,7 @@
     private XidFactory xidFactory = new XidFactoryImpl("geronimo.test.tm".getBytes());
 
     protected void setUp() throws Exception {
-        TransactionManagerProxy tm = new GeronimoTransactionManager(null, null);
+        TransactionManagerProxy tm = new GeronimoTransactionManager(10, null, null);
         transactionContextManager = new TransactionContextManager(tm, tm, tm);
     }
 

Modified: geronimo/trunk/modules/transaction/src/test/org/apache/geronimo/transaction/manager/XATransactionTester.java
==============================================================================
--- geronimo/trunk/modules/transaction/src/test/org/apache/geronimo/transaction/manager/XATransactionTester.java
(original)
+++ geronimo/trunk/modules/transaction/src/test/org/apache/geronimo/transaction/manager/XATransactionTester.java
Sat Oct 16 12:41:29 2004
@@ -53,7 +53,7 @@
         XAResource xaRes = xaConn.getXAResource();
         log = new DummyLog();
         XidFactory xidFactory = new XidFactoryImpl();
-        manager = new TransactionManagerImpl(log, xidFactory);
+        manager = new TransactionManagerImpl(10, log, xidFactory);
         Connection c = xaConn.getConnection();
         Statement s = c.createStatement();
 

Mime
View raw message