How to add other JDBC drivers

When using the built-in data source component type (see also How to transaction management), you will probably want to use other data base drivers than the ones coming with Z2.

There are different alternatives on how to add a JDBC driver to Z2: If you set up your own system (as in How to create your own system) we recommend to stick to the Z2 convention and put all JDBC drivers into the module com.zfabrik.db.drivers.

Another approach is to put your drivers into your domain module (as in for example Sample-hibernate-basic), or if you have more than one, in some shared utility module that is referenced by all domain modules.

The latter approach applies in particular, if you want to modify the existing samples without setting up your own z2-base.base clone.

An example

Let's assume we want to use the sample Sample-hibernate-basic with MySQL instead of Derby. Due to licensing issues, Z2 does not contain the MySQL JDBC driver.

If you have the sample installed and setup as in How to run a sample, go to http://dev.mysql.com/downloads/connector/j/ and download the JDBC driver for MySQL. Store the driver .jar file into com.zfabrik.samples.hibernate-basic.domain/java/bin.impl/lib.

Modify the persistence unit definition in com.zfabrik.samples.hibernate-basic.domain/java/src.impl/META-INF/persistence.xml to use

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />

instead of using the Derby dialect. Also change com.zfabrik.samples.hibernate-basic.domain/DB.properties to say

com.zfabrik.component.type=javax.sql.DataSource

#
# low-level config
#

ds.type=ZFabrikPoolingDataSource
ds.enlist=workUnit

#
# Connection
#
#
# MySQL
#

ds.dataSourceClass=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
ds.prop.user=z2
ds.prop.password=z2
ds.prop.url=jdbc:mysql://localhost:3306/z2_samples?autoReconnect=true

#
# Pooling config
#

ds.prop.maxInUseConnections=10
ds.propType.maxInUseConnections=int
ds.prop.maxSpareConnections=5
ds.propType.maxSpareConnections=int
ds.prop.connectionExpiration=60000
ds.propType.connectionExpiration=int

where of course the user name, password, and specifics of the JDBC URL need to be adapted. Also, unlike for Derby, you need to create the actual data base. Assuming the database is called "z2-samples" and the user name is "z2" with password "z2", run this on the mysql command line:

create database z2_samples;
grant all on z2_samples.* to z2@localhost identified by 'z2';

References

See also