This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Tomcat and the fly likes mysql and sessions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "mysql and sessions" Watch "mysql and sessions" New topic
Author

mysql and sessions

Hassan Schnieder
Greenhorn

Joined: Jun 09, 2006
Posts: 11
Hello everyone, I'm new here. Nice to meet you.

I have a question I think involves sessions. My problem I'm thinking is...

1) I have someone log into my application.
2) The user has to add his "username","password".
3) My application checks the database to see if the username/password already exists.
4) My application returns, "username"/"password" does not exist..."username/password" added to database.

MY QUESTION.
what if 5 people are logging in and checking the data at the same time. Do sessions prevent more than one user viewing or modifying the data in a single instance? I want to prevent someone adding a password that doesn't exist at the same time someone may be adding that password.

Please help me understand this process, if sessions don't take care of that and I don't have to worry then how does one implement code to prevent an error.

best regards,
Hassan





Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196
My first suggestion, since it should be quick to query, and then add the
information, would be to place the code for that in a single method and
synchronize that method.
Hassan Schnieder
Greenhorn

Joined: Jun 09, 2006
Posts: 11
Hello,
Okay that's what I was thinking "sort-of". I thought that if you had your own session that would prevent 2 people access at the same time? Is that the only way to do it?

Regards,
Hassan
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Sessions do nothing to prevent multiple threads (requests) from trying to access the same database tables simultaneously.

If your database doesn't have transaction support (not sure where mySql stands on this) then you would want to synchrnoize methods where this is a potential problem.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Hassan Schnieder
Greenhorn

Joined: Jun 09, 2006
Posts: 11
Hi,

Thanks for all the replys. Synchronized connection methods it is. Time to do more homework on this.

Best Regards,
Hassan
Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196
Not all db interaction methods need to be synchronized. Only those where
you believe there will be a probable conflict, as in the posted example.
Also, as stated above, using transactions will also prevent alot of
problems. If you are using the InnoDB engine in MySQL then, you have
transaction support.
[ June 13, 2006: Message edited by: Martin Simons ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: mysql and sessions
 
Similar Threads
Understanding Sessions
How to change user credentials when accessing DB
Sessions being opened in a closed state...
MySQL - add and remove username from table
Avoiding two sessions by a single user