Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating threads in 'init' for servlet (advanced)

 
Bhaarat Sharma
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18164
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bhaarat Sharma
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bhaarat Sharma
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic