tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Smith <>
Subject Re: Dynamic Connection
Date Wed, 26 Jan 2005 22:03:18 GMT
A JNDI lookup isn't the only way to get a DataSource.  You can just call 
new on your favorite database's implementation of DataSource, set the 
appropriate attributes and cast it to javax.sql.DataSource for generic 
use.  For instance:

com.mysql.jdbc.DataSource mysqlDS = new com.mysql.jdbc.DataSource() ;
// set attributes according to MySQL's javadocs.....

javax.sql.DataSource ds = (javas.sql.DataSource)mysqlDS ;
// Do something with the datasource.

In all reality, that's essentially what tomcat is doing when it makes a 
pooled connection available.  They're just reading your parameters from 
the context.xml, instantiating a new DataSource (specifically 
BasicDataSource from the DBCP project), and putting it into a read-only 
JNDI context for you to find later.  I'm suggesting you drop the JNDI 
stuff and place a new DataSource in the session as soon as you know what 
the parameters are.


micky none wrote:

>Thanks David,
>I am already using the Datasource class to vreate a connection.For eg.
>Context ctx = (Context) init.lookup("java:comp/env");
>ds = (DataSource) ctx.lookup("jdbc/abc");
>The problem is that to know which database is selected I am trying to do exactly what
the BasicDataSource class does,if that's possible.Is there any way by which I can read the
environment variables that TOMCAT has set....please help.
>On Thu, 27 Jan 2005 David Smith wrote :
>>Sounds like you can just about totally abandon the idea of using any pooling functions
provided by DBCP.  If I were designing, I'd construct an object that implements javax.sql.DataSource
and store it in the session as soon as you know what db is being used.  Then on each request,
get the DS from the session and use it as needed.  Just be sure you only store the DS in the
session -- don't store the connections.
>>I'm sure there are other ideas out there as well.
>>micky none wrote:
>>>Hi Friends,
>>>I want to dynamically configure the database,username and password settings,instead
of putting them in conf/server.xml.In other words,I dont know beforehand which database to
>>>with which username and password.It all depends upon the person and the machine
from which it tries to make a connection.In yet another words,I just need to do what the BasicDataSource
is doing i.e get the connection details before making a connection.I am not quite sure if
you got my point,but this is what i've been asked to do and I have no clue as to where to
go or what to do.If anyone of you got my point,please help me.
>>To unsubscribe, e-mail:
>>For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message