• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DATASETS IN SERVLETS?

 
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(1) Since as I understand it, a dataset is not "thread safe", should it be placed within synchrnoized blocks in a Servlet? (for example, a database lookup upon login -- since a servlet allows concurrent access unless you use a single threaded model)
(2) Also, is there any advantage of having a class with dataset operations that you instantiate and all its methods vs. just a class with static methods you make synchronized for the dataset operations? (From the standpoint of thread safety and performance)
Thanks in advance for any and all comments or suggestions.
-- Mike
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create variables such as a dataset, that contain request specific information, as local variables - not instance or static variables.
The problem is not that the methods are instance or static but that the variables must NOT be instance or static. Local variables can only be seen by the Thread that is processing the request, therefore they don't need synchronized access.

Bill
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bill,
Thanks very much for your reply.
In my original problem I had created a "procedure file" (a file with static methods I only needed to call, but didn't need to subclass) and made common methods like "AddUser()", "GetUserInfo()", etc.
These methods *all* had private (local) variables for the ResultSet return sets (I also tried to create ResultSets in JSPs but had similar problems), but I was still getting the Tomcat errors that the "current operation is invalid after the ResultSet is closed".
I made the same methods, the ones with local variables, synchronized, and these errors went away.
Can you explain what might have been going on?
Thanks very much in advance.
-- Mike
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't imagine what would cause what you observe. Synchronization should only affect the result if you have the potential for multiple Threads accessing the data.
How many users are "simultaneously" using this application? Exactly how are you getting a database connection and keeping multiple users from using the same connection?
Bill
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bill,
I'm creating a single connection and passing it around to the various JSP pages for all the users.
I read that the MySQL JDBC connection is multi-threaded.
Could this be the problem?
Thanks in advance ... again!
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep! Thats the cause. You should investigate connection pooling.
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bill,
Yes, I've already looked at connection pooling.
However, if you take a look at Marty Hall's "Core Servlets and Java Server Pages, 2nd Ed.", you'll see he discusses using a single connection as a viable approach (with multi-threaded JDBC drivers, anyway) in applications that aren't too big.
Thanks very much for your reply.
-- Mike
 
reply
    Bookmark Topic Watch Topic
  • New Topic