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 Colin Kilburn <co...@accesstec.ca> on 2005/09/07 22:11:28 UTC

Disabling Optimistic Locking at Runtime

Hello,

I am using optimistic locking for a few classes/tables in my 
application.   However, in certain circumstances when my application 
starts up, I want to disable optimistic locking for the lifetime of the 
application.   I have tried to modify the repository metadata at 
runtime, but I still get an OptimisticLockException when trying to 
update an object whose locking field is not consistent with the 
database.    I've included my disabling method below.  Is there anything 
I can add to it to completely disable this mechanism?

private void disableOptimisticLocking() {
    DescriptorRepository r = 
MetadataManager.getInstance().getGlobalRepository();
    Map dt = r.getDescriptorTable();
        
    Iterator i = dt.entrySet().iterator();
    while (i.hasNext()) {
        Map.Entry entry = (Map.Entry) i.next();
        ClassDescriptor cd = (ClassDescriptor) entry.getValue();

        if (!cd.isInterface() && cd.isLocking()) {
            FieldDescriptor[] lockingFields = cd.getLockingFields();
            for (int j = 0; j < lockingFields.length; j++) {
                FieldDescriptor descriptor = lockingFields[j];
                descriptor.setLocking(false);
                descriptor.setUpdateLock(false);
            }
        }
    }
}

Thanks in advance,
Colin

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


Re: Disabling Optimistic Locking at Runtime

Posted by Colin Kilburn <co...@accesstec.ca>.
Thanks Armin, that the remove/re-add field descriptor seems to work for me.


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


Re: Disabling Optimistic Locking at Runtime

Posted by Armin Waibel <ar...@apache.org>.
Hi Colin,

Colin Kilburn wrote:
> Hello,
> 
> I am using optimistic locking for a few classes/tables in my 
> application.   However, in certain circumstances when my application 
> starts up, I want to disable optimistic locking for the lifetime of the 
> application.   I have tried to modify the repository metadata at 
> runtime, but I still get an OptimisticLockException when trying to 
> update an object whose locking field is not consistent with the 
> database.

The locking fields will be cached (for better performance) in 
ClassDescriptor first time method cld.getLockingFields() was called. In 
#disableOptimisticLocking() you set the locking state after calling this 
method (and the locking state change is currently not reflected in 
ClassDescriptor, e.g. via a state change listener).

A workaround could be to remove and re-add the locking fields. In this 
case the cached locking fields array will be nullified. See below

>    I've included my disabling method below.  Is there anything 
> I can add to it to completely disable this mechanism?
> 
> private void disableOptimisticLocking() {
>    DescriptorRepository r = 
> MetadataManager.getInstance().getGlobalRepository();
>    Map dt = r.getDescriptorTable();
>           Iterator i = dt.entrySet().iterator();
>    while (i.hasNext()) {
>        Map.Entry entry = (Map.Entry) i.next();
>        ClassDescriptor cd = (ClassDescriptor) entry.getValue();
> 
>        if (!cd.isInterface() && cd.isLocking()) {
>            FieldDescriptor[] lockingFields = cd.getLockingFields();
>            for (int j = 0; j < lockingFields.length; j++) {
>                FieldDescriptor descriptor = lockingFields[j];
>                descriptor.setLocking(false);
>                descriptor.setUpdateLock(false);

                // workaround: re-add field-descriptor
                cd.removeFieldDescriptor(descriptor);
                cd.addFieldDescriptor(descriptor);
>            }
>        }
>    }
> }
> 

Never tried this, but if it's done at application start up there 
shouldn't be any side-effects.

regards,
Armin

> Thanks in advance,
> Colin
> 
> ---------------------------------------------------------------------
> 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