Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/JPA/ORMSessions"

(Session Factory)
(Session Factory)
Line 51: Line 51:
 
* Hot/Re-deployment handling of applications
 
* Hot/Re-deployment handling of applications
 
* Detachment helpers to simplify usage within a local session bean
 
* Detachment helpers to simplify usage within a local session bean
 +
 +
== Create a Session Factory that uses the default sessions.xml file ==
 +
 +
<code><pre>
 +
SessionFactory sessionFactory = new SessionFactory("session-name");
 +
</pre></code>
 +
 +
== Create a Session Factory that uses an alternative XML file ==
 +
 +
<code><pre>
 +
SessionFactory sessionFactory = new SessionFactory("app-session.xml", "session-name");
 +
</pre></code>

Revision as of 12:38, 24 October 2007

Sessions are a key component of the EclipseLink Server application— they provide EclipseLink with access to the database. Sessions enable you to execute queries, and they return persistent objects and other results for client applications.

Session Manager

The EclipseLink session manager enables developers to build a series of sessions that are maintained under a single entity. The session manager is a static utility class that loads EclipseLink sessions from the sessions.xml file, caches the sessions by name in memory, and provides a single access point for EclipseLink sessions.

Loading a Session Manager Instance

import org.eclipse.persistence.tools.sessionmanagement.SessionManager;
SessionManager sessionManager = SessionManager.getManager();

EclipseLink uses a class loader to load the session manager. The session manager, in turn, uses that same class loader to load named sessions that are not already initialized in the session manager cache.

Loading a Named Session from Session Manager Using Defaults

/* This example loads a named session (mysession) defined in the sessions.xml file. */
SessionManager manager = SessionManager.getManager();
Server server = (Server) manager.getSession("myserversession");

Loading a Session with an Alternative Class Loader

You can use an alternative class loader to load sessions. This is common when your EclipseLink application integrates with a J2EE container. If the session is not already in the session manager's in-memory cache of sessions, the session manager creates the session and logs in.

/* This example uses the specified ClassLoader to load a session (mysession) defined in the sessions.xml file. */
ClassLoader classLoader = YourApplicationClass.getClassLoader();
SessionManager manager = SessionManager.getManager();
Session session = manager.getSession("mysession", classLoader);

Loading an Alternative Session Configuration File

You can use the XML Session Config Loader to load any XML configuration file on the application class path. This enables you to use files other than the standard sessions.xml file to load sessions. You can use the XML Session Config loader to load different sessions, and even different class loaders, from configuration files. The XMLSessionConfigLoader class defines two constructors:

  • The zero-argument constructor loads the default sessions.xml file.
  • The single argument constructor includes a parameter (a String) that specifies an alternative configuration file.
/* XMLSessionConfigLoader loads the toplink-sessions.xml file */
XMLSessionConfigLoader sessionLoader = new XMLSessionConfigLoader("toplink-sessions.xml");
ClassLoader classLoader = YourApplicationClass.getClassLoader();
SessionManager manager = SessionManager.getManager();
Session session = manager.getSession(sessionLoader, "mysessionname", classLoader);

Session Factory

Alternatively you may use the SessionFactory which is a Helper class to simplify the development and generation of code that accesses EclipseLink through the SessionManager (sessions config XML).

Responsibilities:

  • Lookup of a session by name using default or provided sessions config location
  • Support lookup of active UnitOfWork and Session in JTA environments
  • Hot/Re-deployment handling of applications
  • Detachment helpers to simplify usage within a local session bean

Create a Session Factory that uses the default sessions.xml file

SessionFactory sessionFactory = new SessionFactory("session-name");

Create a Session Factory that uses an alternative XML file

SessionFactory sessionFactory = new SessionFactory("app-session.xml", "session-name");

Back to the top