Johnny Kewl wrote:
> 
> ----- Original Message ----- From: "Scott Dunbar" <[EMAIL PROTECTED]>
> To: <users@tomcat.apache.org>
> Sent: Tuesday, September 30, 2008 10:22 PM
> Subject: Dynamically adding a resource to a context
> 
> 
>> Hi all,
>> I'm honestly not positive that what I want to do is a good idea but
>> the basic concept is that I want to dynamically add a Resource (a JNDI
>> JDBC DataSource) to a Context.  The idea is that I will be dynamically
>> creating databases for new users and want to have a database
>> connection pool for each user.
>>
>> I've been able to simply hand modify
>> ${catalina.home}/conf/Catalina/localhost/contextname.xml and seen that
>> I can now access the new DataSource.  What I'm looking for is a
>> programmatic way to do the same thing, short of parsing and rewriting
>> the XML myself. I've dug though the MBean classes and am honestly a
>> bit confused.
>>
>> Is there something that I'm missing?  This is Tomcat 6.0.18 under
>> Linux and/or Windows.
>>
>> Thanks for any help.

The code for the JNDI Context in Tomcat is available in various classes
here, or thereabouts:

org.apache.naming.*
org.apache.naming.factory.*
org.apache.naming.java.*
org.apache.naming.resources.*

JNDI resources are only available AFAIK (via the configured name) from
objects inside a web application Context (not the same as a JNDI
context).  Resources configured in server.xml, context.xml are available
via a different route in a Valve, though I don't think this is relevant
here.

It seems complicated to try and hook your pools into this though and I
don't know if TC will let you store an object in java:/comp/env/jdbc, so
you may prefer to try the following...

(I assume here, that you're able to configure the pool per client from a
properties file or data persisted elsewhere, so that it recreates itself
after restarts etc.)


Tomcat's connection pool is a repackaged version of the Commons Database
Connection Pool (DBCP):

http://commons.apache.org/dbcp/


The following seems like a reasonably complete tutorial on using Commons
Pool/DBCP to create and store a JNDI Datasource.  Look out for any
differences in more recent versions of Pool/DBCP.

http://today.java.net/pub/a/today/2005/11/17/app-managed-datasources-with-commons-dbcp.html


Now you only need to decide how and when to insert the pool, but a
servlet context listener would probably do.  It's a simple minimum
requirement for any user/developer to include in their app and would
allow you to code the setup/tear-down on a per user basis without
exposing that code to the user.

Downside is that it would create a pool per web-app.

Do your users have a per host or per application pool?



p




> Scott, dont think so, I think the pools are created either as Tomcat
> starts or as the servlets starts...
> I suppose you could write your own JNDI dB pool class as decribed here...
> http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html
> 
> By definition, if you giving each user their own dB... a single
> connection, its going to be some kind of user pool, not a connection
> pool...

A single database != a single connection
A user pool != a connection pool

The OP could have a server farm, and the per user database(s) be
distributed over lots of different physical servers.

> Unusual I must say... possibly a user table in a dB is a better way to
> go... but its your app.
> 
> ... this is what you need to know...
> 
> The INIT function in a servlet runs once when it starts... and its quite
> easy and possible to use a dB pool without using JNDI...
> ie many people use their own, or a third party dB pool and usually then
> dont use JNDI because it becomes a self contained war... especially in
> cases where there is an embedded dB...
> 
> In your case you would set up your user pool in INIT... use normal
> JDBC... and use it in the servlet.

This isn't the appropriate solution - as it would require the OP as
system admin to insert code into all of his users servlets.  I suspect
the choice of JNDI as a repository was aimed at avoiding this.

> Then run it against the logged in user.... or alternatively store
> connections as session cookies.... will then also need a listener...

You can't store a connection as a session cookie.

> It really depends on how hot your JDBC is ;) these pools are pretty
> smart things... and make suer you understand that a servlet is multi
> threaded...



> ---------------------------------------------------------------------------
> HARBOR : http://www.kewlstuff.co.za/index.htm
> The most powerful application server on earth.
> The only real POJO Application Server.
> See it in Action : http://www.kewlstuff.co.za/cd_tut_swf/whatisejb1.htm
> ---------------------------------------------------------------------------
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to