• Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronized problems with JSP/Sverlet, please help!

 
urey
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't a new instance of the Servlet/JSP for each request in Tomcat - they are probably all calling the same instance so any state will be shared. If I Connection is stored as a member variable then I'll have synchronized problems with it already being in use if two people hit the page at the same time.
Incidentally, although it might work, synchronizing the database access in the JSP would be a bad idea since I�d just create a queue of requests waiting to use the one Connection whereas I really need a number of Connections to deal with everyone simultaneously.
What I do? Need any advice from you.
Sincerely
Urey
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use a connection pool to manage connections outside the JSP/servlet code. You can probably find lots of references by searching a Yahoo with "servlet connection pool"
Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For database connections, the connection pool mechanism is certainly the best.
In general though, you are correct in that storing resources in member variable of a servlet creates problems, and that synchronizing on such a member would severly hamper performance.
A mechanism you can use in cases where you want to store a resource on a per-user basis is the session. Each user gets their own session and your servlet can store and retrieve values from it without fear of 'cross contamination'.
hth,
bear
 
ersin eser
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi urey,
We have a naming policy here at JavaRanch where all publicly displayed names take the form, "Firstname Lastname". Could you please change yours to bring it into line?
Thanks in advance and we hope that you enjoy your stay at the JavaRanch!

ersin
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. "urey"
You might wanna check
http://www.coderanch.com/t/280235/JSP/java/Connection-Pooling
or
http://www.javaexchange.com/
a class that implements connection pooling. You can freely use it.
cheers
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!