-->
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.  [ 10 posts ] 
Author Message
 Post subject: lock table in the database
PostPosted: Fri Sep 03, 2004 7:20 pm 
Newbie

Joined: Fri Sep 03, 2004 6:48 pm
Posts: 7
Location: Bel
Hibernate version: 2.1.6

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:PostgreSQL7.2

Debug level Hibernate log excerpt:

Hi!
I have the following scenario: a user wants to register. He/She must enter a login and a password. The login must be unique. How can I garantee this? I read in "Hibernate in action" (Page 172 - CHAPTER 5) that there are two ways:

1- lock held in memory: achieved by using the synchronized java keyword?
2- lock table in the database;

How can I implement number 2 in the specified scenario?

Thanks a million!
M


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 03, 2004 8:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Use a unique constraint?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 03, 2004 8:29 pm 
Newbie

Joined: Fri Sep 03, 2004 6:48 pm
Posts: 7
Location: Bel
OK, this is a very good and simple idea. But I have one more doubt. When two users try to register the same login, one of them will get a success page and the other will get a fail (horrible) page. This fail page will have an HibernateException with a SQL unique constraint violation error. I can not show this to my user. I have to show a "beautiful" message stating that a user with that login already exists. So, here is the question: how do I know that the HibernateException was caused by a unique constraint error in the login column?
Thanks again!
M


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 03, 2004 8:32 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The Hibernate JDBCException wraps the underlying SQLException. Check the JavaDoc.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 03, 2004 8:47 pm 
Newbie

Joined: Fri Sep 03, 2004 6:48 pm
Posts: 7
Location: Bel
Thanks, I will check the SQLState, errorCode and message in the SQLException.
By the way, is there a database independent way of doing this check, such as:
Code:
exception.getSQLState().equals("unique_constraint_error")
?

Thank you SO MUCH! :o)
M


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 04, 2004 6:01 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Yes, there is Spring, which has a converter utility from SQLException to a typed exception hierarchy. Good uses of this are rare, as database exceptions should be non-recoverable (LockFailedException _should_ be recoverable).

This means that your idea works for simple errors such as a unique contraint violation, but falls over if constraints (and business rules) get more complex. In this case, you'd need a validation layer in your application anyway and feed the database only clean/checked values to avoid any exception.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 04, 2004 7:34 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Some databases have native features to evalute "CHECK" or to add "input mask" , it can be used for validation. Probably you can find or implement custom validator for popular MVC frameworks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 04, 2004 11:43 am 
Newbie

Joined: Fri Sep 03, 2004 6:48 pm
Posts: 7
Location: Bel
Hi,
Thanks for the tips. So, I have 3 options:
1- Check if the SQLException was generated because of a unique constraint error;
2- Use a validation layer in my application to check duplicate logins before adding the use to the system. In this case, how would such a validation be performed? I have many concurrent threads accessing my application, so I need some sort of locking (java synchronized keyword or database table lock) so that 2 or more users aren't inserted in the database with the same login at the same time;
3- Use some sort of database validation (CHECK) to verify if another user exists with the same login. This solution looks good, however I am discarding it for now because I want to be as database neutral as I can.

I still don't know how to implement option 2. Any sugestions?

Thanks,
M


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 04, 2004 12:48 pm 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
option "1" is better if you need user friendly output and performance,there is standard "sqlstate" code, google must help to find it or try to reproduce and print it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 06, 2004 9:13 am 
Newbie

Joined: Fri Sep 03, 2004 6:48 pm
Posts: 7
Location: Bel
Thanks everybody!

M


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.