You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by cg...@agirc-arrco.fr on 2003/12/02 13:21:02 UTC

PB API: DELETE object -> OptimisticLockException

Hi all,

we use optimistic locking using timestamp.
is there a best practice to not get the OptimisticLockException
(returned by the method JdbcAccessImpl.executeDelete(ClassDescriptor cld,
Object obj) )
when we try to delete a non-existent object in a database ?
Obviously, we want to keep the locking='true' in the cld.

Thanks!



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


Re: PB API: DELETE object -> OptimisticLockException

Posted by Armin Waibel <ar...@code-au-lait.de>.
Hi,

cgargan@agirc-arrco.fr wrote:

> Hi all,
> 
> we use optimistic locking using timestamp.
> is there a best practice to not get the OptimisticLockException
> (returned by the method JdbcAccessImpl.executeDelete(ClassDescriptor cld,
> Object obj) )
> when we try to delete a non-existent object in a database ?
What do you expect instead? Another exception e.g. 
PersistenceBrokerException say that the object does not exists,
or should the method return without any exception thrown (e.g. only
a log info message)?

Below you can find a workaround for your problem by adding a new method 
to JdbcAccessImpl class. When the statement does not match, check before 
throw an OptimisticLockingException if object does exist. But I don't 
know that this will be the best way to fix your problem.

regards,
Armin

### change in excute delete (line ~132)

             if (stmt.executeUpdate() == 0 && cld.isLocking()) //BRJ
             {
                 if(checkExistence(cld, obj))
                 {
                     throw new OptimisticLockException("Object has been 
modified by someone else", obj);
                 }
                 else
                 {
                     logger.info("Seems Object "+obj+" does not exist, 
can't delete.");
                 }
             }

### add method
private boolean checkExistence(ClassDescriptor cld, Object obj) throws 
PersistenceBrokerException
     {
         boolean result = false;
         PreparedStatement stmt = null;
         ResultSet rs = null;
         try
         {
             stmt = 
broker.serviceStatementManager().getSelectByPKStatement(cld);
             broker.serviceStatementManager().bindSelect(stmt, new 
Identity(obj, broker), cld);
             rs = stmt.executeQuery();
             if(rs.next()) result = true;
         }
         catch (SQLException e)
         {
             throw new PersistenceBrokerSQLException("Can't check 
existence of object: " + obj, e);
         }
         finally
         {
             if(stmt != null)
             {
                 broker.serviceStatementManager().closeResources(stmt, rs);
             }
         }
         return result;
     }

> Obviously, we want to keep the locking='true' in the cld.
> 
> Thanks!
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
> 
> 



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