Chapter 20.  JDBC

Table of Contents

Using the OpenJPA DataSource
Using a Third-Party DataSource
Managed and XA DataSources
Runtime Access to DataSource
Database Support
DBDictionary Properties
FirebirdDictionary Properties
MySQLDictionary Properties
OracleDictionary Properties
SybaseDictionary Properties
Delimited Identifiers Support
Setting the Transaction Isolation
Setting the SQL Join Syntax
Accessing Multiple Databases
Configuring the Use of JDBC Connections
Statement Batching
Large Result Sets
Default Schema
Schema Reflection
Schemas List
Schema Factory
Schema Tool
XML Schema Format

OpenJPA uses a relational database for object persistence. It communicates with the database using the Java DataBase Connectivity (JDBC) APIs. This chapter describes how to configure OpenJPA to work with the JDBC driver for your database, and how to access JDBC functionality at runtime.

Using the OpenJPA DataSource

OpenJPA includes its own simple javax.sql.DataSource implementation. If you choose to use OpenJPA's DataSource , then you must specify the following properties:

  • openjpa.ConnectionUserName: The JDBC user name for connecting to the database.

  • openjpa.ConnectionPassword: The JDBC password for the above user.

  • openjpa.ConnectionURL: The JDBC URL for the database.

  • openjpa.ConnectionDriverName: The JDBC driver class.

To configure advanced features, use the following optional properties. The syntax of these property strings follows the syntax of OpenJPA plugin parameters described in the section called “ Plugin Configuration ”.

  • openjpa.ConnectionProperties: If the listed driver is an instance of java.sql.Driver, this string will be parsed into a Properties instance, which will then be used to obtain database connections through the Driver.connect(String url, Properties props) method. If, on the other hand, the listed driver is a javax.sql.DataSource, the string will be treated as a plugin properties string, and matched to the bean setter methods of the DataSource instance.

  • openjpa.ConnectionFactoryProperties: OpenJPA's built-in DataSource allows you to set the following options via this plugin string:

    • QueryTimeout: The maximum number of seconds the JDBC driver will wait for a statement to execute.

    • PrettyPrint: Boolean indicating whether to pretty-print logged SQL statements.

    • PrettyPrintLineLength: The maximum number of characters in each pretty-printed SQL line.

Example 20.1.  Properties for the OpenJPA DataSource

<property name="openjpa.ConnectionUserName" value="user"/>
<property name="openjpa.ConnectionPassword" value="pass"/>
<property name="openjpa.ConnectionURL" value="jdbc:hsqldb:db-hypersonic"/>
<property name="openjpa.ConnectionDriverName" value="org.hsqldb.jdbcDriver"/>
<property name="openjpa.ConnectionFactoryProperties" 
    value="PrettyPrint=true, PrettyPrintLineLength=80"/>