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!!