Tag Archive: MySql


My last post dealt with Websphere configuration for Hibernate with a Datasource for MySQL.  I also mentioned how I typically use Apache Tomcat to do a lot of my testing before deploying to Websphere.  Smart readers will know that the types of applications I am referring to do not include JMS or EJBs but are straight Servlet or Web Services type apps.  Development and Testing for full J2EE based applications should take place using other Application Servers, and or development environments.

With that said, I thought I would post the steps I went through to configure Tomcat for a Datasource, and what I had to do to configure my Hibernate configuration file for that source.

First off, I made no changes to my hibernate.cfg.xml from the one I deployed to Websphere.  So the following configuration parameters are still the same.

<property name=”current_session_context_class”>thread</property>
<property name=”hibernate.bytecode.use_reflection_optimizer”>false</property>
<property name=”hibernate.connection.datasource”>java:comp/env/jdbc/mysqlblogpostdataref</property>
<property name=”show_sql”>true</property>
<property name=”dialect”>org.hibernate.dialect.MySQLDialect</property>
<property name=”hibernate.bytecode.use_reflection_optimizer”>false</property>
<property name=”hibernate.connection.datasource”>java:comp/env/jdbc/mysqlblogpostdataref</property>
<property name=”show_sql”>true</property>
<property name=”dialect”>org.hibernate.dialect.MySQLDialect</property>

I also kept my web.xml the same as my deployment to Websphere.


<resource-ref>
<description>My Blog DataSource</description>
<res-ref-name>jdbc/mysqlblogpostdataref</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Now edit your context.xml file stored in the $CATALINA_HOME/conf directory and add a Resource entry.


<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">

<!-- maxActive: 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 -1 for no limit.
-->

<!-- maxIdle: Maximum number of idle dB connections to retain in pool.
Set to -1 for no limit.  See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
-->

<!-- maxWait: 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.
-->

<!-- username and password: MySQL dB username and password for dB connections  -->

<!-- driverClassName: Class name for the old mm.mysql JDBC driver is
org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
-->

<!-- url: The JDBC connection url for connecting to your MySQL dB.
-->

<Resource name="jdbc/mysqlblogpostdataref" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="dbuser" password="yourpassword" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/yourdatabase"/>
</Context>

The key here is to make sure your resource name in the context.xml matches your reference name in the web.xml, this is the only way Tomcat environments know how to map resources from your deployment descriptor to the context.xml.

Yes this is a bit of a tangent from my usual posts, but I like to geek out as much as the next guy, write some code, and deploy it to various Application servers.

I, as I suspect many programmers do, tend to develop my applications within Tomcat then build releases for other platforms such as WebSphere or Weblogic.   In doing so, I have come to the pretty obvious conclusion that it’s a good idea to use a Datasource references within my deployment descriptors, and allow the deployment managers to configure the AppServers for the given resources at deployment time. Over the last few days I have been struggling with how to properly configure a datasource within WebSphere and use the proper naming string within my hibernate.cfg.xml files.  I thought I would share my configuration steps in hopes that no one else has to go through this.

First You have to setup a JDBC Provider.  A JDBC provider is a JDBC driver that you can then use to connect to a database using a data source that you setup later in this example.

From the Administrative console, Open up the Resources pane, then expand JDBC.  Select JDBC Providers.

Figure 1. Depending on your scope selection, this screen shows all of the providers you have set up on your Websphere Server.

Select the Scope that you want this resource to be available at. Cell will make this resource available to all of the managed nodes and servers, but you can also make this resource available at the node or the server level.

Select New.

Figure 2. Set up your new resource.

Database Type: User Defined

Implementation Class Name: com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource

Name: That’s up to you.

Select Next, and setup your classpath to your mysql driver.

Figure 3. Setup Class Path

Hit Next then Finish.  Don’t forget to Save this setup to the master configuration.

Under the JDBC node, Select Data Providers.

Figure 4. Data Sources List

Select New.

Figure 5. New Data Source

Data Source Name: Up to you

JNDI Name: this is up to you, but the standard naming convention should be;  jdbc/datasourcerefname.  For this example we will use jdbc/mysqlblogpost.

Select Next

Figure 6. Select an Exisiting JDBC Provider

Make sure on this screen you select the JDBC provider you setup earlier.

Select Next

Figure 7. Leave the default as is.

Hit Next and then Finish.  — Remember to save this to your master configuration.

Figure 8. Your Data Source List should look something like this now.

Select the data source you just created and select the JAAS-J2C authentication data link to the side.

Figure 9. JAAS List

Select New.

Figure 9. New User/Password Reference

Hit Ok, and Save.

Go back to your Datasource and select Custom Properties.

Either set or add the following properties.

Property
Name
Type Value
databaseName java.lang.String mysqlDB
Port java.lang.String 3306
serverName java.lang.String <server-name>

After you add this..  Remember to save the changes to your master configuration.

Now lets get to your Application.

Setup a Resource Reference in your Deployment Descriptor.

When you deploy this application Websphere will ask you to map this resource.  You will be mapping it to the data source you just set up. jdbc/mysqlblogpost


<resource-ref>

<description>Your New DataSource</description>

<res-ref-name>jdbc/mysqlblogpostdataref</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

Then your Hibernate Configuration file can reference your web.xml’s reference.

Hibernate.cfg.xml
<property name="current_session_context_class">thread</property>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.datasource">java:comp/env/jdbc/mysqlblogpostdataref</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

That’s It!!  Happy Deploying!!

Follow

Get every new post delivered to your Inbox.