Monday, 4 July 2011

Installing a MySQL Datasource on Tomcat 7

The default installation of Tomcat doesn’t come with any datasource access, so it’s down to you to set this up and it has to be done manually as there is no wizzo web frontend that’ll do it for you.

The first thing to do is to copy database driver JAR file into the Tomcat lib directory. In my case I’m using the MySQL Community Edition, so I copied mysql-connector-java-5.1.6-bin.jar to Tomcat 7.0\lib.

The next step is to create a datasource resource. If you read the documentation then there are several ways of doing this depending upon whether or not you want to make your datasource global (available to all applications on the server) or local (available only to your application). Here, I’m going to create a global resource and this is done by adding the following resource descriptor...

<Resource name="jdbc/LiveDataSource" auth="Container"
              type="javax.sql.DataSource" 
              username="mysql-user"
              password="my-password"
              driverClassName="com.mysql.jdbc.Driver" 
              url="jdbc:mysql://localhost/database-name"
              maxActive="8" 
              />

...to your server’s context.xml, to produce something like this:

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->
    
    <Resource name="jdbc/LiveDataSource" auth="Container"
              type="javax.sql.DataSource" 
              username="mysql-user"
              password="my-password"
              driverClassName="com.mysql.jdbc.Driver" 
              url="jdbc:mysql://localhost/database-name"
              maxActive="8" 
              />
</Context>

The last step is to let give your web application access to the above resource. This is done by adding the following XML resource reference to the bottom of your application’s web.xml:

<resource-ref>
      <description>Connection Pool</description>
      <res-ref-name>jdbc/LiveDataSource</res-ref-name>
      <res-type>javax.sql.Datasource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref> 

I’m assuming here that your web application is complete and ready to go, so that’s about all there is to it.

2 comments:

Franz said...

Prefer javax.sql.DataSource

with S in upper case

Unknown said...

It works for me. Thanks!