tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tim Funk <funk...@joedog.org>
Subject Re: Question about JNDI resources
Date Mon, 17 Jan 2005 22:44:14 GMT
You should be able to store a reference to the Datasource and that should be 
all you need.

Its probably not done that way in the examples since static variables are 
frowned upon. JNDI lookups are usually almost as faster a Hash lookup. So the 
speed is fairly negligible.

-Tim

Alan Deikman wrote:

> I'm just learning this stuff, but having good success so far.  In my
> SQL-backed bean I use a JDBCResource as per the documentation:
> 
> protected static Connection getConnection() {
>        //System.out.println("User.getConnection Attempting to get
> connection");
>        try {
>            Context initContext = new InitialContext();
>            Context envContext  =
> (Context)initContext.lookup("java:/comp/env");
>            DataSource ds = (DataSource) envContext.lookup("jdbc/zzzz");
>            Connection conn = ds.getConnection();
>            return conn;
>        } catch (NamingException ne) {
>            System.out.println("User.getConnection caught Naming
> exception");
>            System.out.println(ne.toString());
>        } catch (SQLException sqle) {
>            System.out.println("User.getConnection caught SQL exception");
>            System.out.println(sqle.toString());
>        }
> 
> This works, way cool.  My question is do I need to look up two new
> Context objects and a new DataSource each time I get a connection?  Or
> can I just store initContext, envContext, and ds in static variables and
> load them just once for all instances of the class?


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message