You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Jonathan Pierce <Jo...@seagram.com> on 2001/07/04 02:21:12 UTC

JDBC Realm Questions Tomcat 3.2.2

This may be a stupid question but I've found hundreds of messages from confused
users on the subject of JDBC Realms and Tomcat and no good explanation or
example. There is a documentation file JDBCRealm.howto but it doesn't describe
how to instantiate the datasource with a code example or what additional
property files need to be included to configure jndi properties.

Like lots of other people, I'm trying to use the release Tomcat 3.2.2, configure
multiple jdbc datasources in the server.xml file, and reference it in my
servlet. I am only interested in using a shared database ID to connect to the
database for all users of my servlet. The database connect info needs to be in
Tomcat conf files since the passwords need to be reconfigured at deployment time
outside my war file.

1. Is this behavior supported by Tomcat 3.2.2? How do I configure multiple
database datasources and instantiate them in my code?

2. Assuming I setup the datasources like below, what properties do I use to
assign a name to the datasource so I can reference it?

<RequestInterceptor 
    className="org.apache.tomcat.request.JDBCRealm" 
    debug="99" 
    driverName="oracle.jdbc.driver.OracleDriver"
    connectionURL="jdbc:oracle:thin:@ntserver:1521:ORCL"
    connectionName="scott"
    connectionPassword="tiger"
/>

<RequestInterceptor 
    className="org.apache.tomcat.request.JDBCRealm" 
    debug="99" 
    driverName="oracle.jdbc.driver.OracleDriver"
    connectionURL="jdbc:oracle:thin:@ntserver2:1521:ORCL"
    connectionName="scott"
    connectionPassword="tiger"
/>


3. What do I need to add to a config file in order to access the initial context
so I can retrieve the datasource?

import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.sql.*;

String          theDataSourceName = "???";
String          theNamingProviderURL = "???";
String          theInitialContextNamingFactory = "???";
Properties theProperties = new Properties (); 
theProperties.put("java.naming.provider.url", theNamingProviderURL);
theProperties.put("java.naming.factory.initial",theInitialContextNamingFactory);

Context theInitialContext = new InitialContext(theProperties);
DataSource theDataSource = (DataSource) theInitialContext.lookup
(theDatasourceName);

RE: JDBC Realm Questions Tomcat 3.2.2

Posted by Jonathan Pierce <jp...@nyc.rr.com>.
Craig,

Thanks for providing such a complete explanation of Realms. I was confused
about configuring data sources and connection pooling .vs. JDBC Realms for
authentication.

I'm going to try out Tomcat 4.0 and configure some datasources and access
them via jndi lookup.

I'm also planning on taking a look at using PoolMan
(http://www.codestudio.com/) with Tomcat3.2.2 since it looks like it,
like Tomcat 4.0, also lets you configure datasources in an xml file and
provides a connection pooling implementation.

Jonathan


-----Original Message-----
From: Craig R. McClanahan [mailto:craigmcc@apache.org]
Sent: Saturday, July 07, 2001 1:52 AM
To: tomcat-dev@jakarta.apache.org
Cc: jpierce@nyc.rr.com
Subject: Re: JDBC Realm Questions Tomcat 3.2.2


On Tue, 3 Jul 2001, Jonathan Pierce wrote:

> This may be a stupid question but I've found hundreds of messages from
confused
> users on the subject of JDBC Realms and Tomcat and no good explanation or
> example. There is a documentation file JDBCRealm.howto but it doesn't
describe
> how to instantiate the datasource with a code example or what additional
> property files need to be included to configure jndi properties.
>
> Like lots of other people, I'm trying to use the release Tomcat 3.2.2,
configure
> multiple jdbc datasources in the server.xml file, and reference it in my
> servlet. I am only interested in using a shared database ID to connect to
the
> database for all users of my servlet. The database connect info needs to
be in
> Tomcat conf files since the passwords need to be reconfigured at
deployment time
> outside my war file.
>
> 1. Is this behavior supported by Tomcat 3.2.2? How do I configure multiple
> database datasources and instantiate them in my code?
>

IIRC, Tomcat 3.2.2 only supports a single realm of usernames and passwords
for all of the webapps running in a single JVM.  That is not the case with
4.0.

> 2. Assuming I setup the datasources like below, what properties do I use
to
> assign a name to the datasource so I can reference it?
>
> <RequestInterceptor
>     className="org.apache.tomcat.request.JDBCRealm"
>     debug="99"
>     driverName="oracle.jdbc.driver.OracleDriver"
>     connectionURL="jdbc:oracle:thin:@ntserver:1521:ORCL"
>     connectionName="scott"
>     connectionPassword="tiger"
> />
>
> <RequestInterceptor
>     className="org.apache.tomcat.request.JDBCRealm"
>     debug="99"
>     driverName="oracle.jdbc.driver.OracleDriver"
>     connectionURL="jdbc:oracle:thin:@ntserver2:1521:ORCL"
>     connectionName="scott"
>     connectionPassword="tiger"
> />
>
>
> 3. What do I need to add to a config file in order to access the initial
context
> so I can retrieve the datasource?
>
> import javax.servlet.*;
> import javax.servlet.http.*;
> import javax.naming.*;
> import javax.sql.*;
>
> String          theDataSourceName = "???";
> String          theNamingProviderURL = "???";
> String          theInitialContextNamingFactory = "???";
> Properties theProperties = new Properties ();
> theProperties.put("java.naming.provider.url", theNamingProviderURL);
>
theProperties.put("java.naming.factory.initial",theInitialContextNamingFacto
ry);
>
> Context theInitialContext = new InitialContext(theProperties);
> DataSource theDataSource = (DataSource) theInitialContext.lookup
> (theDatasourceName);
>

You might be confusing realms with datasources :-)

The purpose of JDBCRealm is to allow you to store the usernames,
passwords, and roles required for container-managed security (as described
in the servlet spec) in a database accessed via JDBC.  When used in this
manner, only Tomcat talks to the database that you configure with
JDBCRealm -- not your application.

If you want to use a datasource for your own application data, you don't
need JDBCRealm at all.  Instead, you've got a couple choices:

* Use one of the existing datasource implementations, following the
  configuration documentation included with that datasource.

* Use a J2EE application server, where you can declare a
  <resource-ref> in the web.xml file, and use your app server's
  deployment tools to connect that to an actual database.

* Use Tomcat 4.0, which lets you do the same thing -- the connection
  to a particular database is done in the "conf/server.xml" file.

Craig McClanahan



Re: JDBC Realm Questions Tomcat 3.2.2

Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Tue, 3 Jul 2001, Jonathan Pierce wrote:

> This may be a stupid question but I've found hundreds of messages from confused
> users on the subject of JDBC Realms and Tomcat and no good explanation or
> example. There is a documentation file JDBCRealm.howto but it doesn't describe
> how to instantiate the datasource with a code example or what additional
> property files need to be included to configure jndi properties.
> 
> Like lots of other people, I'm trying to use the release Tomcat 3.2.2, configure
> multiple jdbc datasources in the server.xml file, and reference it in my
> servlet. I am only interested in using a shared database ID to connect to the
> database for all users of my servlet. The database connect info needs to be in
> Tomcat conf files since the passwords need to be reconfigured at deployment time
> outside my war file.
> 
> 1. Is this behavior supported by Tomcat 3.2.2? How do I configure multiple
> database datasources and instantiate them in my code?
> 

IIRC, Tomcat 3.2.2 only supports a single realm of usernames and passwords
for all of the webapps running in a single JVM.  That is not the case with
4.0.

> 2. Assuming I setup the datasources like below, what properties do I use to
> assign a name to the datasource so I can reference it?
> 
> <RequestInterceptor 
>     className="org.apache.tomcat.request.JDBCRealm" 
>     debug="99" 
>     driverName="oracle.jdbc.driver.OracleDriver"
>     connectionURL="jdbc:oracle:thin:@ntserver:1521:ORCL"
>     connectionName="scott"
>     connectionPassword="tiger"
> />
> 
> <RequestInterceptor 
>     className="org.apache.tomcat.request.JDBCRealm" 
>     debug="99" 
>     driverName="oracle.jdbc.driver.OracleDriver"
>     connectionURL="jdbc:oracle:thin:@ntserver2:1521:ORCL"
>     connectionName="scott"
>     connectionPassword="tiger"
> />
> 
> 
> 3. What do I need to add to a config file in order to access the initial context
> so I can retrieve the datasource?
> 
> import javax.servlet.*;
> import javax.servlet.http.*;
> import javax.naming.*;
> import javax.sql.*;
> 
> String          theDataSourceName = "???";
> String          theNamingProviderURL = "???";
> String          theInitialContextNamingFactory = "???";
> Properties theProperties = new Properties (); 
> theProperties.put("java.naming.provider.url", theNamingProviderURL);
> theProperties.put("java.naming.factory.initial",theInitialContextNamingFactory);
> 
> Context theInitialContext = new InitialContext(theProperties);
> DataSource theDataSource = (DataSource) theInitialContext.lookup
> (theDatasourceName);
> 

You might be confusing realms with datasources :-)

The purpose of JDBCRealm is to allow you to store the usernames,
passwords, and roles required for container-managed security (as described
in the servlet spec) in a database accessed via JDBC.  When used in this
manner, only Tomcat talks to the database that you configure with
JDBCRealm -- not your application.

If you want to use a datasource for your own application data, you don't
need JDBCRealm at all.  Instead, you've got a couple choices:

* Use one of the existing datasource implementations, following the
  configuration documentation included with that datasource.

* Use a J2EE application server, where you can declare a
  <resource-ref> in the web.xml file, and use your app server's
  deployment tools to connect that to an actual database.

* Use Tomcat 4.0, which lets you do the same thing -- the connection
  to a particular database is done in the "conf/server.xml" file.

Craig McClanahan