How to JDBC drivers » History » Version 4
Henning Blohm, 23.09.2012 11:38
| 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 | 3 | 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. Extract the .jar file into *com.zfabrik.samples.hibernate-basic.domain/java/bin.api/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 | 1 | Henning Blohm | ds.prop.user=<user> |
| 45 | ds.prop.password=<password> |
||
| 46 | 3 | Henning Blohm | ds.prop.url=jdbc:mysql://localhost:3306/<database>?autoReconnect=true |
| 47 | |||
| 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 | 2 | Henning Blohm | |
| 60 | where of course the user name, password, and specifics of the JDBC URL need to be adapted. |
||
| 61 | 3 | Henning Blohm | |
| 62 | See also |
||
| 63 | * "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. |
||
| 64 | * "PoolingDataSource":http://www.z2-environment.net/javadoc/com.zfabrik.db.data!2Fjava/impl/com/zfabrik/impl/db/data/PoolingDataSource.html on connection pool configuration. |
