4 Using TopLink with GlassFish Server
This chapter includes the following sections:
Use Case
Users want to run applications that employ JPA on Oracle GlassFish Server.
Solution
The Oracle GlassFish platform provides full support for EclipseLink. Developers writing applications for the GlassFish Server platform can achieve full Java-to-data source integration that complies with the Java Persistence API (JPA) 2.0 specification. EclipseLink allows you to integrate Java applications with any data source, without compromising ideal application design or data integrity.
Components
-
GlassFish Server 3.1.2.
-
TopLink 12c (12.1.2.0.0) or later.
Note:
TopLink's core functionality is provided by EclipseLink, the open source persistence framework from the Eclipse Foundation. EclipseLink implements Java Persistence API (JPA), Java Architecture for XML Binding (JAXB), and other standards-based persistence technologies, plus extensions to those standards. TopLink includes all of EclipseLink, plus additional functionality from Oracle.
-
EclipseLink 2.3.0 or later.
-
Any compliant JDBC database including Oracle Database, Oracle Database Express Edition, MySQL, and so on.
-
While it is not required, you may want to use a Java EE integrated development environment (IDE) for convenience during development.
Introduction to the Solution
Oracle GlassFish Server is the reference implementation of the Java Platform, Enterprise Edition (Java EE platform) specification. Built using the GlassFish Server Open Source Edition, GlassFish Server delivers a flexible, lightweight, and production-ready Java EE platform.
GlassFish Server is part of the Oracle Fusion Middleware application grid portfolio of products and is ideally suited for applications requiring lightweight infrastructure with the most up-to-date implementation of the Java EE platform. GlassFish Server complements Oracle WebLogic Server, which is designed to run the broader portfolio of Oracle Fusion Middleware and large-scale enterprise applications.
Advantages to Using TopLink with GlassFish Server
By adding TopLink support, developers writing applications for the GlassFish Server platform can achieve full Java-to-data source integration that complies with the Java Persistence API (JPA) 2.0 specification. TopLink allows you to integrate Java applications with any data source, without compromising ideal application design or data integrity. In addition, TopLink gives your GlassFish Server platform applications the ability to store (that is, persist) and retrieve business domain objects using a relational database or an XML data source as a repository.
While GlassFish Server can use other persistence providers and TopLink can be used with other application servers, using GlassFish Server with TopLink provides a number of advantages:
-
TopLink is included in all GlassFish Server distributions and is the default JPA provider.
-
TopLink allows applications running on GlassFish Server to use Oracle Coherence caches. Coherence is a Java-based in-memory application grid product that provides data caching, data replication, and distributed computing services. TopLink includes features that allow deployed applications to use Coherence data caches and to incorporate TopLink Grid as an object-to-relational persistence framework. How to use this feature is beyond the scope of this guide. See Integrating Oracle Coherence for more information.
-
TopLink logging integration in GlassFish Server provides a comprehensive, integrated logging infrastructure.
-
EclipseLink JAXB is also included in GlassFish versions 3.1.2 and later. Although it is not the default JAXB implementation, it can be used in JAX-WS and JAX-RS applications. For more information, see:
http://blog.bdoughan.com/2012/02/glassfish-312-is-full-of-moxy.html
-
EclipseLink MOXy is also included in GlassFish versions 3.1.2 and later. Although it is not the default JAXB implementation, it can be used in JAX-WS and JAX-RS applications. For more information, see:
http://blog.bdoughan.com/2012/02/glassfish-312-is-full-of-moxy.html
-
GlassFish Server supports the Oracle Application Development Framework (Oracle ADF), an end-to-end Java EE framework, based on Struts and JavaServer Faces (JSF). Oracle ADF simplifies application development by providing infrastructure services and a visual and declarative development experience. TopLink and Oracle ADF together provide a complete Java EE application infrastructure. Oracle ADF is beyond the scope of this guide. See Oracle Fusion Middleware Fusion Developer's Guide for Oracle Application Development Framework.
Relationship of GlassFish Server and TopLink to Fusion Middleware Products
Figure 4-1 illustrates how GlassFish Server and TopLink are related to and used with other Oracle products. The following are examples of using GlassFish Server and TopLink with other Oracle Middleware products:
-
Use EclipseLink as the persistence provider.
-
Use Oracle Coherence (through Oracle TopLink Grid integration) for data caching, data replication and distributed computing services.
-
Use GlassFish as the application server.
-
Use the Oracle database for persisting data.
Note:
Oracle Coherence and TopLink Grid are beyond the scope of this guide. For information about Coherence, see Developing Applications with Oracle Coherence, and follow links to other Coherence documentation. For information about TopLink Grid, see Integrating Oracle Coherence.
Figure 4-1 GlassFish Server, TopLink and Other Products in the Oracle Fusion Middleware Stack

Description of "Figure 4-1 GlassFish Server, TopLink and Other Products in the Oracle Fusion Middleware Stack"
Implementing the Solution
To run EclipseLink JPA applications in GlassFish Server, you must configure the server and coordinate certain server and application settings. These are described in the following tasks.
Task 1: Prerequisites
This document is based on the following products and tools, although the principles apply to any supported database or development environment. It is assumed that the software is already installed, except where noted in later sections.
-
GlassFish Server 3.1.2.
For more information and downloads, see
http://www.oracle.com/technetwork/middleware/glassfish/overview/index.html
on the Oracle Technology Network. -
EclipseLink 2.4.1.
For more information and downloads, see
http://www.eclipse.org/eclipselink/
on the EclipseLink website. -
Any compliant JDBC database including Oracle Database, Oracle Database Express Edition, MySQL, and so on.
For Oracle Database, see
http://www.oracle.com/technetwork/database/enterprise-edition/overview/index.html
.For Oracle Database Express Edition, see
http://www.oracle.com/technetwork/database/express-edition/overview/index.html
.For MySQL, see
http://www.oracle.com/us/products/mysql/index.html
. -
While it is not required, you may want to use a Java EE integrated development environment (IDE) for convenience during development. For example, Oracle JDeveloper, Oracle Enterprise Pack for Eclipse, and NetBeans all provide sophisticated Java EE development tools.
For JDeveloper, see
http://www.oracle.com/technetwork/developer-tools/jdev/downloads/index.html
.For Oracle Enterprise Pack for Eclipse, see
http://www.oracle.com/technetwork/developer-tools/eclipse/overview/index.html
.For NetBeans, see
https://www.oracle.com/tools/technologies/netbeans-ide.html
.
Task 2: Install GlassFish Server
EclipseLink is included with the GlassFish Server distribution. You can find instructions for installing and configuring GlassFish Server at this URL:
http://docs.oracle.com/cd/E26576_01/index.htm
The EclipseLink modules appear as separate JAR files in the modules
directory.
* \glassfish\modules . . . o org.eclipse.persistence.antlr.jar o org.eclipse.persistence.asm.jar o org.eclipse.persistence.core.jar o org.eclipse.persistence.jpa.jar o org.eclipse.persistence.jpa.modelgen.jar o org.eclipse.persistence.moxy.jar o org.eclipse.persistence.oracle.jar . . .
Note:
-
The
toplink-grid.jar
file, which provides support for Coherence caches, is available only if you purchase the license for Oracle Coherence. For more information about the functionality provided by thetoplink-grid.jar
file, see Integrating Oracle Coherence. -
The
org.eclipse.persistence.oracle.jar
file is available with GlassFish and provides Oracle Database-specific functionality for EclipseLink. This file is used only for applications running against an Oracle Database.
Object-XML (also known as JAXB support) is a component that enables you to bind Java classes to XML schemas. This support is provided by the org.eclipse.persistence.moxy.jar
.
Object-XML (also known as JAXB support, or MOXy) is a component that enables you to bind Java classes to XML schemas. This support is provided by the org.eclipse.persistence.moxy.jar
.
Task 3: Set Up the Data Source
Configuring an Oracle database as a JDBC resource for a Java EE application involves the following steps:
Integrate the JDBC Driver for Oracle Database into GlassFish Server
To integrate the JDBC driver, copy its JAR file into the domain and then restart the domain and instances to make the driver available.
Create a JDBC Connection Pool for the Resource
You can create a JDBC connection pool from the GlassFish Server Administration Console or from the command line.
To use the GlassFish Server Administration Console:
In the GlassFish Server Administration Console, expand the Common Tasks node, then click the Create New JDBC Connection Pool button in the Common Tasks page. Specify the name of the pool, the resource type, the name of the database provider, the data source and driver class names, and other details. For more information, see To Create a JDBC Connection Pool in GlassFish Server Administration Console Online Help.
To use the command line:
Create the JDBC Resource
You can use the GlassFish Server Administration Console to create the JDBC resource or you can use the command line.
To use the GlassFish Server Administration Console:
In the GlassFish Server Administration Console, expand the Resources node, then the JDBC node, then the JDBC Resources node to open the JDBC Resources page. Provide a unique JNDI resource name and associate the resource with a connection pool. For more information, see "To Create a JDBC Resource" in the GlassFish Server Administration Console Online Help.
To use the command line:
Use the create-jdbc-resource
subcommand to create the JDBC resource, and name it so that the application can discover it using JNDI lookup, for example:
create-jdbc-resource --connectionpoolid poolbvcallbackbmt jdbc/bvcallbackbmt
Task 4: Create the persistence.xml File
Example 4-1 illustrates a sample persistence.xml
file that specifies the default persistence provider for EclipseLink, org.eclipse.persistence.jpa.PersistenceProvider
. For more information about this file, see About the Persistence Unit in Understanding Oracle TopLink.
If you are using the default persistence provider, then you can specify additional database properties described in Java Persistence API (JPA) Extensions Reference for Oracle TopLink.
Several of the values you enter in the file must match the values you chose when you defined the cluster, connection, and connection pool properties in GlassFish Server, as follows:
JDBC Data Source Properties:
-
Name: The name of the data source, which is typically the same as the JNDI name, for example
jdbc/bvcallbackbmt
. -
JNDI Name: The JNDI path to where this data source is bound. This must be the same name as the value for the
<jta-data-source>
element inpersistence.xml
, for examplejdbc/bvcallbackbmt
. -
Database Type:
Oracle
-
Database Driver: (default) Oracle's Driver (Thin XA) for Instance connections; Versions: 9.0.1 and later
Connection Properties:
-
Database Name: The name of the database, for example,
XE
for Oracle Database Express Edition samples. -
Host Name: The IP address of the database server, for example
127.0.0.1
for a locally hosted database. -
Port: The port number on which your database server listens for connection requests, for example,
1521
, the default for Oracle Database Express Edition 11g. -
Database User Name: The database account user name used to create database connections, for example
hr
for Oracle Database Express Edition 11g samples. -
Password: Your password.
Select Targets:
-
Servers / Clusters: Select the administration server, managed servers, or clusters to which you want to deploy the data source. You can choose one or more.
The sample persistence.xml
file in Example 4-1 highlights the properties defining the persistence provider, the JTA data source, and logging details. In this example, the logging level is set to FINE
. At this level, SQL code generated by EclipseLink is logged to the server.log
file. For more information about these properties, see:
Example 4-1 Sample persistence.xml File
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> <persistence-unit name="pu1" transaction-type="JTA"><provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/bvcallbackbmt</jta-data-source>
<properties><property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/> </properties> </persistence-unit> </persistence>
Specify the Persistence Provider
The persistence provider defines the implementation of JPA. It is defined in the provider
element of the persistence.xml
file. Persistence providers are vendor-specific. The persistence provider for EclipseLink is org.eclipse.persistence.jpa.PersistenceProvider
.
Specify an Oracle Database
You specify the database connection details in the persistence.xml
file. GlassFish Server uses the bundled Java DB (Derby) database by default, named jdbc/__default
. To use a nondefault database, such as the Oracle Database, either specify a value for the jta-data-source
element, or set the transaction-type
element to RESOURCE_LOCAL
and specify a value for the non-jta-data-source
element.
If you are using the default persistence provider, org.eclipse.persistence.jpa.PersistenceProvider
, then the provider attempts to automatically detect the database type based on the connection metadata. This database type is used to issue SQL statements specific to the detected database type. You can specify the optional eclipselink.target-database
property to guarantee that the database type is correct.
For more information about specifying database properties in a persistence.xml
file for GlassFish Server, see Specifying the Database for an Application in the Oracle GlassFish Server Application Development Guide.
Specify Logging
EclipseLink provides a logging utility even though logging is not part of the JPA specification. Hence, the information provided by the log is EclipseLink JPA-specific. With EclipseLink, you can enable logging to view the following information:
-
Configuration details
-
Information to facilitate debugging
-
The SQL that is being sent to the database
You can specify logging in the persistence.xml
file. EclipseLink logging properties let you specify the level of logging and whether the log output goes to a file or standard output. Because the logging utility is based on java.util.logging
, you can specify a logging level to use.
The logging utility provides nine levels of logging control over the amount and detail of the log output. Use eclipselink.logging.level
to set the logging level, for example:
<property name="eclipselink.logging.level" value="FINE"/>
By default, the log output goes to System.out
or to the console. To configure the output to be logged to a file, set the property eclipselink.logging.file
, for example:
<property name="eclipselink.logging.file" value="output.log"/>
EclipseLink's logging utility is pluggable, and several different logging integrations are supported, including java.util.logging
. To enable java.util.logging
, set the property eclipselink.logging.logger
, for example:
<property name="eclipselink.logging.logger" value="JavaLogger"/>
While running inside GlassFish Server, EclipseLink is configured by GlassFish Server to use JavaLogger
by default. The log is always redirected to the GlassFish Server server.log
file. For more information, see Setting Log Levels" in Oracle GlassFish Server Administration Guide.
For more information about EclipseLink logging and the levels of logging available in the logging utility, see Persistence Property Extensions Reference in Java Persistence API (JPA) Extensions Reference for Oracle TopLink.
Task 5: Set Up GlassFish Server for JPA
GlassFish Server Application Development Guide describes server-specific considerations on setting up GlassFish Server to run applications that employ JPA:
http://docs.oracle.com/cd/E26576_01/doc.312/e24930/jpa.htm
It provides more information about these topics:
-
"Specifying the Database for an Application," for information about database connection properties
-
"Specifying the Persistence Provider for an Application," for setting the default or non-default persistence provider for an application
-
"Primary Key Generation Defaults," for the default persistence provider's primary key generation defaults
-
"Automatic Schema Generation," for information on annotations and options to manage automatic schema generation
-
"Restrictions and Optimizations," for restrictions and performance optimizations that affect using the Java Persistence API
Task 6: Create the Application
To create an application that uses EclipseLink as its JPA persistence provider, you may want to use a Java EE IDE for convenience during development. For example, JDeveloper, Oracle Enterprise Pack for Eclipse, and NetBeans provide sophisticated Java EE development tools, including support for EclipseLink. See Key Tools in Understanding Oracle TopLink.
For guidance in writing your application, see these topics from the Configuring the Java Persistence Provider chapter in Oracle GlassFish Server Application Development Guide.
Task 7: Deploy the Application to GlassFish Server
For information about deploying to GlassFish Server, see Deploy Applications or Modules, To Deploy an Enterprise Application, and To Deploy a Web Application in GlassFish Server Administration Console Online Help. See also Oracle GlassFish Server Application Deployment Guide, at:
Task 8: Run the Application
For instructions for starting a deployed application from the GlassFish Server Administration Console, see Application Client Launch and To Launch an Application in GlassFish Server Administration Console Online Help.
Task 9: Monitor the Application
GlassFish Server provides a monitoring service to track the health and performance of an application. For information about monitoring an application from the console, see the Monitoring and Monitoring Data topics in GlassFish Server Administration Console Online Help. For information about monitoring the application from the command line, see Administering the Monitoring Service in Oracle GlassFish Server Administration Guide, at:
http://docs.oracle.com/cd/E26576_01/doc.312/e24928/monitoring.htm
Additional Resources
See the following resources for more information about the technologies and tools used to implement the solutions in this chapter:
-
Oracle GlassFish Server Administration Guide
-
Oracle GlassFish Server Application Deployment Guide
-
Oracle GlassFish Server Application Development Guide
-
Oracle GlassFish Server 3.1.2 to 3.1.2.2 Documentation Library