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

Reply via email to