Hi Tomcat Team, On Tue, Jul 21, 2015 at 4:21 PM, Konstantin Kolinko <knst.koli...@gmail.com> wrote:
> 2015-07-21 8:35 GMT+03:00 Niranjan Karunanandham <niranjan.k...@gmail.com > >: > > [sending to users list] > > > > On Thu, Jul 16, 2015 at 10:23 AM, Niranjan Karunanandham < > > niranjan.k...@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 <knst.koli...@gmail.com> wrote: > 2015-07-21 8:35 GMT+03:00 Niranjan Karunanandham <niranjan.k...@gmail.com > >: > > [sending to users list] > > > > On Thu, Jul 16, 2015 at 10:23 AM, Niranjan Karunanandham < > > niranjan.k...@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-unsubscr...@tomcat.apache.org > For additional commands, e-mail: users-h...@tomcat.apache.org > > -- *Niranjan Karunanandham*