Jump to: navigation, search

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

(Session Manager)
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
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.
 
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.
 +
 +
See [[EclipseLink/UserGuide/Using_EclipseLink_Sessions_%28ELUG%29|Using EclipseLink Sessions]] for complete information.
 +
 +
  
 
= Session Manager =
 
= 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.
+
The [[Acquiring_and_Using_Sessions_at_Run_Time_%28ELUG%29#Session_Manager|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.
 +
 
 +
For specific examples, see:
 +
*[[Acquiring_and_Using_Sessions_at_Run_Time_%28ELUG%29#Acquiring_the_Session_Manager| Loading a Session Manager Instance]]
 +
*[[Acquiring_and_Using_Sessions_at_Run_Time_%28ELUG%29#How_to_Load_a_Session_from_sessions.xml_Using_Defaults|Loading a Named Session from Session Manager Using Defaults]]
 +
*[[Acquiring_and_Using_Sessions_at_Run_Time_%28ELUG%29#How_to_Load_a_Session_from_sessions.xml_with_an_Alternative_Class_Loader|Loading a Session with an Alternative Class Loader]]
 +
*[[Acquiring_and_Using_Sessions_at_Run_Time_%28ELUG%29#How_to_Load_a_Session_from_an_Alternative_Session_Configuration_File|Loading an Alternative Session Configuration File]]
 +
 
 +
 
 +
 
 +
= 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
 +
 
 +
 
  
== Loading a Session Manager Instance ==
+
== Create a Session Factory that uses the default sessions.xml file ==
<code><pre>
+
import org.eclipse.persistence.tools.sessionmanagement.SessionManager;
+
SessionManager sessionManager = SessionManager.getManager();
+
</pre></code>
+
  
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.
+
<source lang="java">
 +
SessionFactory sessionFactory = new SessionFactory("session-name");
 +
Session sessionFactory.getSharedSession(); // By default this will log the session in.
 +
</source>
  
== Loading a Named Session from Session Manager Using Defaults ==
 
<code><pre>
 
/* This example loads a named session (mysession) defined in the sessions.xml file. */
 
SessionManager manager = SessionManager.getManager();
 
Server server = (Server) manager.getSession("myserversession");
 
</pre></code>
 
  
== Loading a Session with an Alternative Class Loader ==
+
== Create a Session Factory that loads an alternative XML Configuration file ==
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.
+
The alternative xml file name must be a resource path to the sessions configuration xml.
  
<code><pre>
+
<source lang="java">
/* This example uses the specified ClassLoader to load a session (mysession) defined in the sessions.xml file. */
+
SessionFactory sessionFactory = new SessionFactory("app-session.xml", "session-name");
ClassLoader classLoader = YourApplicationClass.getClassLoader();
+
Session session = sessionFactory.getSharedSession(false, true); // Don't log the session in, but refresh it.
SessionManager manager = SessionManager.getManager();
+
</source>
Session session = manager.getSession("mysession", classLoader);
+
</pre></code>
+

Latest revision as of 15:05, 23 June 2008

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.

See Using EclipseLink Sessions for complete information.


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.

For specific examples, see:


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");
Session sessionFactory.getSharedSession(); // By default this will log the session in.


Create a Session Factory that loads an alternative XML Configuration file

The alternative xml file name must be a resource path to the sessions configuration xml.

SessionFactory sessionFactory = new SessionFactory("app-session.xml", "session-name");
Session session = sessionFactory.getSharedSession(false, true); // Don't log the session in, but refresh it.