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 file should be copied to ${catalina_home}/conf/Catalina/localhost. 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" auth="Container"
type="javax.sql.DataSource" username="user" password="password"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/exampledb"
maxActive="8" maxIdle="4" factory="org.apache.commons.dbcp.BasicDataSourceFactory"/>
<-- Uncomment if you want to use Datasource Realm Security -->
<Realm className="org.apache.catalina.realm.DataSourceRealm"
debug="0"
dataSourceName="jdbc/exampleDS"
localDataSource="true"
userTable="user_table"
userNameCol="username"
userCredCol="password"
userRoleTable="user_role_table"
roleNameCol="rolename"/>
</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
If you are using spring you create the dataSource bean like this
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/exampleDS</value>
</property>
</bean>
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 |
Written by Guest on 2005-07-14 10:27:35 | Written by Guest on 2005-08-10 06:32:11 | 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 ? |
Powered by AkoComment 1.0 beta 2! |