commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r584222 - /commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/managed/TransactionContext.java
Date Fri, 12 Oct 2007 18:12:52 GMT
Author: dain
Date: Fri Oct 12 11:12:52 2007
New Revision: 584222

URL: http://svn.apache.org/viewvc?rev=584222&view=rev
Log:
Change hard Transation reference to a weak reference so TransactionContext can be garbagecollected
from caches WeakHashMap in TransactionManager

Modified:
    commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/managed/TransactionContext.java

Modified: commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/managed/TransactionContext.java
URL: http://svn.apache.org/viewvc/commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/managed/TransactionContext.java?rev=584222&r1=584221&r2=584222&view=diff
==============================================================================
--- commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/managed/TransactionContext.java
(original)
+++ commons/proper/dbcp/trunk/src/java/org/apache/commons/dbcp/managed/TransactionContext.java
Fri Oct 12 11:12:52 2007
@@ -25,6 +25,7 @@
 import javax.transaction.xa.XAResource;
 import java.sql.Connection;
 import java.sql.SQLException;
+import java.lang.ref.WeakReference;
 
 /**
  * TransactionContext represents the association between a single XAConnectionFactory and
a Transaction.
@@ -37,7 +38,7 @@
  */
 public class TransactionContext {
     private final TransactionRegistry transactionRegistry;
-    private final Transaction transaction;
+    private final WeakReference transaction;
     private Connection sharedConnection;
 
     /**
@@ -53,7 +54,7 @@
         if (transactionRegistry == null) throw new NullPointerException("transactionRegistry
is null");
         if (transaction == null) throw new NullPointerException("transaction is null");
         this.transactionRegistry = transactionRegistry;
-        this.transaction = transaction;
+        this.transaction = new WeakReference(transaction);
     }
 
     /**
@@ -82,6 +83,7 @@
 
         // This is the first use of the connection in this transaction, so we must
         // enlist it in the transaction
+        Transaction transaction = getTransaction();
         try {
             XAResource xaResource = transactionRegistry.getXAResource(sharedConnection);
             transaction.enlistResource(xaResource);
@@ -102,7 +104,7 @@
      */
     public void addTransactionContextListener(final TransactionContextListener listener)
throws SQLException {
         try {
-            transaction.registerSynchronization(new Synchronization() {
+            getTransaction().registerSynchronization(new Synchronization() {
                 public void beforeCompletion() {
                 }
 
@@ -122,10 +124,22 @@
      */
     public boolean isActive() throws SQLException {
         try {
+            Transaction transaction = (Transaction) this.transaction.get();
+            if (transaction == null) {
+                return false;
+            }
             int status = transaction.getStatus();
             return status == Status.STATUS_ACTIVE || status == Status.STATUS_MARKED_ROLLBACK;
         } catch (SystemException e) {
             throw (SQLException) new SQLException("Unable to get transaction status").initCause(e);
         }
+    }
+
+    private Transaction getTransaction() throws SQLException {
+        Transaction transaction = (Transaction) this.transaction.get();
+        if (transaction == null) {
+            throw new SQLException("Unable to enlist connection the transaction becuase transaction
has been Garbage Collected");
+        }
+        return transaction;
     }
 }



Mime
View raw message