Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

With Tomcat 5.5, the process is vastly simplified. Tomcat allows you to configure DataSources for your J2EE web application in a context.xml file that is stored in your web application project. You don't have to mess with configuring the DataSource separately in the Tomcat configuration filesserver.xml, or referencing it in your application's web.xml file. Here's how:

...

Notice that, when doing the DataSource lookup, you must prefix the JNDI name of the resource with java:comp/env/

...

Here's a sample web application project that shows where all the files go. This one shows how to access a DataSource data from from a JSP page: attachment:datasourcedemo.war

Known-Working examples for other Databases

No Format

    <Resource name="jdbc/denali" auth="Container" type="javax.sql.DataSource"
              username="denali" url="jdbc:postgresql://localhost:5432/demo"
              factory="org.apache.commons.dbcp.BasicDataSourceFactory"
              driverClassName="org.postgresql.Driver"
              maxActive="20" maxIdle="10"/>
No Format

    <Resource name="jdbc/ccsdatasource" auth="Container" type="javax.sql.DataSource"
              username="ccs" password="secret" url="jdbc:mysql://localhost:3306/ccs"
              driverClassName="com.mysql.jdbc.Driver"
              maxActive="20" maxIdle="10"/>

Please Note

This technique is Tomcat-specific. If you deploy your web application to another application server (or even an older version of Tomcat), you will need to configure a DataSource the database according to your application server's documentation, and reference it in your application's web.xml.