I think I see where the original bug came from.  Even though you create 
a new Context, the old DSpaceObjects (Item, Bundle etc) instances 
you're manipulating have the old Context cached within them and they 
are updating that - e..g when you call item.update() it's using the old 
context.

The fix is to create a new Item instance that's bound internally to 
your new Context, e.g.

Item nitem = Item.find(ctx, item.getID());
...
nitem.update();

.. and so on..

Since each Context has its own private event list, you *should* have no 
problem doing event-generating operations within a Consumer *so long 
as* they happen on a different Context.   Look very closely at where 
each object's internal Context is coming from, it could be that your 
code is unexpectedly doing an operation on the old Context.

I'd recommend against doing explicit surgery on *any* context's cache, 
except perhaps to remove an entry entirely.  It seems like a really bad 
idea to copy objects from the new Context's cache into the old 
Context's cache; those objects will have an internal Context different 
from their cache's home, which could cause all kinds of mischief.

Using a message queue as Richard suggests (to accumulate these events 
for later processing outside the scope of event consumers) would also 
be a fairly clean (though demanding) solution.  It counts as 
"implementing your own event system".

> As I mentioned earlier, I have now got my consumer working without the 
> earlier errors usiing a new context in the following way:
>
> Context ctx = new Context();
> ctx.setCurrentUser(originalContext.getCurrentUser());
> ctx.setCurrentLocale(originalContext.getCurrentLocale());
> ....
> ....
> // If I have to create a new bundle
> tbundles = new Bundle[]{item.createBundle(THUMBNAIL_BUNDLE)};
>  // Commit changes to datastore
> tbundles[0].update();
> item.update();
> // Add bundle and item objects to the original context
> originalContext.cache(tbundles[0], tbundles[0].getID());
> originalContext.cache(item, item.getID());
>  ....
> ....
> ....
> // commit changes to datastore bypassing any event triggered during use
> ctx.getDBConnection().commit();
>
> // Clear temporary context so it will be garbage collected
> ctx.clearCache();
>
>
> By using the temporary context and NOT calling the 
> ctx.getDBConnection().commit() method I save the changes
> to the database while bypassing any events triggered during its use. I 
> found that I had to add any of the DSpace
>  objects that I used with the temporary context to the origonal 
> context so they would be found by my code
> later on. I also cleared the cache of the temporary so it will be 
> properly garbage collected.
>
> I'll see how long this lasts.
>
> On question. Is there some early info at this stage of how the event 
> system for 2.0 is shaping up to be.
>
> John


-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
DSpace-tech mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/dspace-tech

Reply via email to