Chapter 2. Configuring the Provider

Table of Contents

2.1. Configuring the Database
2.1.1. Configuring Each Environment
2.1.2. Configuring the Database Type
2.1.3. Configuring Automatic Schema Creation
2.2. Caching
2.3. Query Hint
2.4. Configuring Logging

This chapter describes how to configure the basic provider of JEUS, EclipseLink. The configuration is needed to implement features that are not defined in JPA specifications, and needs to be set accurately according to each application.

2.1. Configuring the Database

This section describes how to configure the database for each environment and database type, and how to automatically create the database schema.

2.1.1. Configuring Each Environment

Database configuration is different for each environment.

Java EE Environment

The Java EE environment (or mode) refers to a web container, EJB container, application client container of a JEUS Managed Server.

Note

More accurately, the environment refers to the thread controlled by each container. An example is the thread of the web thread pool configured in the web engine. If there is a thread which is not controlled by a container, such as a thread pool created by an application, it is also managed in the same way as the container-controlled environment.

Target database to be used is set by the persistence.xml descriptor. The <jta-data-source> and <non-jta-data-source> elements are set according to the transaction type.

  • When using global transactions

    • Set <transaction-type> to JTA

    • Set <jta-data-source> to the JNDI name of the corresponding data source.

  • When using local transactions

    • Set <transaction-type> to RESOURCE_LOCAL

    • Set <non-jta-data-source> to the JNDI name of the corresponding data source

[Example 2.1] Configuring Database in the Java EE Mode

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="em" transaction-type="JTA">
    <jta-data-source>jdbc/MyDB</jta-data-source>
  </persistence-unit>
</persistence>


Note

1. If <transaction-type> is not set in the Java EE environment, by default, JTA transaction will be used.

2. For information on how to configure DB Data Source in JEUS, refer to JEUS Server Guide. "Chapter 6. DB Connection Pool and JDBC".

Java SE Environment

The Java SE environment (or mode) indicates that JPA is not used in a Java EE container, but in environments like the Java stand-alone client environment. In this environment, only local transactions can be used, and the JDBC properties of the target database must be configured.

Set <transaction-type>to 'RESOURCE_LOCAL'.

[Example 2.2] Configuring Database in the Java SE Mode

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="em" transaction-type="RESOURCE_LOCAL">
    <properties>
      <property name="eclipselink.jdbc.driver"
                value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="eclipselink.jdbc.url"
                value="jdbc:derby://localhost:1527/testdb"/>
      <property name="eclipselink.jdbc.user" value="APP"/>
      <property name="eclipselink.jdbc.password" value="APP"/>
    </properties>
  </persistence-unit>
</persistence>


The following describes each property:

PropertyDescription
eclipselink.jdbc.driverJDBC driver class name of target database.
eclipselink.jdbc.urlJDBC URL of target database.
eclipselink.jdbc.userUsername for target database.
eclipselink.jdbc.passwordPassword for target database.

2.1.2. Configuring the Database Type

In general, database type can be detected through JDBC connection information. However, the 'eclipselink.target-database' property can be set for cases when automatic sensing feature does not work properly or when a separate database is used.

Note

Database type can be detected by using DatabaseMetaData.getDatabaseProductName() of the JDBC driver which searches for a database vendor name using regular expressions.

[Example 2.3] Configuring Database Type

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="em">
    <jta-data-source>jdbc/MyDB</jta-data-source>
    <properties>
      <property name="eclipselink.target-database" value="DB2"/>
    </properties>
  </persistence-unit>
</persistence>

The following describes each database type value:

[Table 2.1] eclipselink.target-Database Property

ValueDescription
AutoAutomatic Sensing (Default)
CloudscapeCloudscape DBMS
DB2IBM DB2 DBMS
DB2MainframeIBM DB2 Mainframe DBMS
DerbyApache Derby DBMS
HSQLHSQL DBMS
JavaDBJavaDB DBMS
MySQL4MySQL DBMS
OracleOracle DBMS
PostreSQLPostreSQL DBMS
SQLServerMicrosoft SQLServer DBMS
SybaseSybase DBMS
Customized class nameUsed to add DBMS that is not supported by default.
OthersFor other DBs, refer to the target-database section in the Persistence Property Extensions Reference page of eclipselink.


If you want to use a DBMS that is not supported by default, you can implement the DBMS support features by specifying the corresponding class name. For more information, refer to Reference Data.

2.1.3. Configuring Automatic Schema Creation

To use the feature that automatically creates the DB schema, set the following property. The property enables the DB tables and constraints to be automatically created when an application is deployed.

[Example 2.4] Automatic Schema Creation

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="em">
    <jta-data-source>jdbc/MyDB</jta-data-source>
    <properties>
      ...
      <property name="eclipselink.ddl-generation" value="create-tables" /> 
      ...
    </properties>
  </persistence-unit>
</persistence>


The following is a configuration property associated with automatic schema creation.

ItemDescription
eclipselink.ddl-generation

Setting on how to create Data Descriptor Language (DDL) for schema.

  • none: Does nothing (default).

  • create-tables: Creates new tables while keeping current ones.

  • drop-and-create-tables: Removes current tables and creates new ones.

  • create-or-extend-tables: Creates tables, but only adds columns if there are existing tables.

When DDL is created, a Java type is created according to the corresponding the Database SQL type. The following table is a summary of Java types and the Database SQL types:

Java TypeDerby, JavaDB, CloudscapeOracleDB2SybaseSQLServerMySQL
boolean, BooleanSMALLINTNUMBER(1)SMALLINTBITBITTINYINT(1)
int, IntegerINTEGERNUMBER(10)INTEGERINTEGERINTEGERINTEGER
long, LongBIGINTNUMBER(19)INTEGERNUMERIC(19)NUMERIC(19)BIGINT
float, FloatFLOATNUMERIC(19,4)FLOATFLOAT(16)FLOAT(16)FLOAT
double, DoubleFLOATNUMERIC(19,4)FLOATFLOAT(32)FLOAT(32)DOUBLE
short, ShortSMALLINTNUMBER(5)SMALLINTSMALLINTSMALLINTSMALLINT
byte, ByteSMALLINTNUMBER(3)SMALLINTSMALLINTSMALLINTSMALLINT

java.lang

.Number

DECIMALNUMBER(38)DECIMAL(15)NUMERIC(38)NUMERIC(28)DECIMAL(38)

java.math

.BigInteger

BIGINTNUMBER(38)BIGINTNUMERIC(38)NUMERIC(28)BIGINT

java.math

.BigDecimal

DECIMALNUMBER(38)DECIMAL(15)NUMERIC(38)NUMERIC(28)DECIMAL(38)

java.lang

.String

VARCHAR(255)VARCHAR(255)VARCHAR(255)VARCHAR(255)VARCHAR(255)VARCHAR(255)
char, CharacterCHAR(1)CHAR(1)CHAR(1)CHAR(1)CHAR(1)CHAR(1)
byte[], Byte[], java.sql.BlobBLOB(64000)LONG RAWBLOB(64000)TEXTTEXTTEXT(64000)
char[], Character[], java.sql.ClobCLOB(64000)LONGCLOB(64000)TEXTTEXTTEXT(64000)
java.sql.DateDATEDATEDATEDATETIMEDATETIMEDATE
java.sql.TimeTIMEDATETIMEDATETIMEDATETIMETIME

java.sql

.Timestamp

TIMESTAMPDATETIMESTAMPDATETIMEDATETIMEDATETIME

2.2. Caching

JPA supports 1st-level caching called persistence context by default. However, since persistence context (except extended persistence context) is newly created for each transaction, caching between transactions is not supported. To compensate for this, Eclipse Link provides 2nd-level caching capability.

Because 2nd-level caching is supported in the EntityManagerFactory level, all EntityManagers created in the same EntityManagerFactory use the shared cache. An entity, that does not exist in persistence context, is obtained from the 2nd-level cache, if the entity exists there. This will help improve performance when repeatedly performing a reading job.

[Example 2.5] Caching Type Configuration Example

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="em">
    <jta-data-source>jdbc/MyDB</jta-data-source>
    <properties>
      ...
      <property name="eclipselink.ddl-generation" value="create-tables" />
      <property name="eclipselink.cache.type.default" value="NONE" />
      <property name="eclipselink.cache.size.default" value="999" />
      <property name="eclipselink.cache.shared.default" value="false" /> 
      ...
    </properties>
  </persistence-unit>
</persistence>

The following table describes caching options:

ItemDescription
eclipselink.cache.type.default

Sets the caching type.

For options other than the ones listed below, or for advanced settings, refer to the CacheType and Size item of "EclipseLink User Guide."

  • Full: Caches objects using hard references. They exist in the cache until the entity is removed.

  • Weak: Caches objects using weak references. They are removed after garbage collection (GC).

  • Soft: Similar to Weak, but removes objects only when memory is insufficient (default value).

  • NONE: Does not save objects in a cache. To disable the caching feature, change the eclipselink.cache.shared.xxx property.

eclipselink.cache.size.defaultSets the maximum number of objects that can be saved in the cache (the default value: 1,000)
eclipselink.cache.shared.default

Sets whether a shared cache is used.

  • true: Objects are saved in the shared cache. All EntityManagers use the cache (the default value).

  • false: Objects are not saved in the shared cache. EntityManagers do not share the cache. If set to "false," the 2nd-level caching is not used.

eclipselink.cache.type.<ENTITY>

Sets the cache type for each entity.

<ENTITY> may be replaced by an entity name or a fully-qualified class name. All entities related to the entity must have the same setting.

Same as the value described for eclipselink.cache.type.default.

eclipselink.cache.size.<ENTITY>

Sets the cache size for each Entity

<ENTITY> may be replaced by an entity name or a fully-qualified class name

Same as the value described for eclipselink.cache.size.default.

eclipselink.cache.shared.<ENTITY>

Sets whether each entity uses the shared cache.

<ENTITY> may be replaced by an entity name or a fully-qualified class name. All entities related to the entity must have the same setting.

Same as the value described for eclipselink.cache.shared.default.

Note

When the 2nd-level caching is used, changes to DB data does not apply to the cache when changes are made directly or by external applications. In this case, the value in the cache, instead of the most recent value, is returned to the applications.

To prevent this, set the caching options accordingly or use EntityManager.refresh() and toplink.refresh query hint or locking (pessimistic/optimistic).

2.3. Query Hint

When using query objects, query hint enables the use of features supported by the provider.

It can be set when a query is performed as shown in the following example. It can also be set by @QueryHintAnnotation when using Named Query.

[Example 2.6] Using Query Hint

List employees 
  = em.createQuery("SELECT e FROM Employee e WHERE e.name = :name")
      .setParameter("name", name)
      .setHint("eclipselink.refresh", true)
      .getResultList();


The following are query hints supported in JEUS8.

ItemDescription
eclipselink.pessimistic-lock

Sets whether pessimistic locking is used when "SELECT" is performed.

  • NoLock: Does not use the locking (the default value).

  • Lock: Uses the locking by executing the "SELECT ... FOR UPDATE" statement.

  • NoLockWait: Uses the locking by executing the "SELECT ... FOR UPDATE NO WAIT" statement.

eclipselink.refresh

Sets whether to update a cache with the latest values from DB.

  • true: Gets the latest values and updates the cache.

  • false : Uses the values in the cache (the default value).

2.4. Configuring Logging

To see more detailed logs, configure the logging level.

By default, the logging level is set to the default level (INFO) of JEUS Server. Use the eclipselink.logging.level property to change the level for each persistence unit.

In the Java EE mode, the JEUS logger provided by JEUS is used by default. In the Java SE mode, the DefaultLogger is used by default. You can change the default logger by using the eclipselink.logging.logger property.

[Example 2.7] Logging Configuration Example

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
  <persistence-unit name="em">
    <jta-data-source>jdbc/MyDB</jta-data-source>
    <properties>
      ...
      <property name="eclipselink.logging.level" value="FINE"/>
      <property name="eclipselink.logging.logger" value="DefaultLogger"/>
      ...
    </properties>
  </persistence-unit>
</persistence>

The following shows more detailed information:

ItemDescription
eclipselink.logging.level

Sets the logging level.

  • OFF: Log is not recorded

  • SEVERE

  • WARNING

  • INFO (the default value)

  • CONFIG

  • FINE: set to this level for more information related to SQL.

  • FINER

  • FINEST

eclipselink.logging.logger

Sets the logger to be used.

  • JEUSLogger: The logger provided by JEUS.

    (The default value for the Java EE mode)

  • DefaultLogger: The default standard output Logger

    (The default value for the Java SE mode)

  • JavaLogger: java.util.logging Logger.

  • Custom class name: Set to this logger if a separate logger is implemented

Note

For more information about EclipseLink, refer to http://wiki.eclipse.org/EclipseLink/UserGuide/JPA