Skip to content

JDBC

JDBC driver, DataSource and XADataSource class names per database, with GlassFish and Payara connection pool examples.

5 min readUpdated

JDBC (Java Database Connectivity) is the standard Java API for talking to relational databases. When you define a connection pool in an application server, you choose which JDBC interface the pool uses and give the vendor’s class that implements it.

This note lists those class names and URL formats for common databases, shows jdbc-connection-pool definitions for GlassFish and Payara (in domain.xml or glassfish-resources.xml), and explains the difference between local and XA transactions.

Choose a resource type

A connection pool is built on one of four interfaces. The interfaces are part of Java SE and keep their java.sql and javax.sql package names, also in Jakarta EE.

  • java.sql.Driver: the basic driver. The server opens connections through the driver class and a URL.
  • javax.sql.DataSource: a connection factory configured with properties (server, port, database).
  • javax.sql.ConnectionPoolDataSource: a data source that hands out pooled physical connections.
  • javax.sql.XADataSource: a data source whose connections can take part in distributed (XA) transactions.

The PostgreSQL driver implements each one in a separate class:

Interface PostgreSQL class
java.sql.Driver org.postgresql.Driver
javax.sql.DataSource org.postgresql.ds.PGSimpleDataSource
javax.sql.ConnectionPoolDataSource org.postgresql.ds.PGConnectionPoolDataSource
javax.sql.XADataSource org.postgresql.xa.PGXADataSource

Driver classes and URLs per database

Database JDBC driver XA data source Example URL
Db2 com.ibm.db2.jcc.DB2Driver com.ibm.db2.jcc.DB2XADataSource jdbc:db2://localhost:50000/MYDB:user=dbadm;password=dbadm;
Derby org.apache.derby.jdbc.ClientDriver org.apache.derby.jdbc.ClientXADataSource jdbc:derby://localhost:1527/myDB, jdbc:derby:memory:myDB;create=true
H2 org.h2.Driver org.h2.jdbcx.JdbcDataSource jdbc:h2:tcp://localhost/~/test, jdbc:h2:mem:myDB
MariaDB org.mariadb.jdbc.Driver org.mariadb.jdbc.MariaDbDataSource jdbc:mariadb://localhost:3306/test
SQL Server com.microsoft.sqlserver.jdbc.SQLServerDriver com.microsoft.sqlserver.jdbc.SQLServerXADataSource jdbc:sqlserver://localhost:1433;databaseName=AdventureWorks
MySQL com.mysql.cj.jdbc.Driver com.mysql.cj.jdbc.MysqlXADataSource jdbc:mysql://localhost:3306/test
Oracle oracle.jdbc.OracleDriver oracle.jdbc.xa.client.OracleXADataSource jdbc:oracle:thin:@localhost:1521/ORCL_SVC
PostgreSQL org.postgresql.Driver org.postgresql.xa.PGXADataSource jdbc:postgresql://localhost/test

Older names you may still find in existing configurations:

  • Oracle: oracle.jdbc.driver.OracleDriver is the deprecated predecessor of oracle.jdbc.OracleDriver.
  • MariaDB: Connector/J 1.x used org.mariadb.jdbc.MySQLDataSource; current versions use org.mariadb.jdbc.MariaDbDataSource, which also implements XADataSource.
  • MySQL: Connector/J 5.1 used com.mysql.jdbc.Driver and com.mysql.jdbc.jdbc2.optional.MysqlXADataSource; Connector/J 8 and later use the com.mysql.cj package.

Configure a connection pool per database

Each example below is a jdbc-connection-pool element for GlassFish or Payara. The three settings that must fit together are:

  • datasource-classname: the class from the tables above.
  • res-type: the interface that class implements.
  • driver-classname: only for pools with res-type="java.sql.Driver".

Use a DataSource class for local transactions and an XADataSource class when the pool must join XA transactions (see Local and XA transactions).

MySQL

Setting Local transactions XA transactions
datasource-classname com.mysql.cj.jdbc.MysqlDataSource com.mysql.cj.jdbc.MysqlXADataSource
res-type javax.sql.DataSource javax.sql.XADataSource

For a pool with res-type="java.sql.Driver", the driver class is com.mysql.cj.jdbc.Driver.

A pool for local transactions:

<jdbc-connection-pool
datasource-classname="com.mysql.cj.jdbc.MysqlDataSource"
name="app_mysql_pool"
res-type="javax.sql.DataSource">
<property name="user" value="dbuser"/>
<property name="password" value="dbpassword"/>
<property name="allowPublicKeyRetrieval" value="true"/>
<property name="useSSL" value="false"/>
<property name="url" value="jdbc:mysql://localhost:3306/dbmaster?zeroDateTimeBehavior=convertToNull"/>
</jdbc-connection-pool>

allowPublicKeyRetrieval=true and useSSL=false let the driver log in to MySQL 8 without TLS; use them only on a trusted local network. zeroDateTimeBehavior=convertToNull returns NULL for MySQL’s 0000-00-00 dates instead of throwing an exception.

Oracle

Setting Local transactions XA transactions
datasource-classname oracle.jdbc.pool.OracleDataSource oracle.jdbc.xa.client.OracleXADataSource
res-type javax.sql.DataSource javax.sql.XADataSource

A pool for local transactions:

<jdbc-connection-pool
datasource-classname="oracle.jdbc.pool.OracleDataSource"
name="app_oracle_pool"
res-type="javax.sql.DataSource">
<property name="user" value="dbuser"/>
<property name="password" value="dbpassword"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"/>
</jdbc-connection-pool>

Oracle thin URLs come in two forms: @host:port:SID (as here) connects to an instance by SID, and @host:port/service (as in the table above) connects to a service name.

SQL Server

Setting Local transactions XA transactions
datasource-classname com.microsoft.sqlserver.jdbc.SQLServerDataSource com.microsoft.sqlserver.jdbc.SQLServerXADataSource
res-type javax.sql.DataSource javax.sql.XADataSource

This pool reads the connection details from environment variables with Payara’s ${ENV=...} syntax, so the same configuration works in every environment:

<jdbc-connection-pool
datasource-classname="com.microsoft.sqlserver.jdbc.SQLServerDataSource"
name="app_mssql_pool"
res-type="javax.sql.DataSource">
<property name="User" value="${ENV=MY_APP_DATABASE_USER}"/>
<property name="Password" value="${ENV=MY_APP_DATABASE_PASS}"/>
<property name="DatabaseName" value="${ENV=MY_APP_DATABASE_NAME}"/>
<property name="ServerName" value="${ENV=MY_APP_DATABASE_SERVER}"/>
<property name="PortNumber" value="1433"/>
</jdbc-connection-pool>

PostgreSQL

Setting Local transactions XA transactions
datasource-classname org.postgresql.ds.PGSimpleDataSource org.postgresql.xa.PGXADataSource
res-type javax.sql.DataSource javax.sql.XADataSource

A pool for XA transactions:

<jdbc-connection-pool
datasource-classname="org.postgresql.xa.PGXADataSource"
name="app_postgresql_pool"
res-type="javax.sql.XADataSource">
<property name="user" value="postgres"/>
<property name="password" value="postgres"/>
<property name="URL" value="jdbc:postgresql://localhost:5432/postgres"/>
<property name="driverClass" value="org.postgresql.Driver"/>
</jdbc-connection-pool>

XA with PostgreSQL also needs max_prepared_transactions set above zero on the server; see the PostgreSQL note.

Derby

Derby has two sets of classes: the client classes connect to a Derby network server, the embedded classes run the database inside the application’s JVM.

  • Client: org.apache.derby.jdbc.ClientDataSource, org.apache.derby.jdbc.ClientXADataSource
  • Embedded: org.apache.derby.jdbc.EmbeddedDataSource, org.apache.derby.jdbc.EmbeddedXADataSource

These are the class names used up to Derby 10.14.

Setting Local transactions XA transactions
datasource-classname org.apache.derby.jdbc.ClientDataSource org.apache.derby.jdbc.ClientXADataSource
res-type javax.sql.DataSource javax.sql.XADataSource

A client pool for XA transactions. create=true in connectionAttributes creates the database on first connection:

<jdbc-connection-pool
datasource-classname="org.apache.derby.jdbc.ClientXADataSource"
name="app_derby_pool"
res-type="javax.sql.XADataSource">
<property name="portNumber" value="1527"/>
<property name="password" value="APP"/>
<property name="user" value="APP"/>
<property name="serverName" value="localhost"/>
<property name="databaseName" value="sun-appserv-samples"/>
<property name="connectionAttributes" value="; create=true"/>
</jdbc-connection-pool>

Local and XA transactions

There are three ways a pool can handle transactions:

  • No transaction: every statement is committed automatically (auto-commit).
  • Local (non-XA) transaction: several operations against one database run in one transaction on the same connection and need an explicit commit or rollback.
  • XA transaction: several operations against different resources (for example two databases, or a database and a JMS queue) run in one global transaction. A transaction manager coordinates the resources and commits them with the two-phase commit protocol.

A non-XA transaction has no coordinator: a single resource does all its transaction work itself. An XA transaction has a transaction manager that coordinates one or more resources in a single global transaction.

A practical rule: reads that need no transaction boundary can run without a transaction; use a local transaction for DML (INSERT, UPDATE, DELETE) so that you control the boundary with begin, commit and rollback; use XA only when one transaction really spans more than one resource, because two-phase commit adds overhead.

References

Related notes

6 min read

SQL joins

INNER, LEFT, RIGHT, FULL OUTER, CROSS and self joins in SQL, with two small example tables, the queries and their results.

  • Databases
3 min read

Database migration: MySQL to PostgreSQL

Migrate a MySQL database to PostgreSQL with pgloader in Docker, and fix the two authentication errors you are likely to hit.

  • Databases
4 min read

Jakarta EE

Jakarta EE 10 and 11: component versions, deployment descriptor headers, Faces namespaces and a multi-module project layout.

  • Java & Jakarta EE

Type to search the whole site.