These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: concurrent sessions
PostPosted: Tue Aug 23, 2005 7:36 am 
Newbie

Joined: Tue Aug 23, 2005 7:18 am
Posts: 5
Location: pune,India
Hello everybody,

I m new on this forum and also i m new to hibernate.

My probelm is.............

I have an Object having some attributes like Id,userID, password, status. Primary key is Id.

I m using MySql database.
I m using save() to insert this object's details in the database.

On a particular page , user enters the userId (String) ,password (String) and on the very next page it again enters the password to confirm the password.(confirmpassword.jsp page)

I made a test case. In that testcase, both the users fill the same userId, password and in the next page i.e. confirmpassword.jsp
they simultaneously submit after entering the same password exactly at the same time . i even tried to hit on a count of 3.

what happens this object is getting created 2 times in the database and that with the same userId which shouldnot.

What should happen is that that it should allow only one user to move forward and prevent the other.Generally an error message should be shown to the other user.

The code is..

session.save(object);


I am not able to rectify this problem.please suggest me something so that my app doesnt allows to put 2 objects with the same userId.i have tried everything from using locks to....

i m really strucked..
i wud b very gratefull if anybody helps me get out of this problem and asap ...


thanx
Prasad


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 7:58 am 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
Just make userID unique

Code:
<property name="userID" type="java.lang.String" unique="true" not-null="true" length="16" />


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 8:30 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Handle exception to redirect user to the first page if constraint check fails and as I understand "userid" must be unique not "password".


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 10:15 am 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
unique="true" affects DDL generation, it will not cause Hibernate to throw an exception if this uniqueness constraint does not already exist in the database.

If you want to allow only one user to go through the workflow at a time, you can synchronize on some lock object held in the servlet context.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 10:48 am 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
The danger of assumptions. I assumed that it would be obvious that unique="true" only affects the ddl. I still think it's by far the easiest way of solving this problem. If two users try and create a user with the same userID one of them will get an exception which can be dealt with.

Using a lock is an unneccesary complication.


Top
 Profile  
 
 Post subject: concurent sessions
PostPosted: Wed Aug 24, 2005 10:29 am 
Newbie

Joined: Tue Aug 23, 2005 7:18 am
Posts: 5
Location: pune,India
thanx jamie and thanx everybody.

My problem got solved.

but....

accuracy is there but not 100%. means, if the users hit even within a time difference of say, 1 millisecond and that with the same userId , password then my app works perfectly fine. In this case one user proceeds and an error message is shown to the other user by handling the exception....

but if the users hit at exactly the same moment then one user is shown the error message but the user who should proceed with userid/password gets an exception..
but still the object gets inserted in the database.

(well this is a really exceptional case)

i m gettin the exception..............




connection handle has been closed and unusable

org.hibernate.exception.GenericJDBCException: could not execute query...





i tried to find it on net..

some say that its a bug in hibernate3.0 which i m using...



regards

prasad


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 24, 2005 10:59 am 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
How did your problem get solved? Because if I read you correctly it's still possible to have two objects with the same userID which I wouldn't consider a solution.

To re-iterate, if you make userID unique (in the database) it will be impossible to have two objects with the same userID.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 25, 2005 1:30 am 
Newbie

Joined: Tue Aug 23, 2005 7:18 am
Posts: 5
Location: pune,India
i did that already in the database making the userId unique.....

well actually this case is really really exceptional as its least possible that 2 users in the world using my app will enter the same userId, password and also hit at exactly the same time.
but still for the sake of accuracy i m doing it...


well it restricts one user to enter the same userId but at the same time
an object is created for the other user but that user gets an exception and that only in the case when they submit the request at exactly the same time (tested using an automated tool) which shouldnt and tats not a problem with the code...

exception msg is..............................

connection handle has been closed and unusable

org.hibernate.exception.GenericJDBCException: could not execute query...



but, still if the difference in submitting is 1 millisecond also, it works perfectly fine...


prasad


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.