aspose file tools*
The moose likes Servlets and the fly likes Is it a good idea to spawn a thread from a servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is it a good idea to spawn a thread from a servlet?" Watch "Is it a good idea to spawn a thread from a servlet?" New topic
Author

Is it a good idea to spawn a thread from a servlet?

Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
Hello,

I have an application which generates reports. Currently this is already in place. Here a servlet (A) invokes another servlet (B) to get the job done. Now i have a new requirement to run multiple reports at a time. I have done it in a sequential manner. Thus A invokes B several times one after another. For the generate of each report a DB2 sp is invoked ( which get the Report data). Actually the DB2 sp is time sonsuming & it take an average of 15 mins. Thus if i have 5 reports to be run it will take an avg of 15 * 5 = 75 mins.
I have 2 queries:
1. If i do this in a multithreaded way ( by spawning different threads for each reports form Servlet A )will it help reducing the time?
2. Is it a good idea to spawn a thread from a servlet?

Regard,
Saj
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Multiple reports at a time? Is it for one user or multiple user.
- If multiple user then servlet is already multithreaded.
- If one user, then why need multiple reports simultaneously?

I think you need to tune your SQL or DB.
- Indexes must be there on appropriate columns. Specifically on columns which participates in condition
- ORing should be after ANDing
- Use LIKE operator instead of functions like substr(), if any is being used.
Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
It is for One User. Multiple reports run simultaneously for 1 user. For each report a .csv file is generated
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I would avoid it if possible.
Threads started from within web apps can have adverse effects when trying to reload the application and/or restart the server.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

This sounds like a candidate for an asynchrous process - since the DB operation is so time consuming, you can't reasonably expect the user to wait for a response. Perhaps JMS to generate the reports and JavaMail to delover them?

I have to agree with Adeel Ansari though, if this is taking so long you might want to review your DB configuration and/or how you've written your Stored Precedures.
[ March 29, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it a good idea to spawn a thread from a servlet?
 
Similar Threads
ansynchronous request
Will Session timeout while the servlet is being executed?
Usability question on Pentaho
Threads and Synchronization examples
Thread hung executing start()