You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by The Duke <mi...@lycos.com> on 2001/11/09 15:04:56 UTC

InitialContext

I would like to know what the InitialContext is. I searched in the user docs, but could not find anything.

thnx
Dennis Knol



--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: InitialContext

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Tue, 13 Nov 2001, Marko Asplund wrote:

> Date: Tue, 13 Nov 2001 17:46:37 +0200 (EET)
> From: Marko Asplund <as...@kronodoc.fi>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> To: Tomcat Users List <to...@jakarta.apache.org>
> Subject: Re: InitialContext
>
> On Sun, 11 Nov 2001, Craig R. McClanahan wrote:
>
> > ...
> > It depends on what resource you are configuring.  See
> > <http://jakarta.apache.org/tomcat/tomcat-4.0-doc/jndi-resources-howto.html>
> > for more details.
> >
> > For the JDBC data source resource, the returned object is an instance of
> > javax.sql.DataSource -- i.e. a connection factory that you then call to
> > ask for a database connection.  The connection pool itself is provided by
> > Tomcat.
>
> sorry, but i'm still a bit confused by this. is JDBC connection pooling
> enabled by default or do i have to set it up somehow in the resource
> factory configuration?
>
> when obtaining JDBC connections do i always have to do the JNDI lookup:
>
> 	Context initCtx = new InitialContext();
> 	Context envCtx = (Context) initCtx.lookup("java:comp/env");
> 	DataSource ds = (DataSource) envCtx.lookup("jdbc/myDB");
>

For this to work, you will need to have configured a connection pool named
"java:comp/env/jdbc/myDB" in your server.xml file.  Detailed instructions
are in the Tomcat docs, or available online:

  http://jakarta.apache.org/tomcat/tomcat-4.0-doc/jndi-resources-howto.html

The info you want is in the "JDBC Data Sources" section.

> or can i just lookup and store the DataSource upon startup and have all
> threads use the same DataSource or should i do the resource lookup once
> per request? can the DataSource objects be shared?
>

The standard J2EE design pattern is to look up the data source every time,
because the container might be doing some fancy things for you under the
covers:

  Context initCtx = new InitialContext();
  Context envCtx = (Context) initCtx.lookup("java:comp/env");
  DataSource ds = (DataSource) envCtx.lookup("jdbc/myDB");
  Connection conn = ds.getConnection();
  ... use the connection ...
  conn.close(); // Returns connection to the pool

> 	aspa

Craig


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: InitialContext

Posted by Marko Asplund <as...@kronodoc.fi>.
On Sun, 11 Nov 2001, Craig R. McClanahan wrote:

> ...
> It depends on what resource you are configuring.  See
> <http://jakarta.apache.org/tomcat/tomcat-4.0-doc/jndi-resources-howto.html>
> for more details.
>
> For the JDBC data source resource, the returned object is an instance of
> javax.sql.DataSource -- i.e. a connection factory that you then call to
> ask for a database connection.  The connection pool itself is provided by
> Tomcat.

sorry, but i'm still a bit confused by this. is JDBC connection pooling
enabled by default or do i have to set it up somehow in the resource
factory configuration?

when obtaining JDBC connections do i always have to do the JNDI lookup:

	Context initCtx = new InitialContext();
	Context envCtx = (Context) initCtx.lookup("java:comp/env");
	DataSource ds = (DataSource) envCtx.lookup("jdbc/myDB");

or can i just lookup and store the DataSource upon startup and have all
threads use the same DataSource or should i do the resource lookup once
per request? can the DataSource objects be shared?

-- 
	aspa


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: InitialContext

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Sun, 11 Nov 2001, Marko Asplund wrote:

> Date: Sun, 11 Nov 2001 20:55:37 +0200 (EET)
> From: Marko Asplund <as...@kronodoc.fi>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> To: Tomcat Users List <to...@jakarta.apache.org>
> Cc: mini78@lycos.com
> Subject: Re: InitialContext
>
> On Fri, 9 Nov 2001, Craig R. McClanahan wrote:
>
> > ...
> > I assume you mean what you get when you call:
> >
> >   InitialContext context = new InitialContext();
> >
> > right?  That is a per-web-application place where the container can
> > provide services and resources (like database connection pools) to a web
> > application, which can access them in a portable manner.  The
> > InitialContext API itself is from the Java Naming and Directory Interface
> > (JNDI) APIs.
>
> what kind of functionality does the container provide exactly for the
> configured resources? does it just create an instance of the configured
> resource type and make it available in the configured context? i suppose
> the container doesn't implement any resource pooling?
>

It depends on what resource you are configuring.  See
<http://jakarta.apache.org/tomcat/tomcat-4.0-doc/jndi-resources-howto.html>
for more details.

For the JDBC data source resource, the returned object is an instance of
javax.sql.DataSource -- i.e. a connection factory that you then call to
ask for a database connection.  The connection pool itself is provided by
Tomcat.

For the Mail Session resource, you get a javax.mail.Session instance
itself -- essentially, the resource acts as a factory.  Your application
doesn't have to care whether or not the Session instance is shared,
because it doesn't matter in JavaMail.

For resources you define yourself (that are compatible with the "Generic
JavaBeans Resources" factory, it is totally up to your implementation
class whether you provide a factory object, or whether your resource acts
as a factory object.

> --
> 	aspa
>

Craig McClanahan


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: InitialContext

Posted by Marko Asplund <as...@kronodoc.fi>.
On Fri, 9 Nov 2001, Craig R. McClanahan wrote:

> ...
> I assume you mean what you get when you call:
>
>   InitialContext context = new InitialContext();
>
> right?  That is a per-web-application place where the container can
> provide services and resources (like database connection pools) to a web
> application, which can access them in a portable manner.  The
> InitialContext API itself is from the Java Naming and Directory Interface
> (JNDI) APIs.

what kind of functionality does the container provide exactly for the
configured resources? does it just create an instance of the configured
resource type and make it available in the configured context? i suppose
the container doesn't implement any resource pooling?

-- 
	aspa


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: InitialContext

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Fri, 9 Nov 2001, The Duke wrote:

> Date: Fri, 09 Nov 2001 15:04:56 +0100
> From: The Duke <mi...@lycos.com>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>,
>      mini78@lycos.com
> To: tomcat-user@jakarta.apache.org
> Subject: InitialContext
>
> I would like to know what the InitialContext is. I searched in the
> user docs, but could not find anything.
>

I assume you mean what you get when you call:

  InitialContext context = new InitialContext();

right?  That is a per-web-application place where the container can
provide services and resources (like database connection pools) to a web
application, which can access them in a portable manner.  The
InitialContext API itself is from the Java Naming and Directory Interface
(JNDI) APIs.

All J2EE servers are required to provide a naming context to each web
application, preconfigured with resources that were filled in by the
system administrator of your server.  Tomcat 4 supports a JNDI context
compatible with J2EE (except for not supporting EJBs), so that programs
you write here are portable to J2EE servers later.

For more info, you might want to browse the source code of the
"JndiExamples" class in the "/examples" webapp (it's under
/WEB-INF/classes), or consult the documentation on setting up your own
JNDI resources:

  http://jakarta.apache.org/tomcat/tomcat-4.0-doc/jndi-resources-howto.html

where, you will see, you configure the resources to be provided in the
server.xml file.

To know what kinds of resources J2EE servers are required to provide to
web apps, you might also want to read the "J2EE Platform Specification"
(version 1.3), which is available via:

  http://java.sun.com/j2ee

> thnx
> Dennis Knol
>
>

Craig McClanahan



--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>