User: mulder  
  Date: 00/09/29 06:50:18

  Modified:    src/main/org/jboss/minerva/xa XAConnectionImpl.java
                        XAResourceImpl.java
  Log:
  Allow you to keep a Connection open across a transaction commit or
  rollback.  This almost certainly does not have the behavior you want
  (the connection is not associated with a transaction ever again), so
  it prints a message to stderr.
  
  Only close an XAConnection if all client Connections referring to it
  have been closed.
  
  Revision  Changes    Path
  1.5       +5 -1      jboss/src/main/org/jboss/minerva/xa/XAConnectionImpl.java
  
  Index: XAConnectionImpl.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jboss/src/main/org/jboss/minerva/xa/XAConnectionImpl.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- XAConnectionImpl.java     2000/08/31 17:28:48     1.4
  +++ XAConnectionImpl.java     2000/09/29 13:50:17     1.5
  @@ -40,7 +40,7 @@
    * also register a TransactionListener that will be notified when the
    * Transaction is finished, and release the XAConnection at that time.</P>
    * @see org.jboss.minerva.xa.TransactionListener
  - * @version $Revision: 1.4 $
  + * @version $Revision: 1.5 $
    * @author Aaron Mulder ([EMAIL PROTECTED])
    */
   public class XAConnectionImpl implements XAConnection {
  @@ -49,6 +49,7 @@
       private XAResourceImpl resource;
       private Vector listeners;
       private TransactionListener transListener;
  +    private int clientConnectionCount = 0;
   
       /**
        * Creates a new transactional wrapper.
  @@ -103,6 +104,8 @@
        * returned to a pool.  If not, it can be closed or returned immediately.
        */
       public void clientConnectionClosed() {
  +        if(--clientConnectionCount > 0)
  +            return;  // Only take action if the last connection referring to this 
is closed
           boolean trans = resource.isTransaction(); // could be committed directly on 
notification?  Seems unlikely, but let's not rule it out.
           Vector local = (Vector)listeners.clone();
           for(int i=local.size()-1; i>=0; i--)
  @@ -169,6 +172,7 @@
       }
   
       public Connection getConnection() {
  +        ++clientConnectionCount;
           return new XAClientConnection(this, con);
       }
   }
  
  
  
  1.6       +14 -8     jboss/src/main/org/jboss/minerva/xa/XAResourceImpl.java
  
  Index: XAResourceImpl.java
  ===================================================================
  RCS file: 
/products/cvs/ejboss/jboss/src/main/org/jboss/minerva/xa/XAResourceImpl.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- XAResourceImpl.java       2000/08/31 18:23:37     1.5
  +++ XAResourceImpl.java       2000/09/29 13:50:17     1.6
  @@ -22,7 +22,7 @@
    * <P><FONT COLOR="RED"><B>Warning:</B></FONT></P> This implementation assumes
    * that forget will be called after a failed commit or rollback.  Otherwise,
    * the database connection will never be closed.</P>
  - * @version $Revision: 1.5 $
  + * @version $Revision: 1.6 $
    * @author Aaron Mulder ([EMAIL PROTECTED])
    */
   public class XAResourceImpl implements XAResource {
  @@ -81,8 +81,8 @@
        *     differs depending on the exact situation.
        */
       public void commit(Xid id, boolean twoPhase) throws XAException {
  -        if(active) // End was not called!
  -            throw new XAException(XAException.XAER_PROTO);
  +        if(active && !twoPhase) // End was not called!
  +            System.err.println("WARNING: Connection not closed before transaction 
commit.\nConnection will not participate in any future transactions.\nAre you sure you 
want to be doing this?");
           if(current == null || !id.equals(current)) // wrong Xid
               throw new XAException(XAException.XAER_NOTA);
   
  @@ -106,7 +106,10 @@
                       // Truly, neither committed nor rolled back.  Ouch!
           }
           current = null;
  -        xaCon.transactionFinished();
  +        if(active)
  +            active = false; // No longer associated with the original transaction
  +        else
  +            xaCon.transactionFinished();  // No longer in use at all
       }
   
       /**
  @@ -137,7 +140,7 @@
           current = null;
           xaCon.transactionFailed();
           if(active) // End was not called!
  -            throw new XAException(XAException.XAER_PROTO);
  +            System.err.println("WARNING: Connection not closed before transaction 
forget.\nConnection will not participate in any future transactions.\nAre you sure you 
want to be doing this?");
       }
   
       /**
  @@ -166,7 +169,7 @@
        */
       public int prepare(Xid id) throws javax.transaction.xa.XAException {
           if(active) // End was not called!
  -            throw new XAException(XAException.XAER_PROTO);
  +            System.err.println("WARNING: Connection not closed before transaction 
commit.\nConnection will not participate in any future transactions.\nAre you sure you 
want to be doing this?");
           if(current == null || !id.equals(current)) // wrong Xid
               throw new XAException(XAException.XAER_NOTA);
   
  @@ -201,7 +204,7 @@
        */
       public void rollback(Xid id) throws javax.transaction.xa.XAException {
           if(active) // End was not called!
  -            throw new XAException(XAException.XAER_PROTO);
  +            System.err.println("WARNING: Connection not closed before transaction 
rollback.\nConnection will not participate in any future transactions.\nAre you sure 
you want to be doing this?");
           if(current == null || !id.equals(current)) // wrong Xid
               throw new XAException(XAException.XAER_NOTA);
           try {
  @@ -215,7 +218,10 @@
               throw new XAException("Rollback failed: "+e.getMessage());
           }
           current = null;
  -        xaCon.transactionFinished();
  +        if(active)
  +            active = false; // No longer associated with the original transaction
  +        else
  +            xaCon.transactionFinished(); // No longer in use at all
       }
   
       /**
  
  
  

Reply via email to