This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "creating threads in Watch "creating threads in New topic
Author

creating threads in 'init' for servlet (advanced)

Bhaarat Sharma
Ranch Hand

Joined: Jun 04, 2007
Posts: 96
Hello

we have a servlet ..in 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 http://www-1.ibm.com/support/docview.wss?rs=180&uid=swg21231761
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


omnipresent
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15950
    
  19

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
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
Sheriff

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 java:comp..works 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 ]
 
 
subject: creating threads in 'init' for servlet (advanced)
 
Similar Threads
installing problem
Container Time Out
WebSphere 4.0.1 AE load balancing
error code 500 and java.lang exceptions in WebSphere
what should be the compiler complicance level?