You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2004/12/12 02:49:43 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker PersistenceBrokerSQLException.java

arminw      2004/12/11 17:49:43

  Modified:    src/java/org/apache/ojb/broker/core Tag: OJB_1_0_RELEASE
                        PersistenceBrokerImpl.java
               src/java/org/apache/ojb/broker Tag: OJB_1_0_RELEASE
                        PersistenceBrokerSQLException.java
  Log:
  minor refactoring
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.83.2.9  +146 -132  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.83.2.8
  retrieving revision 1.83.2.9
  diff -u -r1.83.2.8 -r1.83.2.9
  --- PersistenceBrokerImpl.java	26 Nov 2004 19:32:06 -0000	1.83.2.8
  +++ PersistenceBrokerImpl.java	12 Dec 2004 01:49:43 -0000	1.83.2.9
  @@ -69,6 +69,7 @@
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   import org.apache.ojb.broker.util.sequence.SequenceManager;
   import org.apache.ojb.broker.util.sequence.SequenceManagerFactory;
  +import org.apache.commons.lang.ObjectUtils;
   
   /**
    * The PersistenceBrokerImpl is an implementation of the PersistenceBroker
  @@ -443,23 +444,27 @@
       {
           if(!isInTransaction())
           {
  -            logger.warn("No running tx found, please only delete objects in context of an PB-transaction" +
  -                    ", to avoid side-effects - e.g. when rollback of complex objects");
  -            /*
  -            arminw:
  -            this could help user to find missing tx declaration
  -            */
  +            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))
               {
                   try
                   {
  -                    throw new Exception("** Try to delete object without active PersistenceBroker transaction **");
  +                    /*
  +                    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)
                   {
  -                    e.printStackTrace();
  +                    logger.info(msg, e);
                   }
               }
  +            else
  +            {
  +                logger.warn(msg + " Enable log-level INFO to get more detailed message (stack trace).");
  +            }
           }
           try
           {
  @@ -680,68 +685,148 @@
       }
   
       /**
  -     * 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) 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 = new Identity(obj, this, cld);
  +        /*
  +        if PK values are set, lookup cache or db to see whether object
  +        needs insert or update
  +        */
  +        if (!insert)
  +        {
  +            insert = objectCache.lookup(oid) == null
  +                && !serviceBrokerHelper().doesExist(cld, oid, obj);
  +        }
  +        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 (obj != null && !nowStoring.contains(obj))
  +        if(result != null)
           {
  -            // ProxyObjects only have to be updated if their real subjects have been loaded
  -            obj = ProxyHelper.getRealObjectIfMaterialized(obj);
  -            if (obj == null)    // null for unmaterialized Proxy
  +            // 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
                   {
  -                    doInsert = objectCache.lookup(oid) == null
  -                        && !serviceBrokerHelper().doesExist(cld, oid, obj);
  +                    throw new Exception("** Try to store object without active PersistenceBroker transaction **");
  +                }
  +                catch(Exception e)
  +                {
  +                    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);
  +        }
   
  -            // let the connection manager to execute batch
  -            connectionManager.executeBatchIfNecessary();
  +
  +        // 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);
  +
  +        // let the connection manager to execute batch
  +        connectionManager.executeBatchIfNecessary();
       }
   
       /**
  @@ -779,7 +864,8 @@
        * @param rds {@link ObjectReferenceDescriptor} of the real object
        * @param insert flag for insert operation
        */
  -    private void storeAndLinkOneToOne(boolean onlyLink, Object obj, ClassDescriptor cld, ObjectReferenceDescriptor rds, boolean insert)
  +    private void storeAndLinkOneToOne(boolean onlyLink, Object obj, ClassDescriptor cld,
  +                                      ObjectReferenceDescriptor rds, boolean insert)
       {
           Object ref = rds.getPersistentField().get(obj);
           if (!onlyLink && rds.getCascadingStore() == ObjectReferenceDescriptor.CASCADE_OBJECT)
  @@ -848,7 +934,8 @@
        * @param referencedObjects the referenced objects ({@link ManageableCollection} or Collection or Array) or null
        * @param insert flag for insert operation
        */
  -    private void storeAndLinkMtoN(boolean onlyLink, Object obj, CollectionDescriptor cod, Object referencedObjects, boolean insert)
  +    private void storeAndLinkMtoN(boolean onlyLink, Object obj, CollectionDescriptor cod,
  +                                  Object referencedObjects, boolean insert)
       {
           /*
           - if the collection is a collectionproxy and it's not already loaded
  @@ -919,7 +1006,8 @@
        * @param referencedObjects the referenced objects ({@link ManageableCollection} or Collection or Array) or null
        * @param insert flag for insert operation
        */
  -    private void storeAndLinkOneToMany(boolean linkOnly, Object obj, CollectionDescriptor cod, Object referencedObjects, boolean insert)
  +    private void storeAndLinkOneToMany(boolean linkOnly, Object obj, CollectionDescriptor cod,
  +                                       Object referencedObjects, boolean insert)
       {
           if(referencedObjects == null)
           {
  @@ -1514,13 +1602,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);
  @@ -1545,83 +1633,9 @@
       }
   
       /**
  -     * 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())
  -        {
  -            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");
  -            /*
  -            arminw:
  -            this could help user to find missing tx declaration
  -            */
  -            if(logger.isEnabledFor(Logger.INFO))
  -            {
  -                try
  -                {
  -                    throw new Exception("** Try to store object without active PersistenceBroker transaction **");
  -                }
  -                catch(Exception e)
  -                {
  -                    e.printStackTrace();
  -                }
  -            }
  -        }
  -        // 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
  -     * can recurse this function
  +     * I pulled this out of internal store so that when doing multiple table
  +     * inheritance, i can recurse this function.
  +     *
        * @param obj
        * @param cld
        * @param oid   BRJ: what is it good for ???
  
  
  
  No                   revision
  No                   revision
  1.8.2.1   +12 -4     db-ojb/src/java/org/apache/ojb/broker/PersistenceBrokerSQLException.java
  
  Index: PersistenceBrokerSQLException.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/PersistenceBrokerSQLException.java,v
  retrieving revision 1.8
  retrieving revision 1.8.2.1
  diff -u -r1.8 -r1.8.2.1
  --- PersistenceBrokerSQLException.java	4 Apr 2004 23:53:30 -0000	1.8
  +++ PersistenceBrokerSQLException.java	12 Dec 2004 01:49:43 -0000	1.8.2.1
  @@ -25,7 +25,7 @@
    */ 
   public class PersistenceBrokerSQLException extends PersistenceBrokerException
   {
  -	private String m_sqlState = null;
  +	private String SQLState = null;
   	
       public PersistenceBrokerSQLException()
       {
  @@ -35,7 +35,7 @@
       public PersistenceBrokerSQLException(SQLException t)
       {
           super(t);
  -        m_sqlState = t.getSQLState();
  +        SQLState = t.getSQLState();
       }
       
       public PersistenceBrokerSQLException(String message)
  @@ -46,6 +46,14 @@
       public PersistenceBrokerSQLException(String message, SQLException t)
       {
           super(message,t);
  -        m_sqlState = t.getSQLState();
  +        SQLState = t.getSQLState();
  +    }
  +
  +    /**
  +     * Returns the SQLState of the underlying {@link java.sql.SQLException}.
  +     */
  +    public String getSQLState()
  +    {
  +        return SQLState;
       }
   }
  
  
  

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