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/broker PersistenceBrokerInternal.java
Date Sat, 18 Dec 2004 13:39:27 GMT
arminw      2004/12/18 05:39:26

  Modified:    src/java/org/apache/ojb/broker/core
                        DelegatingPersistenceBroker.java
                        PersistenceBrokerFactoryBaseImpl.java
                        PersistenceBrokerFactoryDefaultImpl.java
                        PersistenceBrokerImpl.java
                        PoolablePersistenceBroker.java
               src/java/org/apache/ojb/broker
                        PersistenceBrokerInternal.java
  Log:
  introduce PBInternal#destroy method
  
  Revision  Changes    Path
  1.18      +5 -0      db-ojb/src/java/org/apache/ojb/broker/core/DelegatingPersistenceBroker.java
  
  Index: DelegatingPersistenceBroker.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/DelegatingPersistenceBroker.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- DelegatingPersistenceBroker.java	5 Dec 2004 17:47:21 -0000	1.17
  +++ DelegatingPersistenceBroker.java	18 Dec 2004 13:39:25 -0000	1.18
  @@ -423,4 +423,9 @@
       {
           getBroker().setSqlInLimit(maxNum);
       }
  +
  +    public void destroy()
  +    {
  +        getBroker().destroy();
  +    }
   }
  
  
  
  1.6       +3 -7      db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerFactoryBaseImpl.java
  
  Index: PersistenceBrokerFactoryBaseImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerFactoryBaseImpl.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- PersistenceBrokerFactoryBaseImpl.java	14 Nov 2004 09:34:27 -0000	1.5
  +++ PersistenceBrokerFactoryBaseImpl.java	18 Dec 2004 13:39:25 -0000	1.6
  @@ -18,8 +18,6 @@
   import org.apache.ojb.broker.PBFactoryException;
   import org.apache.ojb.broker.PersistenceBroker;
   import org.apache.ojb.broker.PersistenceConfiguration;
  -import org.apache.ojb.broker.util.logging.Logger;
  -import org.apache.ojb.broker.util.logging.LoggerFactory;
   
   /**
    * This is an base implementation of the {@link PersistenceBrokerFactoryIF}
  @@ -33,9 +31,6 @@
    */
   public class PersistenceBrokerFactoryBaseImpl implements PersistenceBrokerFactoryIF
   {
  -    private Logger log = LoggerFactory.getLogger(PersistenceBrokerFactoryBaseImpl.class);
  -
  -    private Class implementationClass;
       private PersistenceConfiguration persistenceConfiguration;
   
       public PersistenceBrokerFactoryBaseImpl(PersistenceConfiguration persistenceConfiguration)
  @@ -53,7 +48,8 @@
        */
       protected PersistenceBroker newBrokerInstance() throws PBFactoryException
       {
  -        return (PersistenceBroker)persistenceConfiguration.getComponentContainer().getInstance(PersistenceBroker.class);
  +        return (PersistenceBroker) persistenceConfiguration.getComponentContainer()
  +                    .getInstance(PersistenceBroker.class);
       }
   
       public void releaseAllInstances()
  
  
  
  1.14      +2 -8      db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerFactoryDefaultImpl.java
  
  Index: PersistenceBrokerFactoryDefaultImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerFactoryDefaultImpl.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- PersistenceBrokerFactoryDefaultImpl.java	14 Nov 2004 09:34:27 -0000	1.13
  +++ PersistenceBrokerFactoryDefaultImpl.java	18 Dec 2004 13:39:25 -0000	1.14
  @@ -153,7 +153,6 @@
       public synchronized void releaseAllInstances()
       {
           log.warn("Release all instances referenced by this object");
  -        super.releaseAllInstances();
           try
           {
               brokerPool.clear();
  @@ -164,6 +163,7 @@
           {
               log.error("Clearing of broker pool failed", e);
           }
  +        super.releaseAllInstances();
       }
   
       /**
  @@ -240,12 +240,6 @@
           public void destroyObject(Object obj) throws Exception
           {
               PoolablePersistenceBroker pb = (PoolablePersistenceBroker) obj;
  -            PersistenceBroker broker = pb.getInnermostDelegate();
  -            if (broker instanceof PersistenceBrokerImpl)
  -            {
  -                log.info("Destroy PersistenceBroker instance " + obj);
  -                ((PersistenceBrokerImpl) broker).destroy();
  -            }
               pb.destroy();
           }
   
  
  
  
  1.106     +141 -119  db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java
  
  Index: PersistenceBrokerImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java,v
  retrieving revision 1.105
  retrieving revision 1.106
  diff -u -r1.105 -r1.106
  --- PersistenceBrokerImpl.java	5 Dec 2004 17:47:21 -0000	1.105
  +++ PersistenceBrokerImpl.java	18 Dec 2004 13:39:25 -0000	1.106
  @@ -28,6 +28,7 @@
   import java.util.List;
   import java.util.Set;
   
  +import org.apache.commons.lang.ObjectUtils;
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.IdentityFactory;
   import org.apache.ojb.broker.ManageableCollection;
  @@ -40,7 +41,6 @@
   import org.apache.ojb.broker.TransactionInProgressException;
   import org.apache.ojb.broker.TransactionNotInProgressException;
   import org.apache.ojb.broker.accesslayer.ChainingIterator;
  -import org.apache.ojb.broker.accesslayer.ConnectionFactory;
   import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
   import org.apache.ojb.broker.accesslayer.JdbcAccess;
   import org.apache.ojb.broker.accesslayer.OJBIterator;
  @@ -65,7 +65,6 @@
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
   import org.apache.ojb.broker.metadata.CollectionDescriptor;
  -import org.apache.ojb.broker.metadata.ConnectionFactoryDescriptor;
   import org.apache.ojb.broker.metadata.DescriptorRepository;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
   import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
  @@ -191,7 +190,6 @@
           subContainer             = container.createChildContainer();
   
           JdbcConnectionDescriptor    jcd         = pc.getJdbcConnectionDescriptor();
  -        ConnectionFactoryDescriptor connFacDesc = jcd.getConnectionFactoryDescriptor();
           SequenceDescriptor          seqDesc     = jcd.getSequenceDescriptor();
           BatchDescriptor             batchDesc   = jcd.getBatchDescriptor();
   
  @@ -220,10 +218,6 @@
                   subContainer.setImplementationClass(BatchStrategy.class, batchDesc.getBatchStrategy());
               }
           }
  -        if ((connFacDesc != null) && (connFacDesc.getConnectionFactoryClass() !=
null))
  -        {
  -            subContainer.setImplementationClass(ConnectionFactory.class, connFacDesc.getConnectionFactoryClass());
  -        }
   
           brokerHelper = new BrokerHelper(this);
           dbAccess     = (BatchManager)subContainer.getSingletonInstance(BatchManager.class);
  @@ -355,6 +349,7 @@
        */
       public void destroy()
       {
  +        logger.info("Destroy and cleanup this PB instance. " + this);
           removeAllListeners();
           if (connectionManager != null)
           {
  @@ -562,14 +557,26 @@
       {
           if(!isInTransaction())
           {
  -            try
  +            String msg = "No running PB-tx found. Please, delete objects in context of
an PB-transaction" +
  +                    " to avoid side-effects - e.g. when rollback of complex objects.";
  +            if(logger.isEnabledFor(Logger.INFO))
               {
  -                throw new Exception("No running tx.");
  +                try
  +                {
  +                    /*
  +                    arminw:
  +                    this could help user to find missing tx declaration in stack trace
  +                    */
  +                    throw new Exception("** Delete object without active PersistenceBroker
transaction **");
  +                }
  +                catch(Exception e)
  +                {
  +                    logger.info(msg, e);
  +                }
               }
  -            catch (Exception e)
  +            else
               {
  -                logger.warn("No running tx found for delete operation, please only delete
objects in context of an PB-transaction" +
  -                    ", to avoid side-effects - e.g. when rollback of complex objects",
e);
  +                logger.warn(msg + " Enable log-level INFO to get more detailed message
(stack trace).");
               }
           }
           try
  @@ -788,65 +795,145 @@
       }
   
       /**
  -     * Store an Object
  +     * Store an Object.
        * @see org.apache.ojb.broker.PersistenceBroker#store(Object)
        */
       public void store(Object obj) throws PersistenceBrokerException
       {
  +        obj = extractObjectToStore(obj);
           // only do something if obj != null
  -        if (obj != null && !nowStoring.contains(obj))
  +        if(obj == null) return;
  +
  +        ClassDescriptor cld = getClassDescriptor(obj.getClass());
  +        /*
  +        if one of the PK fields was null, we assume the objects
  +        was new and needs insert
  +        */
  +        boolean insert = serviceBrokerHelper().hasNullPKField(cld, obj);
  +        Identity oid = serviceIdentity().buildIdentity(cld, obj);
  +        /*
  +        if PK values are set, lookup cache or db to see whether object
  +        needs insert or update
  +        */
  +        if (!insert)
           {
  -            // ProxyObjects only have to be updated if their real subjects have been loaded
  -            obj = ProxyHelper.getRealObjectIfMaterialized(obj);
  -            if (obj == null)    // null for unmaterialized Proxy
  +            insert = localCache.lookup(oid) == null
  +                && !serviceJdbcAccess().doesExist(cld, oid);
  +        }
  +        store(obj, oid, cld, insert);
  +    }
  +
  +    /**
  +     * Check if the given object is <code>null</code> or an unmaterialized
proxy object - in
  +     * both cases <code>null</code> will be returned, else the given object
itself or the
  +     * materialized proxy object will be returned.
  +     */
  +    private Object extractObjectToStore(Object obj)
  +    {
  +        Object result = obj;
  +        // only do something if obj != null
  +        if(result != null)
  +        {
  +            // ProxyObjects only have to be updated if their real
  +            // subjects have been loaded
  +            result = ProxyHelper.getRealObjectIfMaterialized(obj);
  +            // null for unmaterialized Proxy
  +            if (result == null)
               {
                   if(logger.isDebugEnabled())
  -                    logger.debug("No materialized object could be found -> nothing to
store");
  -                return;
  +                    logger.debug("No materialized object could be found -> nothing to
store," +
  +                            " object was " + ObjectUtils.identityToString(obj));
               }
  +        }
  +        return result;
  +    }
   
  -            ClassDescriptor cld = getClassDescriptor(obj.getClass());
  -            /*
  -            if one of the PK fields was null, the objects was new
  -            and needs insert
  -            */
  -            boolean doInsert = serviceBrokerHelper().hasNullPKField(cld, obj);
  -            Identity oid = new Identity(obj, this, cld);
  +    /**
  +     * Internal used method which start the real store work.
  +     */
  +    protected void store(Object obj, Identity oid, ClassDescriptor cld,  boolean insert)
  +    {
  +        if(obj == null || nowStoring.contains(obj))
  +        {
  +            return;
  +        }
   
  +        /*
  +        if the object has been deleted during this transaction,
  +        then we must insert it
  +        */
  +        //System.out.println("## insert: " +insert + " / deleted: " + deletedDuringTransaction);
  +        if (!insert)
  +        {
  +            insert = deletedDuringTransaction.contains(oid);
  +        }
  +
  +        //************************************************
  +        // now store it:
  +        if(!isInTransaction())
  +        {
  +            logger.warn("No running tx found, please only store in context of an PB-transaction"
+
  +                    ", to avoid side-effects - e.g. when rollback of complex objects");
               /*
  -            if the object has been deleted during this transaction,
  -            then we must insert it
  +            arminw:
  +            this could help user to find missing tx declaration
               */
  -            boolean shouldRemoveFromDeleted = false;
  -            if (!doInsert)
  +            if(logger.isEnabledFor(Logger.INFO))
               {
  -                doInsert = deletedDuringTransaction.contains(oid);
  -                shouldRemoveFromDeleted = true;
  -                
  -                /*
  -                if PK values are set, lookup cache or db to see whether object
  -                needs insert or update
  -                */
  -                if (!doInsert)
  +                try
  +                {
  +                    throw new Exception("** Try to store object without active PersistenceBroker
transaction **");
  +                }
  +                catch(Exception e)
                   {
  -                    doInsert = localCache.lookup(oid) == null
  -                        && !serviceJdbcAccess().doesExist(cld, oid);
  +                    e.printStackTrace();
                   }
               }
  -            
  -            // now store it:
  -            store(obj, oid, cld, doInsert);
  +        }
  +        // Invoke events on PersistenceBrokerAware instances and listeners
  +        if (insert)
  +        {
  +            BEFORE_STORE_EVENT.setTarget(obj);
  +            fireBrokerEvent(BEFORE_STORE_EVENT);
  +            BEFORE_STORE_EVENT.setTarget(null);
  +        }
  +        else
  +        {
  +            BEFORE_UPDATE_EVENT.setTarget(obj);
  +            fireBrokerEvent(BEFORE_UPDATE_EVENT);
  +            BEFORE_UPDATE_EVENT.setTarget(null);
  +        }
   
  -            if (shouldRemoveFromDeleted)
  -            {
  -                deletedDuringTransaction.remove(oid);
  -            }
  +        try
  +        {
  +            nowStoring.add(obj);
  +            storeToDb(obj, cld, oid, insert);
  +        }
  +        finally
  +        {
  +            // to optimize calls to DB don't remove already stored objects
  +            nowStoring.remove(obj);
  +        }
  +
  +
  +        // Invoke events on PersistenceBrokerAware instances and listeners
  +        if (insert)
  +        {
  +            AFTER_STORE_EVENT.setTarget(obj);
  +            fireBrokerEvent(AFTER_STORE_EVENT);
  +            AFTER_STORE_EVENT.setTarget(null);
           }
  -        // if Object == null do nothing
           else
           {
  -            return;
  +            AFTER_UPDATE_EVENT.setTarget(obj);
  +            fireBrokerEvent(AFTER_UPDATE_EVENT);
  +            AFTER_UPDATE_EVENT.setTarget(null);
           }
  +        // end of store operation
  +        //************************************************
  +
  +        // if the object was stored, remove it from deleted set
  +        if(deletedDuringTransaction.size() > 0) deletedDuringTransaction.remove(oid);
       }
   
       /**
  @@ -1715,13 +1802,13 @@
        */
       public void store(Object obj, ObjectModification mod) throws PersistenceBrokerException
       {
  -        obj = ProxyHelper.getRealObjectIfMaterialized(obj);
  -        if (obj == null)    // null for unmaterialized Proxy
  +        obj = extractObjectToStore(obj);
  +        // null for unmaterialized Proxy
  +        if (obj == null)
           {
  -            if(logger.isDebugEnabled())
  -                logger.debug("No materialized object could be found -> nothing to store");
               return;
           }
  +
           ClassDescriptor cld = getClassDescriptor(obj.getClass());
           // this call ensures that all autoincremented primary key attributes are filled
           Identity oid = serviceIdentity().buildIdentity(cld, obj);
  @@ -1778,72 +1865,7 @@
           return true;
       }
   
  -    /**
  -     * makes object obj persistent in the underlying persistence system.
  -     * E.G. by INSERT INTO ... or UPDATE ...  in an RDBMS.
  -     * The ModificationState parameter can be used to generate optimized SQL code.
  -     * This functionality is typically called from transaction managers, that
  -     * track which objects have to be stored. Thus this store method does not
  -     * use update cascading to referenced objects.
  -     */
  -    private void store(Object obj, Identity oid, ClassDescriptor cld, boolean insert)
  -    {
  -        if(!isInTransaction())
  -        {
  -            try
  -            {
  -                throw new Exception("No running tx.");
  -            }
  -            catch (Exception e)
  -            {
  -                logger.warn("No running tx found on store operation, please only store
in context of an PB-transaction" +
  -                    ", to avoid side-effects - e.g. when rollback of complex objects",
e);
  -            }
  -        }
  -        // Invoke events on PersistenceBrokerAware instances and listeners
  -        if (insert)
  -        {
  -            BEFORE_STORE_EVENT.setTarget(obj);
  -            fireBrokerEvent(BEFORE_STORE_EVENT);
  -            BEFORE_STORE_EVENT.setTarget(null);
  -        }
  -        else
  -        {
  -            BEFORE_UPDATE_EVENT.setTarget(obj);
  -            fireBrokerEvent(BEFORE_UPDATE_EVENT);
  -            BEFORE_UPDATE_EVENT.setTarget(null);
  -        }
   
  -        if (obj != null)
  -        {
  -            nowStoring.add(obj);
  -            try
  -            {
  -                storeToDb(obj, cld, oid, insert);
  -            }
  -            finally
  -            {
  -                nowStoring.remove(obj);
  -            }
  -        }
  -        else
  -        {
  -            return;
  -        }
  -        // Invoke events on PersistenceBrokerAware instances and listeners
  -        if (insert)
  -        {
  -            AFTER_STORE_EVENT.setTarget(obj);
  -            fireBrokerEvent(AFTER_STORE_EVENT);
  -            AFTER_STORE_EVENT.setTarget(null);
  -        }
  -        else
  -        {
  -            AFTER_UPDATE_EVENT.setTarget(obj);
  -            fireBrokerEvent(AFTER_UPDATE_EVENT);
  -            AFTER_UPDATE_EVENT.setTarget(null);
  -        }
  -    }
   
       /**
        * I pulled this out of store so that when doing multiple table inheritance, i
  
  
  
  1.8       +2 -1      db-ojb/src/java/org/apache/ojb/broker/core/PoolablePersistenceBroker.java
  
  Index: PoolablePersistenceBroker.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/PoolablePersistenceBroker.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- PoolablePersistenceBroker.java	15 Nov 2004 17:27:53 -0000	1.7
  +++ PoolablePersistenceBroker.java	18 Dec 2004 13:39:25 -0000	1.8
  @@ -55,6 +55,7 @@
   
       public void destroy()
       {
  +        super.destroy();
           this.setDelegate(null);
       }
   }
  
  
  
  1.5       +8 -1      db-ojb/src/java/org/apache/ojb/broker/PersistenceBrokerInternal.java
  
  Index: PersistenceBrokerInternal.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/PersistenceBrokerInternal.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- PersistenceBrokerInternal.java	5 Dec 2004 17:47:21 -0000	1.4
  +++ PersistenceBrokerInternal.java	18 Dec 2004 13:39:26 -0000	1.5
  @@ -82,4 +82,11 @@
        * @param maxNum The maximum number of values 
        */
       public void setSqlInLimit(int maxNum);
  +
  +    /**
  +     * Release all resources used by this
  +     * class - CAUTION: No further operations can be
  +     * done with this instance after calling this method.
  +     */
  +    public void destroy();
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message