This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Servlets and the fly likes creating threads in 'init' for servlet (advanced) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "creating threads in Watch "creating threads in New topic

creating threads in 'init' for servlet (advanced)

Bhaarat Sharma
Ranch Hand

Joined: Jun 04, 2007
Posts: 96

we have a servlet servlets init method we are creating new threads by calling the Timer class like this

Problem is that during our stress testing on IBM WSAD we've noticed errors like J2CA0075W: An active transaction should be present

Upon researching this issue further i found this link
which i think suggests that the servlet shouldnt be creating threads which make J2C connections. Our thread is making the DB connection.

does nay one have experince with this sort of problem? is it a bad programming construct to create threds in a servlet? if so..where should we be creating our threads? we want this thread to run every 30 seconds and this is a web appliction.

I'd appreciate any help

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17142

Sometimes there's no cleaner way to do asynchronous processing in a web application environment. Don't let the EJB prohibition on threads deceive you into thinking that servlets are also forbidden to spawn threads. All they're really saying is that if you spawn threads in a web app, the web app should take total responsibility for the entire lifecycle of the threads and not expect the appserver itself to try and pick up after you.

You should be OK as long as you scrupulously follow IBM's recommendations as outlined in that bulletin.

An IDE is no substitute for an Intelligent Developer.
Bhaarat Sharma
Ranch Hand

Joined: Jun 04, 2007
Posts: 96
hm ok so should i just kind of cheat and not log this error message? OR try to see if DB connection can be taken out and done some place other than the thread? This message is just coming as a warning so i don't know how much impact this is causing to our system. However lot of our stress tests are failing when the 'background' manager is running every 30 secs. dont know if these two things are related..

actually when we make normal connections to the DB from the UI like when user clicks a link we use DSNames like this: java:comp/env/jdbc/pAdmin

However, when the thread created in servlet makes connection to the DB we HAVE to use this DSName: jdbc/pAdmin. If we use the one listed above we get errors. I dont know how related these two things are...but we have the following snippet in our web.xml

[ July 19, 2007: Message edited by: bhaarat sharma ]
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

If you have a Servlet Spec 2.3 or better container, you can do this work in plain old java classes (POJOs) initialized and bound to scope with the help of a ServletContextListener.

This allows servlets to do what they were meant to do (respond to requests) and nothing else.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bhaarat Sharma
Ranch Hand

Joined: Jun 04, 2007
Posts: 96
i checked and DB connections are being made in run method for BackManager class. Whereelse do you suggest i should be making these connections? when i say they are being made in the run method i mean. the CALL to where the connection is estables is from the run method
Bhaarat Sharma
Ranch Hand

Joined: Jun 04, 2007
Posts: 96
ok i've implemented a servletcontext listener and took out the Timer created from my servlets init method and placed it in my POJO implmenting servletContextListener. However, still my DSName HAS to be jdbc/pAdmin instead of java:comp/env/jdbc/psrAdmin. DSName starting with everywhere else but just not for the back ground manager. if I use jdbc/pAdmin dsName then i constantly get a warning whenever the background manager tries to make DB connection.

any thoughts?

good now i know how to deal with servletcontextlistener but what is the use for it?

even going back to my first post. After even creating threads in plain old java class that implements servletcontextListener i am still getting J2CA0075W: An active transaction should be present

[ July 19, 2007: Message edited by: bhaarat sharma ]
[ July 19, 2007: Message edited by: bhaarat sharma ]
I agree. Here's the link:
subject: creating threads in 'init' for servlet (advanced)
jQuery in Action, 3rd edition