Accessing originally loaded object during a Event Listener?


ebenzacar@...
 

I'm using DN 5.2 with JDO & MSSQL RDBMS.

During a preStore event listener, how can I get access to the original state of the object when it was loaded?  I found the following field `org.datanucleus.state.StateManagerImpl#savedImage` but it seems to be null.
According to the javadocs I would have expected that the object be saved as soon as it is loaded from the DB ("Image of the Persistable instance when the instance is enlisted in the transaction.").  

Additionally, is there any way to peek at the `savedImage` to retrieve the initial value of given fields during an event?

pm = pmf.getPersistenceManager();
pm.addInstanceLifecycleListener( new StoreLifecycleListener(), null);
tx = pm.currentTransaction();
((JDOTransaction)tx).registerEventListener(audit);
try
{
tx.begin();

Student p = pm.getObjectById(Student.class, 1);
p.setName("Second Student");

tx.commit();
}


In StoreLifecycleListener:

public void preStore(InstanceLifecycleEvent event)
{
Persistable pc = (Persistable)event.getSource();
ObjectProvider op = (ObjectProvider)pc.dnGetStateManager();

// via debug inspection, op.savedImage == null
// similarly, op.restoreFields() does nothing to the pc object
}


Thanks!

Eric


Andy
 
Edited

Don't see how you "loaded" the object in the first place, have you checked "loadedFields"? Maybe it was HOLLOW when the listener method was called, in which case no need to "save" a copy of the PC since that would be inefficient.
StateManagerImpl.saveFields is the method that saves it, so suggest that you get the code and debug when that is called.


ebenzacar@...
 

Thanks Andy.  I did see that, and started looking into it, but wasn't sure exactly what to expect.  I wasn't 100% sure what 
    "Image of the Persistable instance when the instance is enlisted in the transaction."
meant.

After digging through the tx flow a little bit more, I realized that I needed to set the javax.jdo.option.RestoreValues=true option to force DN to save the image prior to making a field dirty.

Is there anyway to access that savedImage object?  I can't find anything in the StateManagerImpl that will give me access to it?

Thanks,

Eric