Search code examples
databasearraylistpersistencejdofinally

List becomes empty After query.closeall methodin jdo


public static List<FieldOption> 
getFieldOptionListOfField(PersistenceManager pm, long fieldId) throws NoSuchFieldOptionException { 
    Query query = pm.newQuery(FieldOption.class);

    try {
        query.setFilter("this.fieldId == fieldId");
        query.declareParameters("long fieldId");
        query.setOrdering("orderId ascending");
        List<FieldOption> fieldOptions = (List<FieldOption>) query.execute(fieldId);
        logger.debug("fieldOptions = " + fieldOptions);
        return fieldOptions();
    } finally {
        query.closeAll();
    }
}

After execution of excute method 'fieldOptions' is having certain values. But after closeAll() the list becomes empty. Can you please suggest why it happens?


Solution

  • The "List" returned after you end a transaction and close a PM is not a real List, but instead a lazy load list, that cannot lazy load once it has no connection to the datastore. Easiest option is put (copy) the query results into your own List before closing the txn/PM.