Project

General

Profile

How to JDBC drivers » History » Version 9

Henning Blohm, 24.09.2012 21:14

1 1 Henning Blohm
h1. How to add other JDBC drivers
2
3 4 Henning Blohm
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.
4 2 Henning Blohm
5 3 Henning Blohm
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*.
6 1 Henning Blohm
7 3 Henning Blohm
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.
8 1 Henning Blohm
9
The latter approach applies in particular, if you want to modify the existing samples without setting up your own *z2-base.base* clone. 
10 2 Henning Blohm
11 3 Henning Blohm
h2. An example
12
13 2 Henning Blohm
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.
14
15 9 Henning Blohm
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. Copy the driver .jar file into *com.zfabrik.samples.hibernate-basic.domain/java/bin.impl/lib*.
16 2 Henning Blohm
17
Modify the persistence unit definition in *com.zfabrik.samples.hibernate-basic.domain/java/src.impl/META-INF/persistence.xml* to use 
18
19
<pre><code class="xml">
20
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
21 1 Henning Blohm
</code></pre> 
22
23
instead of using the Derby dialect. Also change *com.zfabrik.samples.hibernate-basic.domain/DB.properties* to say
24
25
<pre><code class="ruby">
26 3 Henning Blohm
com.zfabrik.component.type=javax.sql.DataSource
27
28
29
#
30
# low-level config
31
#
32
33
ds.type=ZFabrikPoolingDataSource
34
ds.enlist=workUnit
35
36
#
37
# Connection
38
#
39
#
40
# MySQL
41
#
42
43
ds.dataSourceClass=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
44 7 Henning Blohm
ds.prop.user=z2
45
ds.prop.password=z2
46 6 Henning Blohm
ds.prop.url=jdbc:mysql://localhost:3306/z2_samples?autoReconnect=true
47 3 Henning Blohm
48
#
49
# Pooling config
50
#
51
52
ds.prop.maxInUseConnections=10
53
ds.propType.maxInUseConnections=int
54
ds.prop.maxSpareConnections=5
55
ds.propType.maxSpareConnections=int
56
ds.prop.connectionExpiration=60000
57
ds.propType.connectionExpiration=int
58 1 Henning Blohm
</code></pre> 
59
60 5 Henning Blohm
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:
61
62
<pre><code class="sql">
63 6 Henning Blohm
create database z2_samples;
64
grant all on z2_samples.* to z2@localhost identified by 'z2';
65 5 Henning Blohm
</code></pre>
66
67
h2. References
68 3 Henning Blohm
69
See also 
70
* "DataSourceResource":http://www.z2-environment.net/javadoc/com.zfabrik.db.data!2Fjava/impl/com/zfabrik/impl/db/data/DataSourceResource.html on how to configure data sources.
71
* "PoolingDataSource":http://www.z2-environment.net/javadoc/com.zfabrik.db.data!2Fjava/impl/com/zfabrik/impl/db/data/PoolingDataSource.html on connection pool configuration.