While learning to use JDO with datanucleus for a project to be run in app engine I tried to implement a method that gets a collection of persistant objects from the database. Trying to test the results I used the size() method on the collection. The result was following exception:

org.datanucleus.exceptions.NucleusUserException: Object Manager has been closed
at org.datanucleus.ObjectManagerImpl.assertIsOpen(ObjectManagerImpl.java:3816)
at org.datanucleus.ObjectManagerImpl.findObjectUsingAID(ObjectManagerImpl.java:2073)
....

The solution is simple. To iterate throw the objects of the collection, they have to be in a detached state, not in persistant state.

So the code in the dao class would look like:



public Collection getAllValueObjects() {
Collection results = Collections.checkedCollection(getJdoTemplate().find(ValueObject.class), ValueObject.class);

// To iterate the collection it has to be detached
getPersistenceManager().detachCopyAll(results);

return results;
}

0 comments: