Peter Harrison wrote:
On Wed, 03 Dec 2003 16:18, Todd O'Bryan wrote:


How do people handle this elegantly? The requirements are: a single,
globally visible (within a webapp) database interface and the ability
to access multiple databases easily.


The first point is to use a singleton to set up the database connection - or more correctly the connection pool. This way you can request a connection and return it to the pool easily. Of course every time you use one you will have to use try-catch blocks. Sorry no way around that.

Both Tomcat and J2EE specification support javax.sql.DataSource objects over JNDI. That is how we handle it elegantly.


A DataSource object is specified by the administrator and created by the container. Container then deploys it under specified JNDI name. A servlet (or EJB) can then lookup this object and use it, something like this:

import java.sql.*;
import javax.sql.*;
import javax.naming.*;

InitialContext ic = new InitialContext();
DataSource ds = (DataSource)ic.lookup( "java:comp/env/jdbc/MyDataSource" );
Connection conn = ds.getConnection();

Nix.


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to