88 Creating a Session

This chapter explains how to create TopLink sessions.

This chapter includes the following sections:

For information on the various types of session available, see Section 87.1, "Session Types".

88.1 Introduction to the Session Creation

Each TopLink session is contained within a sessions configuration (sessions.xml) file. You can create a sessions configuration using Oracle JDeveloper TopLink Editor, TopLink Workbench, or Java code. Oracle recommends that you use Oracle JDeveloper to create and manage your sessions (see Section 88.2, "Creating a Sessions Configuration").

Alternatively, you can create sessions in Java. For more information on creating sessions in Java, see Oracle Fusion Middleware Java API Reference for Oracle TopLink.

After you create a session, you must configure its various options (see Chapter 89, "Configuring a Session"). After configuring the session, you can use it in your application to manage persistence (see Chapter 90, "Acquiring and Using Sessions at Run Time").

88.2 Creating a Sessions Configuration

Oracle JDeveloper TopLink Editor and TopLink Workbench let you create session instances and save them in the sessions.xml file. These tools represent the sessions.xml file as a sessions configuration. Individual session instances are contained within the sessions configuration. You can create multiple sessions configurations, each corresponding to its own uniquely named sessions.xml file.

Oracle recommends that you use Oracle JDeveloper to create and manage sessions. It is the most efficient and flexible approach to session management. For more information about the advantages of this approach, see Section 87.2.2, "Session Configuration and the sessions.xml File".

TopLink Workbench displays sessions configurations and their contents in the Navigator window. When you select a session configuration, its attributes are displayed in the Editor window.

Figure 88-1 calls out the following user interface elements:

  1. Sessions Configuration

  2. Database Session

  3. Relational Server Session

  4. Connection Pool

  5. EIS Server Session

  6. XML Session

  7. Session Broker

Figure 88-1 Sessions Configurations in Navigator Window

Description of Figure 88-1 follows
Description of "Figure 88-1 Sessions Configurations in Navigator Window"

88.2.1 How to Create a Sessions Configuration Using TopLink Workbench

To create a TopLink sessions configuration (sessions.xml file), use this procedure:

  1. New Project button
    Click New on the toolbar and select Sessions Configuration.

    New Sessions Configuration button
    You can also create a new sessions configuration by selecting File > New > Session Configuration from the menu, or by clicking Create New Sessions Configuration in the standard toolbar.

  2. The new sessions configuration element appears in the Navigator window; the Sessions Configuration property sheet appears in the Editor window.

    Enter data in each field on the Sessions Configuration property sheet as Section 88.3, "Configuring a Sessions Configuration" describes.

88.3 Configuring a Sessions Configuration

Each TopLink sessions configuration (sessions.xml file) can contain multiple sessions and session brokers. In addition, you can specify a classpath for each sessions configuration that applies to all the sessions it contains.

88.3.1 How to Configure a Sessions Configuration Using TopLink Workbench

To configure a session configuration, use this procedure:

  1. Select the session configuration in the Navigator. Its properties appear in the Editor.

    Figure 88-2 Sessions Configuration Property Sheet

    Description of Figure 88-2 follows
    Description of "Figure 88-2 Sessions Configuration Property Sheet"

Use the following information to enter data in each field of the Sessions configuration property sheet:

Field Description
Project Save Location Click Change and select the directory in which to save the sessions configuration.
Classpath Lists the JAR or ZIP files that contain the compiled Java classes on which this sessions configuration depends for features that require an external Java class (for example, session event listeners).
  • To add a JAR or ZIP file, click Add Entries or Browse add the file.

  • To remove a JAR or ZIP file, select the file and click Remove.

  • To change the order in which TopLink searches these JAR or ZIP files, select a file and click Up to move it forward or click Down to move it back in the list.

Sessions for <sessions configuration name> Lists the available sessions defined in this sessions configuration:
  • To add a session, click Add Session.

  • To remove a session, select the session and click Remove.

  • To rename a session, select the session and click Rename.

For more information on creating sessions using TopLink Workbench, seen the following:


88.4 Creating a Server Session

Oracle recommends that you create server sessions using Oracle JDeveloper or TopLink Workbench (see Section 88.4.1, "How to Create a Server Session Using TopLink Workbench").

After you create a server session, you create a client session by acquiring it from the server session (see Section 90.4, "Acquiring a Client Session").

88.4.1 How to Create a Server Session Using TopLink Workbench

Before you create a server session, you must first create a sessions configuration (see Section 88.2, "Creating a Sessions Configuration").

To create a new TopLink server session, use this procedure:

  1. Select the sessions configuration in the Navigator window in which you want to create a session.

  2. Add Session button
    Click Add Session on the toolbar. The Create New Session dialog box appears.

    You can also create a new server session by right-clicking the sessions configuration in the Navigator and selecting New > Session from the context menu, or by clicking Add Session on the Sessions Configuration property sheet.

    Figure 88-3 Create New Session Dialog Box, Server Session Option

    Description of Figure 88-3 follows
    Description of "Figure 88-3 Create New Session Dialog Box, Server Session Option"

Use the following information to enter data in each field of the dialog box:

Field Description
Name Specify the name of the new session.
Use Server Platform Check this field if you intend to deploy your application to a Java EE application server.

If you check this field, you must configure the target application server by selecting a Platform.

    Platform This option is only available if you check Use Server Platform.

Select the Java EE application server to which you will deploy your application.

TopLink supports the following Java EE application servers:

  • WebLogic 10.n

  • WebLogic 9.n

  • OC4J 11.1.1

  • OC4J 10.1.3.n

  • SunAS 9.0

  • WebSphere 6.1

  • JBoss

  • Custom

The server platform you select is the default server platform for all sessions you create in this sessions configuration. At the session level, you can override this selection or specify a custom server platform class (see Section 89.9, "Configuring the Server Platform").

Select Data Source Select the data source for this session configuration. Each session configuration must contain one data source. Choose one of the following:
  • Database to create a session for a relational project.

  • EIS to create a session for an EIS project.

  • XML to create a session for an XML projectFoot 1 .

See Section 15.1, "TopLink Project Types" for more information.

Select Session Select Server Session to create a session for a single data source (including shared object cache and connection pools) for multiple clients in a three-tier application.

Footnote 1 You cannot create a server session for an XML project.

88.4.2 How to Create a Server Session Using Java

You can create a server session in Java code using a project. You can create a project in Java code, or read a project from a project.xml file.

Example 88-1 illustrates creating an instance (called serverSession) of a Server class using a Project class.

Example 88-1 Creating a Server Session from a Project Class

Project myProject = new Project();
Server serverSession = myProject.createServerSession();

Example 88-2 illustrates creating an instance (called serverSession) of a Server class using a Project read in from a project.xml file.

Example 88-2 Creating a Server Session from a project.xml File Project

Project myProject = XMLProjectReader.read("myproject.xml");
Server serverSession = myProject.createServerSession();

Example 88-3 illustrates creating a server session with a specified write connection pool minimum and maximum size (when using TopLink internal connection pooling). The default write connection pool minimum size is 5 and maximum size is 10.

Example 88-3 Creating a Server Session with Custom Write Connection Pool Size

XMLProjectReader.read("myproject.xml");
Server serverSession = myProject.createServerSession(32, 32);

88.5 Creating Session Broker and Client Sessions

A session broker may contain both server sessions and database sessions. Oracle recommends that you use the session broker with server sessions because server sessions are the most scalable session type.

Oracle recommends that you create server sessions using Oracle JDeveloper or TopLink Workbench (see Section 88.5.1, "How to Create a Session Broker and Client Sessions Using TopLink Workbench").

After you create and configure a session broker with server sessions, you can acquire a client session from the session broker at run time to provide a dedicated connection to all the data sources managed by the session broker for each client. For more information, see Section 90.4, "Acquiring a Client Session".

88.5.1 How to Create a Session Broker and Client Sessions Using TopLink Workbench

Before you create a session broker session, you must first create a sessions configuration (see Section 88.2, "Creating a Sessions Configuration") and one or more server sessions (Section 88.4, "Creating a Server Session"), or one or more database sessions (Section 88.6, "Creating Database Sessions").

To create a new TopLink session broker, use this procedure:

  1. Select the sessions configuration in the Navigator window in which you want to create a session broker.

  2. Add Session Broker button
    Click Add Session Broker on the toolbar. The Create New Session Broker dialog box appears.

    You can also create a new session broker by right-clicking the sessions configuration in the Navigator window and selecting Add Session Broker from the context menu or by clicking Add Session Broker on the Sessions Configuration property sheet.

    Figure 88-4 Create New Session Broker Dialog Box

    Description of Figure 88-4 follows
    Description of "Figure 88-4 Create New Session Broker Dialog Box"

Use the following information to enter data in each field of the dialog box:

Field Description
Name Specify the name of the new session broker.
Use Server Platform Check this field if you intend to deploy your application to a Java EE application server.

If you check this field, you must configure the target application server by selecting a Platform.

Platform This option is available only if you check Use Server Platform.

Select the Java EE application server to which you will deploy your application.

TopLink supports the following Java EE application servers:

  • WebLogic 10.n

  • WebLogic 9.n

  • OC4J 11.1.1

  • OC4J 10.1.3.n

  • SunAS 9.0

  • WebSphere 6.1

  • JBoss

  • Custom

The server platform you select is the default server platform for all sessions you create in this sessions configuration. At the session level, you can override this selection or specify a custom server platform class (see Section 89.9, "Configuring the Server Platform").

Select the sessions to Manage Select sessions to be managed by this new session broker (from the list of available sessions) and click OK.

Note: This field appears only if the configuration contains valid sessions.


Continue with Chapter 89, "Configuring a Session".

88.5.2 How to Create a Session Broker and Client Sessions Using Java

Example 88-4 illustrates how you can create a session broker in Java code by instantiating a SessionBroker and registering the brokered sessions with it.

Because the session broker references other sessions, configure these sessions before instantiating the session broker. Add all required descriptors to the session, but do not initialize the descriptors or log the sessions. The session broker manages these issues when you instantiate it.

Example 88-4 Creating a Session Broker

Project databaseProject = new MyDatabaseProject();
Server databaseSession = databaseProject.createServerSession();

Project eisProject = new MyEISProject();
Server eisSession = eisProject.createServerSession();

SessionBroker sessionBroker = new SessionBroker();
sessionBroker.registerSession("myDatabase", databaseSession);
sessionBroker.registerSession("myEIS", eisSession);

sessionBroker.login();

88.6 Creating Database Sessions

Oracle recommends that you create database sessions using Oracle JDeveloper or TopLink Workbench (see Section 88.6.1, "How to Create Database Sessions Using TopLink Workbench").

After you create a database session, you can acquire and use it at run time. For more information on acquiring a database session, see Section 90.3, "Acquiring a Session from the Session Manager".

88.6.1 How to Create Database Sessions Using TopLink Workbench

Before you create a database session, you must first create a sessions configuration (see Section 88.2, "Creating a Sessions Configuration").

To create a new TopLink database session, use this procedure:

  1. Select the session configuration in the Navigator window in which you want to create a session.

  2. Add Session button.
    Click Add Session on the toolbar. The Create New Session dialog box appears.

    You can also create a new configuration by right-clicking the sessions configuration in the Navigator window and selecting New > Session from the context menu.

    Figure 88-5 Create New Session Dialog Box, Database Session Option

    Description of Figure 88-5 follows
    Description of "Figure 88-5 Create New Session Dialog Box, Database Session Option"

Use the following information to enter data in each field of the dialog box:

Field Description
Name Specify the name of the new session.
Use Server Platform Check this field if you intend to deploy your application to a Java EE application server.

If you check this field, you must configure the target application server by selecting a Platform.

Platform This option is available only if you check Use Server Platform.

Select the Java EE application server to which you will deploy your application.

TopLink supports the following Java EE application servers:

  • WebLogic 10.n

  • WebLogic 9.n

  • OC4J 11.1.1

  • OC4J 10.1.3.n

  • SunAS 9.0

  • WebSphere 6.1

  • JBoss

  • Custom

The server platform you select is the default server platform for all sessions you create in this sessions configuration. At the session level, you can override this selection or specify a custom server platform class (see Section 89.9, "Configuring the Server Platform").

Select Data Source Select the data source for this session configuration. Each session configuration must contain one data source. Choose one of the following:
  • Database to create a session for a relational project.

  • EIS to create a session for an EIS project.

  • XML to create a session for an XML project.

See Section 15.1, "TopLink Project Types" for more information.

Select Session Select Database Session to create a session for a single database (including shared object cache and connection pools) for a single client suitable for simple applications or prototyping.

Enter the necessary information and click OK.

TopLink Workbench window appears, showing the database session in the Navigator window.

Continue with Chapter 89, "Configuring a Session".

88.6.2 How to Create Database Sessions Using Java

You can create an instance of the DatabaseSession class in Java code using a Project. You can create a project in Java code or read a project from a project.xml file.

Example 88-5 illustrates creating a DatabaseSession using a Project class.

Example 88-5 Creating a Database Session from a Project Class

Project myProject = new Project();
DatabaseSession databaseSession = myProject.createDatabaseSession();

Example 88-6 illustrates creating a DatabaseSession using a Project read in from a project.xml file.

Example 88-6 Creating a Database Session from a project.xml File Project

Project myProject = XMLProjectReader.read("myproject.xml");
DatabaseSession databaseSession = myProject.createDatabaseSession();

88.7 Creating Remote Sessions

Remote sessions are acquired through a remote connection to their server-side session. Remote sessions are acquired through Java code on the remote client. The server-side session must also be registered with an oracle.toplink.remote.ejb.RemoteSessionController and accessible from the RMI naming service.

You create remote sessions entirely in Java code (see Section 88.7.1, "How to Create Remote Sessions Using Java").

88.7.1 How to Create Remote Sessions Using Java

Example 88-7 and Example 88-8 demonstrate how to create a remote TopLink session on a client that communicates with a remote session controller on a server that uses RMI. After creating the connection, the client application uses the remote session as it does with any other TopLink session.

88.7.1.1 Server

Example 88-7 shows the code you add to your application's RMI service (MyRMIServerManagerImpl) to create and return an instance of an RMIRemoteSessionController to the client. The controller sits between the remote client and the local TopLink session.

The RMIRemoteSessionController you create on the server is based on a TopLink server session. You create and configure this server session as described in Section 88.4, "Creating a Server Session" and Section 91, "Configuring Server Sessions".

Example 88-7 Server Creating RMIRemoteSessionController for Client

RMIRemoteSessionController controller = null;
try {
    // Create instance of RMIRemoteSessionControllerDispatcher which implements
    // RMIRemoteSessionController. The constructor takes a TopLink session as a parameter
    controller = new RMIRemoteSessionControllerDispatcher (localTopLinkSession);
} 
catch (RemoteException exception) {
    System.out.println("Error in invocation " + exception.toString());
}
return controller;

88.7.1.2 Client

The client-side code gets a reference to the application's RMI service (in this example it is called MyRMIServerManager) and uses this code to get the RMIRemoteSessionController running on the server. The reference to the session controller is then used to create the RMIConnection from which it acquires a remote session.

Example 88-8 Client Acquiring RMIRemoteSessionController from Server

MyRMIServerManager serverManager = null;
// Set the client security manager
try {
    System.setSecurityManager(new MyRMISecurityManager());
} 
catch(Exception exception) {
    System.out.println("Security violation " + exception.toString());
}
// Get the remote factory object from the Registry
try {
    serverManager = (MyRMIServerManager) Naming.lookup("SERVER-MANAGER");
} 
catch (Exception exception) {
    System.out.println("Lookup failed " + exception.toString());
}
// Start RMIRemoteSession on the server and create an RMIConnection
RMIConnection rmiConnection = null;
try {
    rmiConnection = new RMIConnection(
        serverManager.createRemoteSessionController()
    );
} 
catch (RemoteException exception) {
    System.out.println("Error in invocation " + exception.toString());
}
// Create a remote session which we can use as a normal TopLink session
Session session = rmiConnection.createRemoteSession();