Part I Development Tasks and Tools
1. Setting Up a Development Environment
Part II Developing Applications and Application Components
6. Using the Java Persistence API
Additional Database Properties
Primary Key Generation Defaults
Changing the Persistence Provider
Restrictions and Optimizations
Using @OrderBy with a Shared Session Cache
Using BLOB or CLOB Types with the Inet Oraxo JDBC Driver
Named Native Queries and JDBC Queries
7. Developing Web Applications
8. Using Enterprise JavaBeans Technology
9. Using Container-Managed Persistence
12. Developing Lifecycle Listeners
13. Developing OSGi-enabled Java EE Applications
Part III Using Services and APIs
14. Using the JDBC API for Database Access
15. Using the Transaction Service
16. Using the Java Naming and Directory Interface
The GlassFish Server uses the bundled Java DB (Derby) database by default, named jdbc/__default. If the transaction-type element is omitted or specified as JTA and both the jta-data-source and non-jta-data-source elements are omitted in the persistence.xml file, Java DB is used as a JTA data source. If transaction-type is specified as RESOURCE_LOCAL and both jta-data-source and non-jta-data-source are omitted, Java DB is used as a non-JTA data source.
To use a non-default 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, 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's dialect. You can specify the optional eclipselink.target-database property to guarantee that the database type is correct. For example:
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence"> <persistence-unit name ="em1"> <jta-data-source>jdbc/MyDB2DB</jta-data-source> <properties> <property name="eclipselink.target-database" value="DB2"/> </properties> </persistence-unit> </persistence>
The following eclipselink.target-database property values are allowed. Supported platforms have been tested with the GlassFish Server and are found to be Java EE compatible.
//Supported platforms JavaDB Derby Oracle MySQL4 //Others available SQLServer DB2 Sybase PostgreSQL Informix TimesTen Attunity HSQL SQLAnyWhere DBase DB2Mainframe Cloudscape PointBase
For more information about the eclipselink.target-database property, see Using EclipseLink JPA Extensions for Session, Target Database and Target Application Server.
To use the Java Persistence API outside the EJB container (in Java SE mode), do not specify the jta-data-source or non-jta-data-source elements. Instead, specify the provider element and any additional properties required by the JDBC driver or the database. For example:
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> <persistence-unit name ="em2"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <class>ejb3.war.servlet.JpaBean</class> <properties> <property name="eclipselink.target-database" value="Derby"/> <!-- JDBC connection properties --> <property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/> <property name="eclipselink.jdbc.url" value="jdbc:derby://localhost:1527/testdb;retrieveMessagesFromServerOnGetMessage=true;create=true;"/> <property name="eclipselink.jdbc.user" value="APP"/> <property name="eclipselink.jdbc.password" value="APP"/> </properties> </persistence-unit> </persistence>
For more information about eclipselink properties, see Additional Database Properties.
For a list of the JDBC drivers currently supported by the GlassFish Server, see the Oracle GlassFish Server 3.1-3.1.1 Release Notes. For configurations of supported and other drivers, see Configuration Specifics for JDBC Drivers in Oracle GlassFish Server 3.1 Administration Guide.
To change the persistence provider, see Changing the Persistence Provider.