aspose file tools*
The moose likes Servlets and the fly likes DATASETS IN SERVLETS? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "DATASETS IN SERVLETS?" Watch "DATASETS IN SERVLETS?" New topic
Author

DATASETS IN SERVLETS?

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
(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
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12779
    
    5
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
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
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
Rancher

Joined: Mar 22, 2000
Posts: 12779
    
    5
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
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
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
Rancher

Joined: Mar 22, 2000
Posts: 12779
    
    5
Yep! Thats the cause. You should investigate connection pooling.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1066
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DATASETS IN SERVLETS?