Search code examples
javagoogle-app-engineexceptionjdo

Unable to use JDO in a GAE background thread


I have created a background thread on a Runnable object in Google App Engine like this:

BackendService s = new BackendService();
thread = ThreadManager.createBackgroundThread(s);
thread.start();

However - when it comes to initialize get the PersistenceManagerFactory , which I do like this:

private static final PersistenceManagerFactory pmfInstance = JDOHelper.getPersistenceManagerFactory("transactional");

I get a classloader exception:

Uncaught exception from servlet
com.google.apphosting.runtime.FatalError: A not-user-defined ClassLoader was set as the thread'scontextClassLoader: sun.misc.Launcher$AppClassLoader@1a8c4e7
at com.google.appengine.runtime.Request.process-f71d5e950ca508ff(Request.java)
at java.security.AccessController.doPrivileged(AccessController.java:34)

What am I doing wrong?


Solution

  • I had the same issue (using JPA.) The solution that seems to work is to set the class loader in the Runnable manually.

    In calling object:

    private static Thread thread;
    private static ClassLoader cl;
    

    In calling method:

    ...
    cl = getClass().getClassLoader();
    thread = ThreadManager.createBackgroundThread(...);
    thread.start();
    ...
    

    In Runnable run():

    thread.setContextClassLoader(cl);
    ...