You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Abid Hussain <di...@googlemail.com> on 2010/12/13 17:18:44 UTC

How to configure JNDI resource

Hi all,

I've a question about how to configure a datasource to be accessed via JNDI.

I have a datasource configured in server.xml (In
Server/GlobalNamingResources/Resource).

In the documentation
http://tomcat.apache.org/tomcat-6.0-doc/config/context.html#Resource_Definitions
it says that that a resource/datasource can be declared in the context
element of a webapp like this:
<Context ...>
  ...
  <Resource name="jdbc/EmployeeDB" auth="Container" type="javax.sql.DataSource"
     description="Employees Database for HR Applications"/>
  ...
</Context>

Also it is written that it is possible to declare a resource inside web.xml:
<resource-ref>
  <description>Employees Database for HR Applications</description>
  <res-ref-name>jdbc/EmployeeDB</res-ref-name>
  <res-ref-type>javax.sql.DataSource</res-ref-type>
  <res-auth>Container</res-auth>
</resource-ref>

My question is:
1) is it enough to declare the needed resource inside web.xml or
2) do I have to declare it also in the context element
for accessing it via JNDI?

And, if 2) is correct what is the use to declare a resouce in the
webapps web.xml when it has been already declared in the context
element of the webapp?

Regards,

Abid

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


Re: How to configure JNDI resource

Posted by Mark Thomas <ma...@apache.org>.
On 13/12/2010 20:40, Christopher Schultz wrote:
> Mark,
> 
> On 12/13/2010 3:21 PM, Mark Eggers wrote:
>> In order to use the resource, you'll need to tell your application about it
>> (resource-ref in web.xml), and create it (Resource in context.xml or
>> server.xml). Where you put the creation depends on your requirements.
> 
> My experience with Tomcat 5.5 and 6.0 is that when the resource is
> defined in META-INF/context.xml, no <resource-ref> is necessary in
> web.xml. I'm not sure if that's intentional or not.

Intentional and documented.
http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html

Mark

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


Re: How to configure JNDI resource

Posted by Mark Eggers <it...@yahoo.com>.
Chris,

Weird. I guess I've just blindly followed the standards. I'll have to give it a 
try later on today.

Thanks for the info.

/mde/

Well, how about that. I just commented out the resource-ref element in web.xml 
for a Hibernate-based application and it ran fine. I have the following line in 
my hibernate.cfg.xml file:

<property name="hibernate.connection.datasource">
  java:comp/env/jdbc/jndi-name
</property>


----- Original Message ----
From: Christopher Schultz <ch...@christopherschultz.net>
To: Tomcat Users List <us...@tomcat.apache.org>
Sent: Mon, December 13, 2010 12:40:16 PM
Subject: Re: How to configure JNDI resource

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mark,

On 12/13/2010 3:21 PM, Mark Eggers wrote:
> In order to use the resource, you'll need to tell your application about it 
> (resource-ref in web.xml), and create it (Resource in context.xml or 
> server.xml). Where you put the creation depends on your requirements.

My experience with Tomcat 5.5 and 6.0 is that when the resource is
defined in META-INF/context.xml, no <resource-ref> is necessary in
web.xml. I'm not sure if that's intentional or not.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk0GhLAACgkQ9CaO5/Lv0PC3igCeP85AKX62BbcAhZUSjJiwzFJ1
CyUAnR9YlmRNZOfo8QQk4JVEbcNpHtUG
=w0dE
-----END PGP SIGNATURE-----

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


      

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


Re: How to configure JNDI resource

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mark,

On 12/13/2010 3:21 PM, Mark Eggers wrote:
> In order to use the resource, you'll need to tell your application about it 
> (resource-ref in web.xml), and create it (Resource in context.xml or 
> server.xml). Where you put the creation depends on your requirements.

My experience with Tomcat 5.5 and 6.0 is that when the resource is
defined in META-INF/context.xml, no <resource-ref> is necessary in
web.xml. I'm not sure if that's intentional or not.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk0GhLAACgkQ9CaO5/Lv0PC3igCeP85AKX62BbcAhZUSjJiwzFJ1
CyUAnR9YlmRNZOfo8QQk4JVEbcNpHtUG
=w0dE
-----END PGP SIGNATURE-----

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


Re: How to configure JNDI resource

Posted by Mark Eggers <it...@yahoo.com>.
Abid,

The resource-ref element in web.xml describes an external resource to your web 
application.

The Resource element in either context.xml or server.xml describes to Tomcat how 
to create the resource being accessed by your web application.

The first (resource-ref) is part of the standard. The second (Resource) is 
Tomcat-specific. Different containers have different ways of describing 
resources to be created.

In order to use the resource, you'll need to tell your application about it 
(resource-ref in web.xml), and create it (Resource in context.xml or 
server.xml). Where you put the creation depends on your requirements.

. . . . just my two cents.

/mde/

----- Original Message ----
From: Abid Hussain <di...@googlemail.com>
To: users@tomcat.apache.org
Sent: Mon, December 13, 2010 8:18:44 AM
Subject: How to configure JNDI resource

Hi all,

I've a question about how to configure a datasource to be accessed via JNDI.

I have a datasource configured in server.xml (In
Server/GlobalNamingResources/Resource).

In the documentation
http://tomcat.apache.org/tomcat-6.0-doc/config/context.html#Resource_Definitions
it says that that a resource/datasource can be declared in the context
element of a webapp like this:
<Context ...>
  ...
  <Resource name="jdbc/EmployeeDB" auth="Container" type="javax.sql.DataSource"
     description="Employees Database for HR Applications"/>
  ...
</Context>

Also it is written that it is possible to declare a resource inside web.xml:
<resource-ref>
  <description>Employees Database for HR Applications</description>
  <res-ref-name>jdbc/EmployeeDB</res-ref-name>
  <res-ref-type>javax.sql.DataSource</res-ref-type>
  <res-auth>Container</res-auth>
</resource-ref>

My question is:
1) is it enough to declare the needed resource inside web.xml or
2) do I have to declare it also in the context element
for accessing it via JNDI?

And, if 2) is correct what is the use to declare a resouce in the
webapps web.xml when it has been already declared in the context
element of the webapp?

Regards,

Abid

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


      

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


RE: How to configure JNDI resource

Posted by Dale Ogilvie <Da...@trimble.co.nz>.
This is how we do it in a spring app, with a resourcelink in context.xml

conf/server.xml

<GlobalNamingResources>  
  <Resource auth="Container"    
            driverClassName="oracle.jdbc.OracleDriver"    
            maxActive="10"    
            maxIdle="5"    
            maxWait="10000"    
            name="jdbc/oraclePool"    
            username="myuser"  
            password="notapassword"    
            type="javax.sql.DataSource"    
            url="jdbc:oracle:thin:@myhost.acme.com:1521:TEST"    
  />  
</GlobalNamingResources>  

META-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
  <ResourceLink name="jdbc/oraclePool" type="javax.sql.DataSource"
global="jdbc/oraclePool"/>
</Context>

WEB-INF/applicationContext.xml

<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/oraclePool" /> 
    <property name="resourceRef" value="true" />
</bean>


-----Original Message-----
From: Abid Hussain [mailto:dilax.hussain@googlemail.com] 
Sent: Tuesday, 14 December 2010 5:19 a.m.
To: users@tomcat.apache.org
Subject: How to configure JNDI resource

Hi all,

I've a question about how to configure a datasource to be accessed via
JNDI.

I have a datasource configured in server.xml (In
Server/GlobalNamingResources/Resource).

In the documentation
http://tomcat.apache.org/tomcat-6.0-doc/config/context.html#Resource_Def
initions
it says that that a resource/datasource can be declared in the context
element of a webapp like this:
<Context ...>
  ...
  <Resource name="jdbc/EmployeeDB" auth="Container"
type="javax.sql.DataSource"
     description="Employees Database for HR Applications"/>
  ...
</Context>

Also it is written that it is possible to declare a resource inside
web.xml:
<resource-ref>
  <description>Employees Database for HR Applications</description>
  <res-ref-name>jdbc/EmployeeDB</res-ref-name>
  <res-ref-type>javax.sql.DataSource</res-ref-type>
  <res-auth>Container</res-auth>
</resource-ref>

My question is:
1) is it enough to declare the needed resource inside web.xml or
2) do I have to declare it also in the context element for accessing it
via JNDI?

And, if 2) is correct what is the use to declare a resouce in the
webapps web.xml when it has been already declared in the context element
of the webapp?

Regards,

Abid

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


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