You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Niranjan Karunanandham <ni...@gmail.com> on 2015/07/21 07:35:07 UTC

Re: Tomcat initialize SelectorContext twice when lookup for JNDI defined in web application (META-INF/context.xml)

[sending to users list]

On Thu, Jul 16, 2015 at 10:23 AM, Niranjan Karunanandham <
niranjan.karu@gmail.com> wrote:

> Hi,
>
> On debugging Tomcat (7.0.59), I noticed that the SelectorContext is
> initialized twice when a lookup is performed for JNDI defined in web
> application (META-INF/context.xml).
> When the lookup is performed, the Servlet first calls the init method of
> InitailContext and this "returns new SelectorContext(env, true)". Then it
> calls the lookup method of InitialContext which again initializes the
> SelectorContext but now it "returns new SelectorContext(env)" [where the
> SelectorContextor.initialContext is set to *false*] and then the lookup
> is performed.
>
> Why is tomcat initializing the SelectorContext twice here?
>
> My Java Webapp Code which does the lookup:
> *initCtx = new InitialContext();*
> *Context envContext = (Context) initCtx.lookup("java:comp/env");*
> *DataSource dataSource = (DataSource) envContext.lookup("*jdbc/contextDB
> *");*
>
> Resource defined in META-INF/context.xml in webapp:
>
> <Resource name="jdbc/contextDB" auth="Container" type
> ="javax.sql.DataSource"
> maxActive="100" maxIdle="30" maxWait="10000"
> username="user" password="user123" driverClassName="com.mysql.jdbc.Driver"
>
>           url="jdbc:mysql://localhost:3306/WebAppTestDB"/>
>
>
> Regards,
>
> *Niranjan Karunanandham*
>



-- 
*Niranjan Karunanandham*

Re: Tomcat initialize SelectorContext twice when lookup for JNDI defined in web application (META-INF/context.xml)

Posted by Niranjan Karunanandham <ni...@gmail.com>.
Hi all

On Tue, Jul 21, 2015 at 7:27 PM, Niranjan Karunanandham <
niranjan.karu@gmail.com> wrote:

> Hi Tomcat Team,
>
> On Tue, Jul 21, 2015 at 4:21 PM, Konstantin Kolinko <
> knst.kolinko@gmail.com> wrote:
>
>> 2015-07-21 8:35 GMT+03:00 Niranjan Karunanandham <niranjan.karu@gmail.com
>> >:
>> > [sending to users list]
>> >
>> > On Thu, Jul 16, 2015 at 10:23 AM, Niranjan Karunanandham <
>> > niranjan.karu@gmail.com> wrote:
>> >
>> >> Hi,
>> >>
>> >> On debugging Tomcat (7.0.59), I noticed that the SelectorContext is
>> >> initialized twice when a lookup is performed for JNDI defined in web
>> >> application (META-INF/context.xml).
>> >> When the lookup is performed, the Servlet first calls the init method
>> of
>> >> InitailContext and this "returns new SelectorContext(env, true)". Then
>> it
>> >> calls the lookup method of InitialContext which again initializes the
>> >> SelectorContext but now it "returns new SelectorContext(env)" [where
>> the
>> >> SelectorContextor.initialContext is set to *false*] and then the lookup
>> >> is performed.
>>
>> 1. javax.naming.InitialContext class is not ours. It is part of JDK.
>>
>> 2. SelectorContext object is immutable. The first one has
>> "initialContext == true", the second one has "false". Those are two
>> different objects.
>>
> Why are there two different SelectorContext object being created. I
> noticed that when we do a JNDI lookup from webapp, it requires the
> Context.xml to be only read-only (please correct me if am wrong) for which
> the initialContext is false for the SelectorContext. So when is the first
> SelectorContext (where the initialContext is set to true) use?
>

On further debugging, I noticed that when JNDI lookup is performed as
mentioned in my sample, it was creating a new SelectorContext which the
initialContext as false. On trying to connect to a queue (apacheMQ), it
does not create new SelectorContext. Why is this so or am I missing
something?

This is the code (in NamingManager) which does this:





















*private static Object getURLObject(String scheme, Object urlInfo,
                              Name name, Context nameCtx,
                     Hashtable environment)        throws
NamingException {    // e.g. "ftpURLContextFactory"    ObjectFactory
factory = (ObjectFactory)ResourceManager.getFactory(
Context.URL_PKG_PREFIXES, environment, nameCtx,        "." + scheme +
"." + scheme + "URLContextFactory", defaultPkgPrefix);    if (factory
== null)      return null;    // Found object factory    try {
return factory.getObjectInstance(urlInfo, name, nameCtx, environment);
   } catch (NamingException e) {        throw e;    } catch (Exception
e) {        NamingException ne = new NamingException();
ne.setRootCause(e);        throw ne;    }}*



> 3. It is usually better to lookup the DataSource once (at startup time
>> /first access time) and cache the reference locally.
>>
>> >> Why is tomcat initializing the SelectorContext twice here?
>> >>
>> >> My Java Webapp Code which does the lookup:
>> >> *initCtx = new InitialContext();*
>> >> *Context envContext = (Context) initCtx.lookup("java:comp/env");*
>> >> *DataSource dataSource = (DataSource)
>> envContext.lookup("*jdbc/contextDB
>> >> *");*
>> >>
>> >> Resource defined in META-INF/context.xml in webapp:
>> >>
>> >> <Resource name="jdbc/contextDB" auth="Container" type
>> >> ="javax.sql.DataSource"
>> >> maxActive="100" maxIdle="30" maxWait="10000"
>> >> username="user" password="user123"
>> driverClassName="com.mysql.jdbc.Driver"
>> >>
>> >>           url="jdbc:mysql://localhost:3306/WebAppTestDB"/>
>> >>
>
>
Regards,
Nira

Re: Tomcat initialize SelectorContext twice when lookup for JNDI defined in web application (META-INF/context.xml)

Posted by Niranjan Karunanandham <ni...@gmail.com>.
Hi Tomcat Team,

On Tue, Jul 21, 2015 at 4:21 PM, Konstantin Kolinko <kn...@gmail.com>
 wrote:

> 2015-07-21 8:35 GMT+03:00 Niranjan Karunanandham <niranjan.karu@gmail.com
> >:
> > [sending to users list]
> >
> > On Thu, Jul 16, 2015 at 10:23 AM, Niranjan Karunanandham <
> > niranjan.karu@gmail.com> wrote:
> >
> >> Hi,
> >>
> >> On debugging Tomcat (7.0.59), I noticed that the SelectorContext is
> >> initialized twice when a lookup is performed for JNDI defined in web
> >> application (META-INF/context.xml).
> >> When the lookup is performed, the Servlet first calls the init method of
> >> InitailContext and this "returns new SelectorContext(env, true)". Then
> it
> >> calls the lookup method of InitialContext which again initializes the
> >> SelectorContext but now it "returns new SelectorContext(env)" [where the
> >> SelectorContextor.initialContext is set to *false*] and then the lookup
> >> is performed.
>
> 1. javax.naming.InitialContext class is not ours. It is part of JDK.
>
> 2. SelectorContext object is immutable. The first one has
> "initialContext == true", the second one has "false". Those are two
> different objects.
>
Why are there two different SelectorContext object being created. I noticed
that when we do a JNDI lookup from webapp, it requires the Context.xml to
be only read-only (please correct me if am wrong) for which the
initialContext is false for the SelectorContext. So when is the first
SelectorContext (where the initialContext is set to true) use?



> 3. It is usually better to lookup the DataSource once (at startup time
> /first access time) and cache the reference locally.
>
> >> Why is tomcat initializing the SelectorContext twice here?
> >>
> >> My Java Webapp Code which does the lookup:
> >> *initCtx = new InitialContext();*
> >> *Context envContext = (Context) initCtx.lookup("java:comp/env");*
> >> *DataSource dataSource = (DataSource) envContext.lookup("*jdbc/contextDB
> >> *");*
> >>
> >> Resource defined in META-INF/context.xml in webapp:
> >>
> >> <Resource name="jdbc/contextDB" auth="Container" type
> >> ="javax.sql.DataSource"
> >> maxActive="100" maxIdle="30" maxWait="10000"
> >> username="user" password="user123"
> driverClassName="com.mysql.jdbc.Driver"
> >>
> >>           url="jdbc:mysql://localhost:3306/WebAppTestDB"/>
> >>
>

Regards,
Nira

On Tue, Jul 21, 2015 at 4:21 PM, Konstantin Kolinko <kn...@gmail.com>
wrote:

> 2015-07-21 8:35 GMT+03:00 Niranjan Karunanandham <niranjan.karu@gmail.com
> >:
> > [sending to users list]
> >
> > On Thu, Jul 16, 2015 at 10:23 AM, Niranjan Karunanandham <
> > niranjan.karu@gmail.com> wrote:
> >
> >> Hi,
> >>
> >> On debugging Tomcat (7.0.59), I noticed that the SelectorContext is
> >> initialized twice when a lookup is performed for JNDI defined in web
> >> application (META-INF/context.xml).
> >> When the lookup is performed, the Servlet first calls the init method of
> >> InitailContext and this "returns new SelectorContext(env, true)". Then
> it
> >> calls the lookup method of InitialContext which again initializes the
> >> SelectorContext but now it "returns new SelectorContext(env)" [where the
> >> SelectorContextor.initialContext is set to *false*] and then the lookup
> >> is performed.
>
> 1. javax.naming.InitialContext class is not ours. It is part of JDK.
>
> 2. SelectorContext object is immutable. The first one has
> "initialContext == true", the second one has "false". Those are two
> different objects.
>
> 3. It is usually better to lookup the DataSource once (at startup time
> /first access time) and cache the reference locally.
>
> >> Why is tomcat initializing the SelectorContext twice here?
> >>
> >> My Java Webapp Code which does the lookup:
> >> *initCtx = new InitialContext();*
> >> *Context envContext = (Context) initCtx.lookup("java:comp/env");*
> >> *DataSource dataSource = (DataSource) envContext.lookup("*jdbc/contextDB
> >> *");*
> >>
> >> Resource defined in META-INF/context.xml in webapp:
> >>
> >> <Resource name="jdbc/contextDB" auth="Container" type
> >> ="javax.sql.DataSource"
> >> maxActive="100" maxIdle="30" maxWait="10000"
> >> username="user" password="user123"
> driverClassName="com.mysql.jdbc.Driver"
> >>
> >>           url="jdbc:mysql://localhost:3306/WebAppTestDB"/>
> >>
> >
> > --
> > *Niranjan Karunanandham*
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


-- 
*Niranjan Karunanandham*

Re: Tomcat initialize SelectorContext twice when lookup for JNDI defined in web application (META-INF/context.xml)

Posted by Konstantin Kolinko <kn...@gmail.com>.
2015-07-21 8:35 GMT+03:00 Niranjan Karunanandham <ni...@gmail.com>:
> [sending to users list]
>
> On Thu, Jul 16, 2015 at 10:23 AM, Niranjan Karunanandham <
> niranjan.karu@gmail.com> wrote:
>
>> Hi,
>>
>> On debugging Tomcat (7.0.59), I noticed that the SelectorContext is
>> initialized twice when a lookup is performed for JNDI defined in web
>> application (META-INF/context.xml).
>> When the lookup is performed, the Servlet first calls the init method of
>> InitailContext and this "returns new SelectorContext(env, true)". Then it
>> calls the lookup method of InitialContext which again initializes the
>> SelectorContext but now it "returns new SelectorContext(env)" [where the
>> SelectorContextor.initialContext is set to *false*] and then the lookup
>> is performed.

1. javax.naming.InitialContext class is not ours. It is part of JDK.

2. SelectorContext object is immutable. The first one has
"initialContext == true", the second one has "false". Those are two
different objects.

3. It is usually better to lookup the DataSource once (at startup time
/first access time) and cache the reference locally.

>> Why is tomcat initializing the SelectorContext twice here?
>>
>> My Java Webapp Code which does the lookup:
>> *initCtx = new InitialContext();*
>> *Context envContext = (Context) initCtx.lookup("java:comp/env");*
>> *DataSource dataSource = (DataSource) envContext.lookup("*jdbc/contextDB
>> *");*
>>
>> Resource defined in META-INF/context.xml in webapp:
>>
>> <Resource name="jdbc/contextDB" auth="Container" type
>> ="javax.sql.DataSource"
>> maxActive="100" maxIdle="30" maxWait="10000"
>> username="user" password="user123" driverClassName="com.mysql.jdbc.Driver"
>>
>>           url="jdbc:mysql://localhost:3306/WebAppTestDB"/>
>>
>
> --
> *Niranjan Karunanandham*

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