Google
 
 
Home arrow Other Articles arrow Configure JDBC DataSource in Tomcat

Main Menu
 Home
 Linux Articles
 FreeBSD Articles
 Apache Articles
 Perl Articles
 Other Articles
 Program Downloads
 Free Books
 News
 The Web Links
 Contact Us

Most Read
Automating SFTP using expect
FreeBSD PPTP VPN
Shorewall Router on Linux
Shorewall Stand Alone Firewall
SnortShorwall - Using Snort And Shorewall Together

Polls
Favorite Linux/BSD
Fedora
Mandrake
Debian
Slackware
Gentoo
Suse
FreeBSD
Other
  

Syndicate
Latest news direct to your desktop
RSS

Login Form
Username

Password

Remember me
Forgotten your password?
No account yet? Create one

Members Online
 Linux-BSD-Central Has a Total of 139 Members   Members (139) # Online
 We have 14 Guests Online. Guests 14
 We have 0 Users Online. Users 0

Online Users
No Users Online

Statistics
OS: Linux b
PHP: 5.2.5
MySQL: 5.0.45-community-log
Time: 16:37
Members: 139
Hits: 731660
News: 269
WebLinks: 15




Configure JDBC DataSource in Tomcat   PDF  Print  E-mail 
Contributed by Chad Brandt  
Thursday, 29 July 2004
In most java web application you will need to access a relational database. The most efficent way to do this is to use a connection pool datasource defined in your application server. Tomcat is package with apache commons DBCP(database connection pool) and is very easy to configure. I will go through the steps you need to setup a datasouce connection pool in Tomcat and how you would access it in your java code.

1. Create a Tomcat configuration file for you application.
The configuration file will define the context your application will be in and will also define the datasource for your application. The file should have the same name as your application, in this example I will create a web application called dbexample. The following would be my configuration file

dbexample.xml
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="dbexample" path="/dbexample">

    <Resource name="jdbc/exampleDS" type="javax.sql.DataSource" auth="Container"/>
    <ResourceParams name="jdbc/exampleDS">
        <parameter>
            <name>validationQuery</name>
            <value>select 1</value>
        </parameter>
        <parameter>
            <name>maxWait</name>
            <value>5000</value>
        </parameter>
        <parameter>
            <name>maxActive</name>
            <value>10</value>
        </parameter>
        <parameter>
            <name>username</name>
            <value>youruser</value>
        </parameter>
        <parameter>
            <name>password</name>
            <value>yourpassword</value>
        </parameter>
        <parameter>
            <name>url</name>
           <value>jdbc:postgresql://localhost:5432/exampledb</value>
        </parameter>
        <parameter>
            <name>driverClassName</name>
            <value>org.postgresql.Driver</value>
        </parameter>
        <parameter>
            <name>maxIdle</name>
            <value>2</value>
        </parameter>
        <parameter>
            <name>factory</name>
           <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
        </parameter>
    </ResourceParams>

</Context>

This will define a JDBC connection pool accessing a postgres database called exampledb. I have highlighted the values you will need to modify for your server


2. Add The resource to your applications web.xml file. This will make the DataSource available to your application


    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/exampleDS</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>



3. Access the datasource in your java code to get java.sql.Connection from

This is a simple class that accesses the datasource and uses a connection

public class DBAccess {

DataSource ds = null;

public DBAccess() throws Exception {

        try {
            Context ctx = new InitialContext();
            if (ctx == null)  throw new Exception("No Context");

            // note that we lookup java:comp/env/<name defined in configuration file>
            DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/exampleDS");

        } catch (Exception e) {
            e.printStackTrace();
        }
}


public Connection getConnection(){
    return ds.getConnection();
}

public void doSomething() throws SQLException{

    Connection con = null;

    try {
        con = getConnection();
        // do something here with the connection
    } catch (SQLException sql){
        // do exception handling here, rollback etc...
    } finally {
        // VERY IMPORTANT, ALWAYS CLOSE IN THE FINALLY
        con.close();
    }
   
}
}

In this example you can see we get our connection for the DataSource. It is very important that we do our database login in a try / catch / finally because you should always close the connection in the finally, this will get called even if an exception occurs. This does not really close the connection, but instead, puts it back in the connection pool


4.Create a war file to deploy to Tomcat.

This is usually done using apache ant, but you can do this however you know how.


5. Deploy your application.

You can do this in Tomcats management console or simple copy the war file to ${CATATLINA_HOME}/webapps and copy the configuration file to ${CATALINA_HOME}/conf/Catalina/localhost and than start tomcat up. In my example I will acces the application by going to http://myserver/dbexample

Comments
Written by Guest on 2005-07-14 10:27:35
:)
Written by Guest on 2005-08-10 06:32:11
:upset :zzz :sigh :cry :x :( :grin ;) 8) :eek :roll
Tomcat and PostgreSql
Written by Guest on 2005-09-10 01:41:25
What is tomcat version ? 
If I want to work with ProgreSql How to ?

Only registered users can write comments.
Please login or register.

Powered by AkoComment 1.0 beta 2!




 
Google Ads