HowTo use custom data sources with TopLink

OracleAS TopLink - HowTo use custom data sources with TopLink

Date: 21/04/2004

After completing this HowTo you should be able to:
• Understand how to use custom data sources with TopLink
• Understand how to use sessions.xml in a J2EE application
• Run the sample using the instruction given

Table of Contents

Introduction
Prerequisites
Software Requirements
HowTo use custom data sources with OracleAS TopLink 10g
Useful References

Introduction 

Oracle Application Server TopLink is an advanced object-to-relational persistence. It helps to build high performance applications that store persistent data in a relational database. You can use the OracleAS TopLink Sessions Editor to manage sessions.xml which contains details of how TopLink accesses the database.

Prerequisites 

Before we get started, the following are the assumptions that will be made in this document:
  • Minimum knowledge of OracleAS TopLink Workbench

Software Requirements 

How to use custom data sources OracleAS TopLink 10g

OracleAS TopLink Sessions Editor provides a graphical way to manage sessions.xml file outside the TopLink Workbench.
The sessions.xml file allows you to configure one or more sessions for the TopLink project and associate these sessions with the project. You can configure -

  •   Database login information
  •   JTA/JTS transactions
  •   Cache related properties
  •   sessions encompassing multiple databases (Session Broker)

   Let us consider a simple J2EE application, with one table Employees,two JSP files to search/display records from this table, a TopLink generated class Employees and a utility class EmployeeReader. A snapshot of the TopLink workbench for this project is shown below

Figure 1:TopLink Project for the example

There is no default login specified in this project, since we are going to add that information in our sessions.xml file.
To do this,

1. Open the TopLink Sessions Editor.
2. Create a new Sessions file by selecting File->New.

Figure 2:Creating a new sessions.xml file
 


3.Right click on sessions.xml->New->Session and provide a name for the session, CustomDS. 
Associate this session with the project file,by entering the path to the project file in the 'General' tab in the navigator pane.

Figure 3:Associate session with project

4. This session will be used to access a data source defined in OC4J. So in the navigator pane, enter the name of the data source

Figure 4: Creating a session that uses custom data source

The data source should  defined in a data-sources.xml file, which should be bundled with the application.

<data-sources>
...
<data-source
class="com.evermind.sql.DriverManagerDataSource"
name="CustomDS"
location="jdbc/CustomCoreDS"
xa-location="jdbc/xa/CustomXADS"
ejb-location="jdbc/CustomDS"
connection-driver="oracle.jdbc.driver.OracleDriver"
username="hr"
password="hr"
url="jdbc:oracle:thin:@localhost:1521:orcl"
/>
...
</data-sources >

This is the  relevant part of sessions.xml 

<toplink-configuration>
...
<session>
<name>CustomDS</name>
<project-xml>toplink-descriptor.xml</project-xml>
<session-type>
<server-session/>
</session-type>
<login>
<type>oracle.toplink.sessions.DatabaseLogin</type>
<datasource>jdbc/CustomDS</datasource>
<uses-external-connection-pool>true</uses-external-connection-pool>
</login>
</session>
</toplink-configuration>


5. Similarly define another session ,say 'DirectConnection', which uses JDBC.

Figure 5:Using JDBC

 This is the part of sessions.xml

<toplink-configuration>
<session>
<name>DirectConnection</name>
<project-xml>toplink-descriptor.xml</project-xml>
...
<login>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<connection-url>jdbc:oracle:thin:@insn104a.idc.oracle.com:1522:ora9idb</connection-url>
<user-name>hr</user-name>
<encrypted-password>62C32F70E98297522AD97E15439FAC0E</encrypted-password>
</login>
...
</session>
</toplink-configuration>

In the code, you will use the session name to obtain a session.

 //Get the named session using sessionName and SessionManager
// The sessions.xml file is loaded by SessionManager and a Session
// instance is created based on the properties set in the file
Session session=SessionManager.getManager().getSession("DirectConnection");

  Note that in a J2EE application, the sessions.xml and the project descriptor files should be available in the classpath.
e.g. Place them under WEB-INF/classes folder of your web application

Steps to run the sample 

  • Download the jar from here
  • Extract the jar file
  • Edit <SAMPLE_HOME>/META-INF/data-sources.xml to point to your database installation.
  • Open <SAMPLE_HOME>/META-INF/sessions.xml enter your database connection parameters in <connection-url>
  • Set the following environment variables
    • OC4J_HOME to the OC4J installation directory 
    • OC4J_HOST to the machine where OC4J is running.
    • JAVA_HOME to the  J2SDK 1.4.x installation directory
    • ANT_HOME to the ANT 1.4.1 installation directory
  • Make sure that <JAVA_HOME>/bin and <ANT_HOME>/bin are in PATH
  • From <SAMPLE_HOME> directory run,
    • ant - this will deploy the application to OC4J 10g
    • Point your browser to http://<your-machine>:<port>/customds/search.jsp
  • In the JSP you can select the session to use.
 

Useful References 

Please enter your comments about this sample in the OTN Sample Code Discussion Forum.


How-to use custom data sources with OracleAS TopLink

Please rate this how-to:
Excellent
Good
Average
Below Average
Poor
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy