Reputation: 2284
I have an old project where I used JDO to fetch the data from Datastore. I have the following code
public List<User> findAll() {
LOGGER.info("Getting users list");
Query query = null;
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
query = pm.newQuery(User.class);
List<User> user = (List<User>) query.execute();
return user;
} catch (Exception e) {
LOGGER.error("Error while getting user list", e);
return Collections.emptyList();
} finally {
if(query != null) query.closeAll();
pm.close();
}
}
I am calling the above method from my service and when I try to process the result, I am getting an error as
javax.jdo.JDOUserException: Query result has been closed
I tried to copy the result set to new list and return it but it also didn't work.
List<User> user = (List<User>) query.execute();
return new ArrayList<>(user);
How can I solve the issue? Shouldn't we have to close the query and pm ?
Upvotes: 1
Views: 64
Reputation: 1
I had a similar problem in a old project. The problem is that when you return the List of User, that List is linked to the query execute, and in the finally you close the query, so you can try return a new List, which will not link to query
public List<User> findAll() {
LOGGER.info("Getting users list");
Query query = null;
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
query = pm.newQuery(User.class);
List<User> users = new LinkedList<>((List<User>) query.execute());
return users;
} catch (Exception e) {
LOGGER.error("Error while getting user list", e);
return Collections.emptyList();
} finally {
if(query != null) query.closeAll();
pm.close();
}
}
Upvotes: 0
Reputation: 425
You can test this code :
public List<User> findAll() {
LOGGER.info("Getting users list");
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query = pm.newQuery(User.class);
try {
return (List<User>) query.execute();
} catch (Exception e) {
LOGGER.error("Error while getting user list", e);
return Collections.emptyList();
} finally {
query.closeAll();
}
}
Upvotes: 0