Skip Headers

# Loading a Session From sessions.xml With an Alternative Class Loader

Previous

Next

You can use an alternative class loader to load sessions. This is common when your TopLink application integrates with a J2EE container. The session manager uses the class loader to find and load the `sessions.xml` resource and resolve any classes referenced in the `sessions.xml` and `project.xml` files.

In most cases, you use the class loader from the current thread context, as Example: Loading a Session Using the Current Thread Context Class Loader illustrates. In this example, the session named `mysession` is loaded from the first file in the application classpath named `sessions.xml` using the class loader associated with the current thread context.

Loading a Session Using the Current Thread Context Class Loader

    /* Use the specified ClassLoader to load a session (mysession) defined in the sessions.xml file */
    SessionManager manager = SessionManager.getManager();
    Session session = manager.getSession(
        "mysession",  // session name to load
        Thread.current().getContextClassLoader() // ClassLoader instance to use
    );

However, if your J2EE container does not support using the current thread context class loader, you can use the class loader from the current class, as Example: Loading a Session Using the Current Class's Class Loader illustrates.

Loading a Session Using the Current Class's Class Loader

    /* Use the specified ClassLoader to load a session (mysession) defined in the sessions.xml file */
    SessionManager manager = SessionManager.getManager();
    Session session = manager.getSession(
        "mysession",  // session name to load
        this.getClass().getClassLoader() // ClassLoader instance to use
    );

Note: Oracle Containers for J2EE supports the use of the class loader from the current thread.