Project

General

Profile

How to JDBC drivers » History » Revision 4

Revision 3 (Henning Blohm, 23.09.2012 11:37) → Revision 4/10 (Henning Blohm, 23.09.2012 11:38)

h1. How to add other JDBC drivers 

 When using the built-in data source component type (see also [[How to transaction management]]), 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.  

 h2. 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. Extract the .jar file into *com.zfabrik.samples.hibernate-basic.domain/java/bin.api/lib*. 

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

 <pre><code class="xml"> 
 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> 
 </code></pre>  

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

 <pre><code class="ruby"> 
 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=<user> 
 ds.prop.password=<password> 
 ds.prop.url=jdbc:mysql://localhost:3306/<database>?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 
 </code></pre>  

 where of course the user name, password, and specifics of the JDBC URL need to be adapted. 

 See also  
 * "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. 
 * "PoolingDataSource":http://www.z2-environment.net/javadoc/com.zfabrik.db.data!2Fjava/impl/com/zfabrik/impl/db/data/PoolingDataSource.html on connection pool configuration.