You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Scott Dunbar <sc...@xigole.com> on 2008/09/30 22:22:15 UTC

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.

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


Re: Dynamically adding a resource to a context

Posted by Johnny Kewl <jo...@kewlstuff.co.za>.
----- Original Message ----- 
From: "Pid" <p...@pidster.com>
To: "Tomcat Users List" <us...@tomcat.apache.org>
Sent: Wednesday, October 01, 2008 10:41 PM
Subject: Re: Dynamically adding a resource to a context


> Johnny Kewl wrote:
>>
>> ----- Original Message ----- From: "Pid" <p...@pidster.com>
>> To: "Tomcat Users List" <us...@tomcat.apache.org>
>> Sent: Wednesday, October 01, 2008 11:50 AM
>> Subject: Re: Dynamically adding a resource to a context
>>
>>
>>
>>> http://today.java.net/pub/a/today/2005/11/17/app-managed-datasources-with-commons-dbcp.html
>>>
>
> At the risk of veering wildly off topic...
>
>> Thanks PID... nice article... would like to have seen some code on JNDI
>> for dropping a pool and making another one...
>
> I don't understand what you mean, you want to remove a configured pool &
> replace it with a new one?
>
>> ... wonder if anyone has tried this stuff on TC?
>
> JNDI (in one sense) is just a convenient store for useful objects - like
> DB connection pools.  Many app Contexts can share access to one pool
> this way.
>
> So, really, there's no "with JNDI is XXX true", as once you've retrieved
> the DataSource you have access to a javax.sql.Connection object as normal.
>
>> A QU...
>> With JNDI if you have a dB with a lot of access control... like say
>>    Mr X can access table 1,2,3,4 but not 5,6,7 etc
>>
>> Do you set up a connection pool for each role? ... using JNDI?
>
> A DB connection pool needs credentials configured in the DB, so
> whichever roles that user has are assigned to the pool.
> Ergo, one pool per DB user.
>
>> Or does the servlet typically do access control and the dB pool is
>> effectively admin access?
>
> Access control for what? The DB or web app?  Don't confuse DB
> credentials with application credentials.
>
>> Wondering how you handle this stuff using JNDI... actually wondering how
>> many veteran coders use JNDI for dB access?
>
> Again, JNDI is just a convenient place to store a connection pool.
> Q: Is a connection pool faster than creating a new connection each time
> you need one?
> A: Yes, usually way faster, so most 'veterans' will probably use one in
> a web application.
>
>> Allowing users to change passwords and that sort of stuff seems near
>> impossible with JNDI... just wondering?
>
> Sounds like you're confusing DB credentials with app credentials.
> The Connection retrieved from the pool stored in JNDI is usable just
> like any other Connection object.
>
> I don't create a new DB user account for every user on my system, but I
> do store their web app credentials in the DB.
>
> The *DB* user account that I have configured in the DataSourceRealm
> (which has a small connection pool) has read only access to the tables
> needed to get the user, do auth & get roles.
>
> Other DB connection pools have write access and permit updates to data
> in tables, e.g. updating user passwords, via a servlet which retrieves
> the pool from JNDI.

Ok thanks 4 explaining how you do it....

---------------------------------------------------------------------------
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: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Dynamically adding a resource to a context

Posted by Pid <p...@pidster.com>.
Johnny Kewl wrote:
> 
> ----- Original Message ----- From: "Pid" <p...@pidster.com>
> To: "Tomcat Users List" <us...@tomcat.apache.org>
> Sent: Wednesday, October 01, 2008 11:50 AM
> Subject: Re: Dynamically adding a resource to a context
> 
> 
> 
>> http://today.java.net/pub/a/today/2005/11/17/app-managed-datasources-with-commons-dbcp.html
>>

At the risk of veering wildly off topic...

> Thanks PID... nice article... would like to have seen some code on JNDI
> for dropping a pool and making another one...

I don't understand what you mean, you want to remove a configured pool &
replace it with a new one?

> ... wonder if anyone has tried this stuff on TC?

JNDI (in one sense) is just a convenient store for useful objects - like
DB connection pools.  Many app Contexts can share access to one pool
this way.

So, really, there's no "with JNDI is XXX true", as once you've retrieved
the DataSource you have access to a javax.sql.Connection object as normal.

> A QU...
> With JNDI if you have a dB with a lot of access control... like say
>    Mr X can access table 1,2,3,4 but not 5,6,7 etc
>
> Do you set up a connection pool for each role? ... using JNDI?

A DB connection pool needs credentials configured in the DB, so
whichever roles that user has are assigned to the pool.
Ergo, one pool per DB user.

> Or does the servlet typically do access control and the dB pool is
> effectively admin access?

Access control for what? The DB or web app?  Don't confuse DB
credentials with application credentials.

> Wondering how you handle this stuff using JNDI... actually wondering how
> many veteran coders use JNDI for dB access?

Again, JNDI is just a convenient place to store a connection pool.
Q: Is a connection pool faster than creating a new connection each time
you need one?
A: Yes, usually way faster, so most 'veterans' will probably use one in
a web application.

> Allowing users to change passwords and that sort of stuff seems near
> impossible with JNDI... just wondering?

Sounds like you're confusing DB credentials with app credentials.
The Connection retrieved from the pool stored in JNDI is usable just
like any other Connection object.

I don't create a new DB user account for every user on my system, but I
do store their web app credentials in the DB.

The *DB* user account that I have configured in the DataSourceRealm
(which has a small connection pool) has read only access to the tables
needed to get the user, do auth & get roles.

Other DB connection pools have write access and permit updates to data
in tables, e.g. updating user passwords, via a servlet which retrieves
the pool from JNDI.

p


> ---------------------------------------------------------------------------
> 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: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 


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


Re: Dynamically adding a resource to a context

Posted by Johnny Kewl <jo...@kewlstuff.co.za>.
----- Original Message ----- 
From: "Pid" <p...@pidster.com>
To: "Tomcat Users List" <us...@tomcat.apache.org>
Sent: Wednesday, October 01, 2008 11:50 AM
Subject: Re: Dynamically adding a resource to a context



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

Thanks PID... nice article... would like to have seen some code on JNDI for 
dropping a pool and making another one...
... wonder if anyone has tried this stuff on TC?

A QU...
With JNDI if you have a dB with a lot of access control... like say
    Mr X can access table 1,2,3,4 but not 5,6,7 etc

Do you set up a connection pool for each role? ... using JNDI?
Or does the servlet typically do access control and the dB pool is 
effectively admin access?

Wondering how you handle this stuff using JNDI... actually wondering how 
many veteran coders use JNDI for dB access?
Allowing users to change passwords and that sort of stuff seems near 
impossible with JNDI... just wondering?

---------------------------------------------------------------------------
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: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Dynamically adding a resource to a context

Posted by Pid <p...@pidster.com>.
Johnny Kewl wrote:
> 
> ----- Original Message ----- From: "Scott Dunbar" <sc...@xigole.com>
> To: <us...@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: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 


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


Re: Dynamically adding a resource to a context

Posted by Johnny Kewl <jo...@kewlstuff.co.za>.
----- Original Message ----- 
From: "Scott Dunbar" <sc...@xigole.com>
To: <us...@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.

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...
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.
Then run it against the logged in user.... or alternatively store 
connections as session cookies.... will then also need a listener...

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...

Good Luck...

---------------------------------------------------------------------------
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: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org