java servlet conflict

S

Steven S

I have a servlet setup to access my database. The servlet works perfectly
except when there are multiple simultaneous posts that run the servlet.

It seems that that when the servlet is run by two separate threads they
conflict somehow and cause the other to not function properly.

The question is, how do I make sure each thread that is running concurrently
runs without affecting the other threads?

I tried setting the servlet as Private but got a compile error.

Steve
 
S

Steven S

Probably the easiest fix is to have your servlet implement
SingleThreadModel which will force your engine to deploy an additional
instance of your servlet rather than trying to have one instance process
both requests concurrently.

DM

Could you elaborate a bit on how to do this?

Thanks for the response,

Steve
 
V

VisionSet

Steven S said:
I have a servlet setup to access my database. The servlet works perfectly
except when there are multiple simultaneous posts that run the servlet.

It seems that that when the servlet is run by two separate threads they
conflict somehow and cause the other to not function properly.

The question is, how do I make sure each thread that is running concurrently
runs without affecting the other threads?

I tried setting the servlet as Private but got a compile error.

Do you have any class attributes that are request specific?
You should never have this situation.
SingleThreadModel does solve this but it is highly none recommended.

If you have much logic at all you should move it out into a Model tier
your servlet can then become purely a 'Controller' marshalling requests,
sessions etc.
 
V

VisionSet

Steven S said:
Not sure what this means exactly, but I must otherwise it would be working.
How do I check for this?

Post your servlet code
I would suppose the reason it is not recommended is because it probably
gives up flexibility and performance. I would most likely agree if I knew
how to set it up and test it. Can you point me in the right direction so
that I set it up in Tomcat 4.1?

Simply implement SingleThreadModel - it's in the Servlet API
But I shouldn't bother.
I would probably agree if I could envision what a Model tier is? Can you
clerify?

A full MVC (Model View Controller) paradigm explanation is here

http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_
2e/web-tier/web-tier5.html

It may be a little to heavy at this stage!

Model is simply the back end of your web application, when web-apps become
large, it is useful to put all the business logic (stuff that has nothing to
do with the fact it is a web-app could equally well apply to a Swing GUI
front end) in seperate classes that have no knowledge of the Front end.
 
S

Steven S

Could you elaborate a bit on how to do this?
public class YourServlet extends HttpServlet implements
SingleThreadModel{

Worked.

What are the consequences?

THANK YOU.

Steve
 
D

Dave Miller

Worked.

What are the consequences?

THANK YOU.

Steve

Limited. The engine will spawn a new instance for each concurrent
request which, obviously, uses resources. If you compare the resources
that a servlet uses processing an HTTP request and then executing an SQL
statement versus those it takes for any GUI, you'll realize that it is
almost impossible to get yourself in trouble unless you are spawning
thousands of concurrent instances. If you have that level of traffic,
nothing that you do from a coding standpoint will solve your need for
more resources.

Regards,

DM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top