"Object Manager has been closed"Exception using Datanucleus

Posted on by Charalampos Chrysikopoulos

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

[cc]
size()
[/cc]

method on the collection. The result was following exception:
[cc]

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)
....
[/cc]

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 detachedgetPersistenceManager().detachCopyAll(results);
 
return results;}
This entry was posted in datanucleus, jdo and tagged by Charalampos Chrysikopoulos