Hibernate HOW TO: Switching between Direct JDBC and Connection Pool Datasource
Question:
How do I switch between an
Hibernate direct JDBC connection to a connection pool datasource? Or is it hard to change Hibernate to a direct connect, to be able to run stuff as a main() method versus inside a web container with datasources.
Solution:
The way you configure a JDBC Resource for Hibernate is via an hibernate.properties file below /WEB-INF/classes or in your CLASSPATH, if you're developing a standalone application.
Scenerio #1 : Direct JDBC Connection:
If you wish to use a straight JDBC connection then you define the following properties in the hibernate.properties file:
hibernate.connection.driver_class
hibernate.connection.url
hibernate.connection.username
hibernate.connection.password
hibernate.connection.pool_size
See the Hibernate reference documentation for details on the options (Chapter 4.3 JDBC Connections).
NOTE: Note from the hibernate reference documentation:
"Hibernate's own connection pooling algorithm is however quite rudimentary. It is intended to help you get started and is not intended for use in a production system or even for performance testing. You should use a third party pool for best performance and stability. Just replace the hibernate.connection.pool_size property with connection pool specific settings. This will turn off Hibernate's internal pool."
Scenerio #2 : Datasource Connection Pool:
To use a connection pool from an external verdor specify the following properties in the hibernate.properties file:
hibernate.connection.datasource
hibernate.jndi.url (optional)
hibernate.jndi.class (optional)
hibernate.connection.username(optional)
hibernate.connection.password
It's that simple switching between the two. What I have usually seen in examples is that when you are developing locally you use 'scenerio #1' and once you're done developing you switch to 'scenerio #2' and you define your JDBC resource in the JBoss-Tomcat-Catalina container (server.xml) and reference it via the hibernate.properties file.
Example of defining a JDBC resource in Server.xml under Tomcat-Catalina container
<Host name="localhost" ....>
<Context ...>
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/TestDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- Maximum number of dB connections in pool. Make sure you configure
your mysqld max_connections large enough to handle all of your db connections.
Set to 0 for no limit. -->
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<!-- Maximum number of idle dB connections to retain in pool. Set to 0 for
no limit. -->
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<!-- Maximum time to wait for a dB connection to become available in ms,
in this example 10 seconds. An Exception is thrown if this timeout is exceeded.
Set to -1 to wait indefinitely. -->
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<!-- MySQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>test</value>
</parameter>
<parameter>
<name>password</name>
<value>test</value>
</parameter>
<!-- Class name for JDBC driver -->
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<!-- Autocommit setting. This setting is required to make Hibernate work.
Or you can remove calls to commit(). -->
<parameter>
<name>defaultAutoCommit</name>
<value>true</value>
</parameter>
<!-- The JDBC connection url for connecting to your MySQL dB. The
autoReconnect=true argument to the url makes sure that the mm.mysql JDBC
Driver will automatically reconnect if mysqld closed the connection.
mysqld by default closes idle connections after 8 hours.-->
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost/venispringtestDB?autoReconnect=true&
useUnicode=true&characterEncoding=utf-8</value>
</parameter>
<!-- Recover abandoned connections -->
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
<!-- Set the number of seconds a dB connection has been idle before it
is considered abandoned. -->
<parameter>
<name>removeAbandonedTimeout</name>
<value>60</value>
</parameter>
<!-- Log a stack trace of the code which abandoned the dB connection
resources. -->
<parameter>
<name>logAbandoned</name>
<value>true</value>
</parameter>
</ResourceParams>
</Context>
</Host>
The above example is from
Matt's AppFuse generator and is used as a example to illustrate.
If you have any questions please feel free to contact us via the Aveda Technology Website.
If you found this article helpful, send me an email. Also if you know places (Java Blogs) that you can post a link to this article that would hopefully help others in locating this article.
Created By: Venkatt Guhesan at Aveda Technology, Inc.
Last Updated: Thursday, August 28, 2005.
Happy Coding!!!