You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Priyam Srivastava <pr...@gmail.com> on 2018/03/16 12:06:13 UTC

Tomcat's data-source issue with Fork Join Tasks

I have a scenario where we have to run some random number of independent
tasks to load data from DB. So I am using Java's fork Join framework to
create those task and then invoke them.

Each task opens its own connection using datasource and closes it.

But in Tomcat, I am getting below error at line:

initialContext = new InitialContext();

javax.naming.NoInitialContextException: Cannot instantiate class:
org.apache.naming.java.javaURLContextFactory
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
~[?:1.8.0_161]
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
~[?:1.8.0_161]
at javax.naming.InitialContext.init(Unknown Source) ~[?:1.8.0_161]
at javax.naming.InitialContext.<init>(Unknown Source) ~[?:1.8.0_161]
at com.dummy.test.TestClass.compute(TestClass.java:71) [classes/:?]
at java.util.concurrent.RecursiveAction.exec(Unknown Source) [?:1.8.0_161]
at java.util.concurrent.ForkJoinTask.doExec(Unknown Source) [?:1.8.0_161]
at java.util.concurrent.ForkJoinPool$WorkQueue.execLocalTasks(Unknown
Source) [?:1.8.0_161]
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(Unknown Source)
[?:1.8.0_161]
at java.util.concurrent.ForkJoinPool.runWorker(Unknown Source) [?:1.8.0_161]
at java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
[?:1.8.0_161]
Caused by: java.lang.ClassNotFoundException:
org.apache.naming.java.javaURLContextFactory

This error seems to be coming only in Tomcat and when I run the same code
in Wildfly/Glassfish or JBOSS EAP, everything works fine.

On the other hand if I change my code and run these tasks using Thread
instead of Fork Join framework, I don't face this issue in Tomcat.

So why this error is coming in Tomcat only?

Note: I am getting this error after deploying in Tomcat and hitting app URL
from Postman. The so called missing class is already there in
jar catalina.jar present inside <Tomcat_Home>/lib

Environment Details:

Java Version: 1.8
Tomcat Version: 8.5, 9.0.6
OS: Windows 10 Pro 64 bit
Database: Oracle 11g and MySQL 5.7

I have uploaded a dummy code to simulate this issue in Git. Please refer to
the readme.txt for full details there.

Git URL:
https://github.com/wambling/my-project.git

Regards,
Priyam

Re: Tomcat's data-source issue with Fork Join Tasks

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

Priyam,

On 3/17/18 12:10 AM, Priyam Srivastava wrote:
> Many Thanks for your response. I was able to resolve this issue by
> writing the below code just before JNDI Look Up:
> 
> Thread l_thread = Thread.currentThread(); 
> l_thread.setContextClassLoader(this.getClass().getClassLoader()); 
> initialContext = new InitialContext(); ....

Don't forget to "pop" the TCCL in a finally block after you do your
work, or you may potentially confuse some code later down the pipeline.

Tomcat will likely recover *its* own state so if control transfers
back to Tomcat right away, you might not notice any problems.

But it's a good practice (and essential in many situations) to restore
the state of the TCCL if you are going to be making changes to it.

- -chris
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAlqtjIsACgkQHPApP6U8
pFgD/Q//UZQuNPk4368jDu6o2nszk7aqEQ2KSNsKkbVPYpptmBMzNX9K2iJ+I4Jd
h2BgVXcH7hMu0lLtEiV67Ml57XMfWeySJy9kbrSnzlys9h0EETmmoPxS7ckdFZ/5
CB76l8xNYA/jN7w7XyLAyRRjP9SUdN6eBwcXuZHILNq1lM6Y8KomqT/TvBAwpr69
NFoL47qPnwktu+m60U3nlRZ6tqQft8NoJkGUp12bqlx3oWt5RybsHJJfjDcvWxXG
jgntbqwjzRVwNANAkPL1NbwHKdZvMzozGxcsXazL1JQ/GtIfJ20lxPSqbe9f+dq9
wqJE7wn5gVy/Hgx2wMqcGMY3dHfch0bjBvX5/QpaVNTKGrsTAIw7lHt8eP4TDi6b
1syMIJ9kCe89fm01DxGnODlDxP2yKidQW709uzZ+kFRBB7aNhntO6JPtYRLjKGNX
3lB/PuQ1oF7GBPh8rjibee3bkMtcPDnOX5tK48xKXb7cpdFbb7eHK5kDevLb+hvJ
wzOS51JYnrTaKsom3Mn5q54GO1r/IY09uc/wT7uIAlirdEISs/OR1904V+L/4nAq
4KjT1mEEjwhgvssELMwPEw498hxngwex/88HlC52v3hevDexkE6CNSvV9Tbl9Qhq
Nqyk788uhmUbWmAqcri4wi3+cfCxwaxHRScv8tGlJP5Ubz951Z8=
=9ar9
-----END PGP SIGNATURE-----

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


Re: Tomcat's data-source issue with Fork Join Tasks

Posted by Mark Thomas <ma...@apache.org>.
On 17/03/18 04:10, Priyam Srivastava wrote:
> Hi Mark,
> 
> Many Thanks for your response. I was able to resolve this issue by writing
> the below code just before JNDI Look Up:
> 
> Thread l_thread = Thread.currentThread();
> l_thread.setContextClassLoader(this.getClass().getClassLoader());
> initialContext = new InitialContext();
> ....
> 
> 
> I have a questions based on the discussions on the thread link you provided:
> 
> If this is something to do with ForkJoin Pool, then why it is happening
> only in Tomcat why not in other application servers as I mentioned in my
> initial post.

Because of the way Tomcat implements the memory leak protection for the
problem described in bug 60620.

I can't speak for the other containers as I don't know how their
internals are coded.

Mark


> 
> Regards,
> Priyam
> 
> On Sat, Mar 17, 2018 at 2:22 AM, Mark Thomas <ma...@apache.org> wrote:
> 
>> On 16/03/18 12:06, Priyam Srivastava wrote:
>>> I have a scenario where we have to run some random number of independent
>>> tasks to load data from DB. So I am using Java's fork Join framework to
>>> create those task and then invoke them.
>>
>> See:
>> https://bz.apache.org/bugzilla/show_bug.cgi?id=60620 and the various
>> threads linked from there.
>>
>> Mark
>>
>>
>>>
>>> Each task opens its own connection using datasource and closes it.
>>>
>>> But in Tomcat, I am getting below error at line:
>>>
>>> initialContext = new InitialContext();
>>>
>>> javax.naming.NoInitialContextException: Cannot instantiate class:
>>> org.apache.naming.java.javaURLContextFactory
>>> at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
>>> ~[?:1.8.0_161]
>>> at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
>>> ~[?:1.8.0_161]
>>> at javax.naming.InitialContext.init(Unknown Source) ~[?:1.8.0_161]
>>> at javax.naming.InitialContext.<init>(Unknown Source) ~[?:1.8.0_161]
>>> at com.dummy.test.TestClass.compute(TestClass.java:71) [classes/:?]
>>> at java.util.concurrent.RecursiveAction.exec(Unknown Source)
>> [?:1.8.0_161]
>>> at java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
>> [?:1.8.0_161]
>>> at java.util.concurrent.ForkJoinPool$WorkQueue.execLocalTasks(Unknown
>>> Source) [?:1.8.0_161]
>>> at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(Unknown Source)
>>> [?:1.8.0_161]
>>> at java.util.concurrent.ForkJoinPool.runWorker(Unknown Source)
>> [?:1.8.0_161]
>>> at java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
>>> [?:1.8.0_161]
>>> Caused by: java.lang.ClassNotFoundException:
>>> org.apache.naming.java.javaURLContextFactory
>>>
>>> This error seems to be coming only in Tomcat and when I run the same code
>>> in Wildfly/Glassfish or JBOSS EAP, everything works fine.
>>>
>>> On the other hand if I change my code and run these tasks using Thread
>>> instead of Fork Join framework, I don't face this issue in Tomcat.
>>>
>>> So why this error is coming in Tomcat only?
>>>
>>> Note: I am getting this error after deploying in Tomcat and hitting app
>> URL
>>> from Postman. The so called missing class is already there in
>>> jar catalina.jar present inside <Tomcat_Home>/lib
>>>
>>> Environment Details:
>>>
>>> Java Version: 1.8
>>> Tomcat Version: 8.5, 9.0.6
>>> OS: Windows 10 Pro 64 bit
>>> Database: Oracle 11g and MySQL 5.7
>>>
>>> I have uploaded a dummy code to simulate this issue in Git. Please refer
>> to
>>> the readme.txt for full details there.
>>>
>>> Git URL:
>>> https://github.com/wambling/my-project.git
>>>
>>> Regards,
>>> Priyam
>>>
>>
>>
>> ---------------------------------------------------------------------
>> 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: Tomcat's data-source issue with Fork Join Tasks

Posted by Priyam Srivastava <pr...@gmail.com>.
Hi Mark,

Many Thanks for your response. I was able to resolve this issue by writing
the below code just before JNDI Look Up:

Thread l_thread = Thread.currentThread();
l_thread.setContextClassLoader(this.getClass().getClassLoader());
initialContext = new InitialContext();
....


I have a questions based on the discussions on the thread link you provided:

If this is something to do with ForkJoin Pool, then why it is happening
only in Tomcat why not in other application servers as I mentioned in my
initial post.

Regards,
Priyam

On Sat, Mar 17, 2018 at 2:22 AM, Mark Thomas <ma...@apache.org> wrote:

> On 16/03/18 12:06, Priyam Srivastava wrote:
> > I have a scenario where we have to run some random number of independent
> > tasks to load data from DB. So I am using Java's fork Join framework to
> > create those task and then invoke them.
>
> See:
> https://bz.apache.org/bugzilla/show_bug.cgi?id=60620 and the various
> threads linked from there.
>
> Mark
>
>
> >
> > Each task opens its own connection using datasource and closes it.
> >
> > But in Tomcat, I am getting below error at line:
> >
> > initialContext = new InitialContext();
> >
> > javax.naming.NoInitialContextException: Cannot instantiate class:
> > org.apache.naming.java.javaURLContextFactory
> > at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
> > ~[?:1.8.0_161]
> > at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
> > ~[?:1.8.0_161]
> > at javax.naming.InitialContext.init(Unknown Source) ~[?:1.8.0_161]
> > at javax.naming.InitialContext.<init>(Unknown Source) ~[?:1.8.0_161]
> > at com.dummy.test.TestClass.compute(TestClass.java:71) [classes/:?]
> > at java.util.concurrent.RecursiveAction.exec(Unknown Source)
> [?:1.8.0_161]
> > at java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
> [?:1.8.0_161]
> > at java.util.concurrent.ForkJoinPool$WorkQueue.execLocalTasks(Unknown
> > Source) [?:1.8.0_161]
> > at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(Unknown Source)
> > [?:1.8.0_161]
> > at java.util.concurrent.ForkJoinPool.runWorker(Unknown Source)
> [?:1.8.0_161]
> > at java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
> > [?:1.8.0_161]
> > Caused by: java.lang.ClassNotFoundException:
> > org.apache.naming.java.javaURLContextFactory
> >
> > This error seems to be coming only in Tomcat and when I run the same code
> > in Wildfly/Glassfish or JBOSS EAP, everything works fine.
> >
> > On the other hand if I change my code and run these tasks using Thread
> > instead of Fork Join framework, I don't face this issue in Tomcat.
> >
> > So why this error is coming in Tomcat only?
> >
> > Note: I am getting this error after deploying in Tomcat and hitting app
> URL
> > from Postman. The so called missing class is already there in
> > jar catalina.jar present inside <Tomcat_Home>/lib
> >
> > Environment Details:
> >
> > Java Version: 1.8
> > Tomcat Version: 8.5, 9.0.6
> > OS: Windows 10 Pro 64 bit
> > Database: Oracle 11g and MySQL 5.7
> >
> > I have uploaded a dummy code to simulate this issue in Git. Please refer
> to
> > the readme.txt for full details there.
> >
> > Git URL:
> > https://github.com/wambling/my-project.git
> >
> > Regards,
> > Priyam
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Tomcat's data-source issue with Fork Join Tasks

Posted by Mark Thomas <ma...@apache.org>.
On 16/03/18 12:06, Priyam Srivastava wrote:
> I have a scenario where we have to run some random number of independent
> tasks to load data from DB. So I am using Java's fork Join framework to
> create those task and then invoke them.

See:
https://bz.apache.org/bugzilla/show_bug.cgi?id=60620 and the various
threads linked from there.

Mark


> 
> Each task opens its own connection using datasource and closes it.
> 
> But in Tomcat, I am getting below error at line:
> 
> initialContext = new InitialContext();
> 
> javax.naming.NoInitialContextException: Cannot instantiate class:
> org.apache.naming.java.javaURLContextFactory
> at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
> ~[?:1.8.0_161]
> at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
> ~[?:1.8.0_161]
> at javax.naming.InitialContext.init(Unknown Source) ~[?:1.8.0_161]
> at javax.naming.InitialContext.<init>(Unknown Source) ~[?:1.8.0_161]
> at com.dummy.test.TestClass.compute(TestClass.java:71) [classes/:?]
> at java.util.concurrent.RecursiveAction.exec(Unknown Source) [?:1.8.0_161]
> at java.util.concurrent.ForkJoinTask.doExec(Unknown Source) [?:1.8.0_161]
> at java.util.concurrent.ForkJoinPool$WorkQueue.execLocalTasks(Unknown
> Source) [?:1.8.0_161]
> at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(Unknown Source)
> [?:1.8.0_161]
> at java.util.concurrent.ForkJoinPool.runWorker(Unknown Source) [?:1.8.0_161]
> at java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
> [?:1.8.0_161]
> Caused by: java.lang.ClassNotFoundException:
> org.apache.naming.java.javaURLContextFactory
> 
> This error seems to be coming only in Tomcat and when I run the same code
> in Wildfly/Glassfish or JBOSS EAP, everything works fine.
> 
> On the other hand if I change my code and run these tasks using Thread
> instead of Fork Join framework, I don't face this issue in Tomcat.
> 
> So why this error is coming in Tomcat only?
> 
> Note: I am getting this error after deploying in Tomcat and hitting app URL
> from Postman. The so called missing class is already there in
> jar catalina.jar present inside <Tomcat_Home>/lib
> 
> Environment Details:
> 
> Java Version: 1.8
> Tomcat Version: 8.5, 9.0.6
> OS: Windows 10 Pro 64 bit
> Database: Oracle 11g and MySQL 5.7
> 
> I have uploaded a dummy code to simulate this issue in Git. Please refer to
> the readme.txt for full details there.
> 
> Git URL:
> https://github.com/wambling/my-project.git
> 
> Regards,
> Priyam
> 


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