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/test/org/apache/ojb/broker PersistenceBrokerTest.java
Date Wed, 25 Feb 2004 00:32:11 GMT
arminw      2004/02/24 16:32:11

  Modified:    src/java/org/apache/ojb/broker/core
                        PersistenceBrokerImpl.java
               src/java/org/apache/ojb/broker PersistenceBroker.java
               src/test/org/apache/ojb/broker PersistenceBrokerTest.java
  Log:
  - no longer throw an exception if PB.abortTransaction was called without
  a running connection. Allows a better exception handling.
  
  - add new test for changed behaviour
  
  - update javadoc in PB interface
  
  - clean-up code
  
  Revision  Changes    Path
  1.69      +21 -19    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.68
  retrieving revision 1.69
  diff -u -r1.68 -r1.69
  --- PersistenceBrokerImpl.java	17 Feb 2004 20:52:30 -0000	1.68
  +++ PersistenceBrokerImpl.java	25 Feb 2004 00:32:11 -0000	1.69
  @@ -391,14 +391,25 @@
        */
       public synchronized void abortTransaction() throws TransactionNotInProgressException
       {
  -        if (!this.connectionManager.isInLocalTransaction())
  -        {
  -            throw new TransactionNotInProgressException("ConnectionManager is NOT in transaction");
  -        }
  +        /*
  +        arminw:
  +        TODO: does it make sense to prevent rollback call??
  +        by default ConnectionManager does an internal rollback when an
  +        SQLException occur, thus this check will fail
  +        */
  +//        if (!this.connectionManager.isInLocalTransaction())
  +//        {
  +//            throw new TransactionNotInProgressException("ConnectionManager is NOT in
transaction");
  +//        }
  +
           fireBrokerEvent(BEFORE_ROLLBACK_EVENT);
           clearRegistrationLists();
           referencesBroker.removePrefetchingListeners();
  -        this.connectionManager.localRollback();
  +        /*
  +        check if we in local tx, before do local rollback
  +        Necessary, because ConnectionManager may do a rollback by itself
  +        */
  +        if(isInTransaction()) this.connectionManager.localRollback();
           fireBrokerEvent(AFTER_ROLLBACK_EVENT);
       }
   
  @@ -1545,26 +1556,17 @@
       /**
        * @see org.apache.ojb.broker.PersistenceBroker#removeFromCache
        */
  -    public void removeFromCache(Object obj) throws PersistenceBrokerException
  +    public void removeFromCache(Object objectOrIdentity) throws PersistenceBrokerException
       {
  -
           Identity identity;
  -        if (obj instanceof Identity)
  +        if (objectOrIdentity instanceof Identity)
           {
  -            identity = (Identity)obj;
  +            identity = (Identity)objectOrIdentity;
           }
           else
           {
  -            identity = new Identity(obj, this);
  +            identity = new Identity(objectOrIdentity, this);
           }
  -        // Object objectToRemove = objectCache.lookup(identity);
  -        /*
  -        objects must also be remove from LoadedObjectsRegistry
  -        Fix by Jamie Burns
  -        arminw:
  -        move LoadedObjectsRegistry to odmg-layer
  -        */
  -        // LoadedObjectsRegistry.remove(objectToRemove);
           objectCache.remove(identity);
       }
   
  
  
  
  1.28      +19 -14    db-ojb/src/java/org/apache/ojb/broker/PersistenceBroker.java
  
  Index: PersistenceBroker.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/PersistenceBroker.java,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- PersistenceBroker.java	16 Jul 2003 11:19:32 -0000	1.27
  +++ PersistenceBroker.java	25 Feb 2004 00:32:11 -0000	1.28
  @@ -231,7 +231,7 @@
        * Calling <code>commit</code> commits to the database all
        * UPDATE, INSERT and DELETE statements called within the transaction and
        * releases any locks held by the transaction.
  -     * If beginTransaction() has not been called before a
  +     * If beginTransaction() has not been called before, a
        * TransactionNotInProgressException exception is thrown.
        * If the transaction cannot be commited a TransactionAbortedException exception is
thrown.
        */
  @@ -240,7 +240,8 @@
   
   
       /**
  -     * Returns true if the broker performs a transaction, false in the other case
  +     * Returns <tt>true</tt> if the broker performs a transaction, <tt>false</tt>
  +     * in the other case.
        */
       public boolean isInTransaction() throws PersistenceBrokerException;
   
  @@ -254,6 +255,9 @@
        */
       public boolean close();
   
  +    /**
  +     * Returns <tt>true</tt> if this instance is closed.
  +     */
       public boolean isClosed();
   
   
  @@ -274,16 +278,16 @@
       public PBKey getPBKey();
   
       /**
  -     * returns a ClassDescriptor for the persistence capable class clazz.
  -     * throws a PersistenceBrokerException if clazz is not persistence capable,
  -     * i.e. if clazz is not defined in the DescriptorRepository.
  +     * Returns a ClassDescriptor for the persistence capable class.
  +     * Throws a PersistenceBrokerException if clazz is not persistence capable,
  +     * i.e. if no metadata was defined for this class or class was not found.
        */
       public ClassDescriptor getClassDescriptor(Class clazz) throws PersistenceBrokerException;
   
       /**
  -     * same as getClassDescriptor, but does not throw. Useful for checking if an object
is
  -     * persistence Capable.
  -     * @param clazz
  +     * Same as {@link #getClassDescriptor}, but does not throw an exception when class
was not found.
  +     * Useful for checking if an object is persistence Capable.
  +     * @param clazz target class
        * @return true if descriptor was found
        */
       public boolean hasClassDescriptor(Class clazz);
  @@ -318,9 +322,9 @@
        * If object is instance of {@link org.apache.ojb.broker.Identity},
        * the associated object was removed from cache.
        * <br/>
  -     * Removing is not recursive.
  +     * Note: Removing is not recursive.
        */
  -    public void removeFromCache(Object obj) throws PersistenceBrokerException;
  +    public void removeFromCache(Object objectOrIdentity) throws PersistenceBrokerException;
   
       /**
        * makes object obj persistent in the underlying persistence system.
  @@ -328,7 +332,6 @@
        * The ObjectModification parameter can be used to generate optimized SQL code
        *
        * (decide whether insert or update is needed. And for updates only generate code for
modified columns)
  -     *
        */
       public void store(Object obj,
                         ObjectModification modification) throws PersistenceBrokerException;
  @@ -340,10 +343,8 @@
       public void store(Object obj) throws PersistenceBrokerException;
   
       /**
  -     *
        * deletes the objects obj concrete representation in the underlying persistence system.
        * E.G. by DELETE FROM ... WHERE ... in an RDBMS
  -     *
        */
       public void delete(Object obj) throws PersistenceBrokerException;
   
  @@ -371,11 +372,15 @@
        */
       public void deleteMtoNImplementor(MtoNImplementor m2nImpl) throws PersistenceBrokerException;
   
  +    /**
  +     *
  +     */
       public void addMtoNImplementor(MtoNImplementor m2nImpl) throws PersistenceBrokerException;
   
       /**
        * Deletes all objects matching the query from the underlying persistence system.
        * E.G. by DELETE FROM ... WHERE ... in an RDBMS
  +     * <b>Note:</b> Currently this method does not remove objects from the
cache too.
        */
       public void deleteByQuery(Query query) throws PersistenceBrokerException;
   
  
  
  
  1.39      +59 -1     db-ojb/src/test/org/apache/ojb/broker/PersistenceBrokerTest.java
  
  Index: PersistenceBrokerTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/PersistenceBrokerTest.java,v
  retrieving revision 1.38
  retrieving revision 1.39
  diff -u -r1.38 -r1.39
  --- PersistenceBrokerTest.java	18 Jan 2004 16:47:35 -0000	1.38
  +++ PersistenceBrokerTest.java	25 Feb 2004 00:32:11 -0000	1.39
  @@ -393,6 +393,64 @@
           }
       }
   
  +    public void testLocalTransactionDemarcation()
  +    {
  +        PersistenceBroker pb = PersistenceBrokerFactory.defaultPersistenceBroker();
  +
  +        try
  +        {
  +            pb.beginTransaction();
  +            pb.commitTransaction();
  +            pb.close();
  +
  +            pb = PersistenceBrokerFactory.defaultPersistenceBroker();
  +            pb.beginTransaction();
  +            pb.abortTransaction();
  +            pb.abortTransaction();
  +            pb.close();
  +
  +            pb = PersistenceBrokerFactory.defaultPersistenceBroker();
  +            pb.beginTransaction();
  +            pb.commitTransaction();
  +            pb.abortTransaction();
  +            pb.close();
  +
  +            pb = PersistenceBrokerFactory.defaultPersistenceBroker();
  +            try
  +            {
  +                pb.commitTransaction();
  +                fail("Commit tx without begin shouldn't be possible");
  +            }
  +            catch(TransactionNotInProgressException e)
  +            {
  +                assertTrue(true);
  +            }
  +
  +            try
  +            {
  +                pb.beginTransaction();
  +                Query q = QueryFactory.newQuery(Article.class, "Select * from NOT_EXIST");
  +                pb.getObjectByQuery(q);
  +                pb.commitTransaction();
  +                fail("Query should fail");
  +            }
  +            catch(PersistenceBrokerException e)
  +            {
  +                assertTrue(true);
  +            }
  +            finally
  +            {
  +                pb.close();
  +            }
  +        }
  +        finally
  +        {
  +            if(pb != null) pb.close();
  +        }
  +
  +
  +    }
  +
       /**
        * test the the PB deleteByQuery() method.
        */
  @@ -424,7 +482,7 @@
           // 1. check for matching items
           broker.clearCache();
           Collection col = broker.getCollectionByQuery(q);
  -        assertTrue("There should be 3 matching items", col.size() == 3);
  +        assertEquals("There should be 3 matching items", 3, col.size());
   
           // 2. perform delete by query
           broker.deleteByQuery(q);
  
  
  

---------------------------------------------------------------------
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