db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/odmg OJBJ2EE_2.java JTATxManager.java J2EETransactionImpl.java
Date Fri, 14 Mar 2003 01:38:15 GMT
arminw      2003/03/13 17:38:15

  Modified:    src/java/org/apache/ojb/odmg OJBJ2EE_2.java
                        JTATxManager.java J2EETransactionImpl.java
  Log:
  fix j2ee integration, all tests
  passed now, do some
  improvements(hope so :)
  
  Revision  Changes    Path
  1.7       +42 -0     db-ojb/src/java/org/apache/ojb/odmg/OJBJ2EE_2.java
  
  Index: OJBJ2EE_2.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/OJBJ2EE_2.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- OJBJ2EE_2.java	3 Jan 2003 21:38:26 -0000	1.6
  +++ OJBJ2EE_2.java	14 Mar 2003 01:38:15 -0000	1.7
  @@ -5,9 +5,15 @@
   import org.apache.ojb.broker.util.configuration.ConfigurationException;
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
  +import org.apache.ojb.odmg.oql.EnhancedOQLQuery;
   import org.odmg.Database;
   import org.odmg.Implementation;
   import org.odmg.Transaction;
  +import org.odmg.DList;
  +import org.odmg.DBag;
  +import org.odmg.DSet;
  +import org.odmg.DArray;
  +import org.odmg.DMap;
   
   /**
    *
  @@ -50,6 +56,42 @@
           beginInternTransaction();
           // we wrap the intern odmg transaction to avoid unauthorised calls
           return new NarrowTransaction(super.currentTransaction());
  +    }
  +
  +    public EnhancedOQLQuery newOQLQuery()
  +    {
  +        beginInternTransaction();
  +        return super.newOQLQuery();
  +    }
  +
  +    public DList newDList()
  +    {
  +        beginInternTransaction();
  +        return super.newDList();
  +    }
  +
  +    public DBag newDBag()
  +    {
  +        beginInternTransaction();
  +        return super.newDBag();
  +    }
  +
  +    public DSet newDSet()
  +    {
  +        beginInternTransaction();
  +        return super.newDSet();
  +    }
  +
  +    public DArray newDArray()
  +    {
  +        beginInternTransaction();
  +        return super.newDArray();
  +    }
  +
  +    public DMap newDMap()
  +    {
  +        beginInternTransaction();
  +        return super.newDMap();
       }
   
       /**
  
  
  
  1.4       +59 -52    db-ojb/src/java/org/apache/ojb/odmg/JTATxManager.java
  
  Index: JTATxManager.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/JTATxManager.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- JTATxManager.java	1 Feb 2003 18:29:37 -0000	1.3
  +++ JTATxManager.java	14 Mar 2003 01:38:15 -0000	1.4
  @@ -66,9 +66,6 @@
   import javax.transaction.SystemException;
   import javax.transaction.Transaction;
   import javax.transaction.TransactionManager;
  -import java.util.Iterator;
  -import java.util.Map;
  -import java.util.WeakHashMap;
   
   /**
    * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a>
  @@ -80,8 +77,8 @@
    */
   public class JTATxManager implements OJBTxManager
   {
  -    private Map m_transactionBuffer = new WeakHashMap();
       private static Logger log = LoggerFactory.getLogger(JTATxManager.class);
  +    private static ThreadLocal txRepository = new ThreadLocal();
   
       /**
        * Remove the ODMG transaction from the transaction buffer
  @@ -89,15 +86,16 @@
        */
       public void deregisterTx(Object transaction)
       {
  -        synchronized (m_transactionBuffer)
  +        TxBuffer buf = (TxBuffer) txRepository.get();
  +        if (buf != null)
           {
  -            m_transactionBuffer.remove(transaction);
  +            buf.setInternTx(null);
           }
       }
   
       public void registerTx(TransactionImpl odmgTrans)
       {
  -         if (log.isDebugEnabled()) log.debug("registerSynchronization was called");
  +        if (log.isDebugEnabled()) log.debug("registerSynchronization was called");
           Transaction transaction = null;
           try
           {
  @@ -158,10 +156,7 @@
               transaction.registerSynchronization((J2EETransactionImpl) odmgTrans);
               // 2. mark the ODMG transaction as being in a JTA Transaction
               // Associate external transaction with the odmg transaction.
  -            synchronized (m_transactionBuffer)
  -            {
  -                m_transactionBuffer.put(transaction, odmgTrans);
  -            }
  +            txRepository.set(new TxBuffer(odmgTrans, transaction));
           }
           catch (Exception e)
           {
  @@ -224,7 +219,7 @@
           if (log.isDebugEnabled()) log.debug("getTransaction called");
           if (getTransactionManager() == null)
           {
  -            log.info("TransactionManager was null");
  +            log.warn("TransactionManager was null");
               return null;
           }
           return getTransactionManager().getTransaction();
  @@ -251,15 +246,8 @@
        */
       public TransactionImpl getTransaction()
       {
  -        try
  -        {
  -            return (TransactionImpl) m_transactionBuffer.get(getJTATransaction());
  -        }
  -        catch (SystemException e)
  -        {
  -            log.error("exception getting transaction from transactionmanager: " + e.getMessage(),
e);
  -            throw new OJBRuntimeException("exception getting transaction from transactionmanager:
" + e.getMessage());
  -        }
  +        TxBuffer buf = (TxBuffer) txRepository.get();
  +        return buf != null ? buf.getInternTx() : null;
       }
   
       /**
  @@ -269,41 +257,20 @@
       {
           if (log.isDebugEnabled()) log.debug("abortExternTransaction was called");
           if (odmgTrans == null) return;
  -        synchronized (m_transactionBuffer)
  +        TxBuffer buf = (TxBuffer) txRepository.get();
  +        Transaction extTx = buf != null ? buf.externTx : null;
  +        try
           {
  -            if (m_transactionBuffer.containsValue(odmgTrans))
  +            if (extTx != null && extTx.getStatus() == Status.STATUS_ACTIVE)
               {
  -                Iterator it = m_transactionBuffer.keySet().iterator();
  -                TransactionImpl foundOdmgTrans = null;
  -                while (it.hasNext())
  -                {
  -                    Transaction transaction = (Transaction) it.next();
  -                    try
  -                    {
  -                        if (transaction == null || transaction.getStatus() != Status.STATUS_ACTIVE)
continue;
  -                    }
  -                    catch (SystemException ignore)
  -                    {
  -                    }
  -                    foundOdmgTrans = (TransactionImpl) m_transactionBuffer.get(transaction);
  -                    if (odmgTrans.equals(foundOdmgTrans))
  -                    {
  -                        log.info("Set extern transaction to rollback");
  -                        try
  -                        {
  -                            if (transaction.getStatus() == Status.STATUS_ACTIVE)
  -                            {
  -                                transaction.setRollbackOnly();
  -                            }
  -                        }
  -                        catch (Exception e)
  -                        {
  -                            log.warn("Cannot work on extern transaction", e);
  -                        }
  -                    }
  -                }
  +                log.info("Set extern transaction to rollback");
  +                extTx.setRollbackOnly();
               }
           }
  +        catch (Exception e)
  +        {
  +            log.warn("Cannot work on extern transaction", e);
  +        }
       }
   
       public void configure(Configuration config)
  @@ -311,5 +278,45 @@
           /**
            * no-op
            */
  +    }
  +
  +    //************************************************************************
  +    // inner class
  +    //************************************************************************
  +
  +    final class TxBuffer
  +    {
  +        Transaction externTx = null;
  +        TransactionImpl internTx = null;
  +
  +        public TxBuffer()
  +        {
  +        }
  +
  +        public TxBuffer(TransactionImpl internTx, Transaction externTx)
  +        {
  +            this.internTx = internTx;
  +            this.externTx = externTx;
  +        }
  +
  +        public Transaction getExternTx()
  +        {
  +            return externTx;
  +        }
  +
  +        public void setExternTx(Transaction externTx)
  +        {
  +            this.externTx = externTx;
  +        }
  +
  +        public TransactionImpl getInternTx()
  +        {
  +            return internTx;
  +        }
  +
  +        public void setInternTx(TransactionImpl internTx)
  +        {
  +            this.internTx = internTx;
  +        }
       }
   }
  
  
  
  1.16      +91 -40    db-ojb/src/java/org/apache/ojb/odmg/J2EETransactionImpl.java
  
  Index: J2EETransactionImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/J2EETransactionImpl.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- J2EETransactionImpl.java	6 Jan 2003 22:47:33 -0000	1.15
  +++ J2EETransactionImpl.java	14 Mar 2003 01:38:15 -0000	1.16
  @@ -59,6 +59,8 @@
   
   import javax.transaction.Status;
   import javax.transaction.Synchronization;
  +import javax.transaction.SystemException;
  +
   import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.util.GUID;
   import org.apache.ojb.broker.util.logging.Logger;
  @@ -70,7 +72,7 @@
       private Logger log = LoggerFactory.getLogger(J2EETransactionImpl.class);
       private boolean isInExternTransaction;
       private String txGUID;
  -    private OJBTxManager txManager = TxManagerFactory.instance();
  +
       /**
        * beforeCompletion is being called twice in JBoss, so this
        * isPrepared flag prevents code from executing twice.
  @@ -132,54 +134,102 @@
        * FOR internal use. This method was called before the external transaction was completed.
        * @see javax.transaction.Synchronization
        */
  +//    public void beforeCompletion()
  +//    {
  +//        if (!m_isPrepared)
  +//        {
  +//            try
  +//            {
  +//                try
  +//                {
  +//                    JTATxManager mgr = (JTATxManager)TxManagerFactory.instance();
  +//                    if (mgr.getJTATransaction().getStatus() == Status.STATUS_MARKED_ROLLBACK)
  +//                    {
  +//                        abort();
  +//                    }
  +//                    else
  +//                    {
  +//                        this.prepare();
  +//                        commit();
  +//                    }
  +//                    /*
  +//                     * MBAIRD: hack to get around the lack of swizzling.
  +//                     */
  +//                    getBroker().clearCache();
  +//                }
  +//                catch (Throwable t)
  +//                {
  +//                    log.error(t);
  +//                    abort();
  +//    //                if (log.isDebugEnabled())
  +//    //                {
  +//    //                    log.debug(t.getMessage());
  +//    //                    t.printStackTrace();
  +//    //                }
  +//                }
  +//            }
  +//            finally
  +//            {
  +//                m_isPrepared = true;
  +//				try
  +//		        {
  +//		            doClose();
  +//		            setInExternTransaction(false);
  +//		        }
  +//		        catch (Throwable t)
  +//		        {
  +//		            if (log.isDebugEnabled())
  +//		            {
  +//		                log.debug(t.getMessage());
  +//		                t.printStackTrace();
  +//		            }
  +//		        }
  +//            }
  +//        }
  +//    }
  +
       public void beforeCompletion()
       {
  -        if (!m_isPrepared)
  +        // avoid redundant calls
  +        if (m_isPrepared) return;
  +        OJBRuntimeException ex = null;
  +        try
           {
               try
               {
  -				JTATxManager mgr = (JTATxManager)TxManagerFactory.instance();
  -				if (mgr.getJTATransaction().getStatus() == Status.STATUS_MARKED_ROLLBACK)
  -				{
  -					abort();
  -				}
  -				else
  -				{
  -					this.prepare();
  -					commit();
  -				}
  -                /*
  -                 * MBAIRD: hack to get around the lack of swizzling.
  -                 */
  -                getBroker().clearCache();
  -            }
  -            catch (Throwable t)
  -            {
  -				abort();
  -                if (log.isDebugEnabled())
  +                JTATxManager mgr = (JTATxManager)TxManagerFactory.instance();
  +                if (mgr.getJTATransaction().getStatus() == Status.STATUS_MARKED_ROLLBACK)
                   {
  -                    log.debug(t.getMessage());
  -                    t.printStackTrace();
  +                    ex = new OJBRuntimeException("Could not commit, because tx status was
marked rollback");
                   }
  +                else
  +                {
  +                    try
  +                    {
  +                        prepare();
  +                        commit();
  +                    }
  +                    catch (Exception e)
  +                    {
  +                        ex = new OJBRuntimeException("beforeCompletion: commit failed",
e);
  +                    }
  +                }
  +            }
  +            catch (Exception e)
  +            {
  +                ex = new OJBRuntimeException("beforeCompletion: commit failed", e);
               }
  -            finally
  +            if(ex != null)
               {
  -                m_isPrepared = true;
  -				try
  -		        {
  -		            doClose();
  -		            setInExternTransaction(false);
  -		        }
  -		        catch (Throwable t)
  -		        {
  -		            if (log.isDebugEnabled())
  -		            {
  -		                log.debug(t.getMessage());
  -		                t.printStackTrace();
  -		            }
  -		        }
  +                log.error(ex);
  +                abort();
               }
           }
  +        finally
  +        {
  +            m_isPrepared = true;
  +            setInExternTransaction(false);
  +        }
       }
   
       public void commit()
  @@ -189,18 +239,18 @@
               // prepareForCommit was done before
               if (log.isDebugEnabled()) log.debug("Commit transaction " + this + ", commit
on broker " + broker);
               getBroker().commitTransaction();
  +            doClose();
           }
           catch (Exception ex)
           {
               // We should not reach this block
  -            log.warn("!! Error while commit PB-Instance, system could be in a inconsistent
condition !!");
  +            log.error("Error while commit used PB-Instance", ex);
               if (log.isDebugEnabled())
               {
                   log.debug(ex.getMessage());
                   ex.printStackTrace();
               }
               abort();
  -            throw new OJBRuntimeException("Unexpected error occured while commit PB", ex);
           }
       }
   
  @@ -210,6 +260,7 @@
           {
               doAbort();
               TxManagerFactory.instance().abortExternalTx(this);
  +            doClose();
           }
           catch (Throwable t)
           {
  
  
  

Mime
View raw message