-->
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.  [ 3 posts ] 
Author Message
 Post subject: Continue after Exception was thrown??
PostPosted: Mon Apr 22, 2013 5:15 am 
Newbie

Joined: Mon Apr 22, 2013 4:53 am
Posts: 2
Hi all,

I'm facing the problem that my custom DAO object (derived from HibernateDaoSupport) breaks after a DataIntegrityViolationException was thrown.

I've a MySQL table with 2 different unique keys (email and username must be unique in that table). When I'm trying to insert a new entity in that table and one of the attributes violates an unique key constraint, a DataIntegrityViolationException is thrown. After that exception I like to figure out what was the reason (either a duplicated key on column "username" or "email" or on both columns), but my HibernateDaoSupport object seems to be broken then:

I'm using Hibernate 3.1.3 on a multitheaded spring-based webservice. I assume that the session handling is "thread-bound". What is the common way to distinguish what was the reason for that exception? I really don't like to parse the MySQL error string.

Code:
try {
   final Entity entity = new Entity();
   entity.setUserName(username);
   entity.setEmail(email);
   dao.persist(entity); // fails on duplicated key violation
} catch (DataIntegrityViolationException e) {
    // trying to figure out if there is an existing entity with the given email or username to return the right error message.

    // that call also fails because getHibernateTemplate() returns null then... so I can't submit a new query.
    if (dao.getByEmail(email) != null) {
        throw new DuplicateEmailException(email);
    }

    // that call also fails because getHibernateTemplate() returns null then... so I can't submit a new query.
    if (dao.getByUsername(username) != null) {
        throw new DuplicateUsernameException(username);
    }
}


Top
 Profile  
 
 Post subject: Re: Continue after Exception was thrown??
PostPosted: Mon Apr 22, 2013 8:00 am 
Newbie

Joined: Mon Apr 22, 2013 4:53 am
Posts: 2
In addition: When trying to lookup an existing Entity by email within the catch block then I get a org.hibernate.AssertionFailure

"null id in [...] entry (don't flush the Session after an exception occurs)"

Is there any way to recover the current Session or at least to manually close/reopen a new Session? It seems that "thread-bound" sessions are in place here.

Thank you


Top
 Profile  
 
 Post subject: Re: Continue after Exception was thrown??
PostPosted: Mon Apr 22, 2013 11:57 am 
Regular
Regular

Joined: Wed Apr 10, 2013 1:02 am
Posts: 50
I think you gotta Check what should you set a value of current session context if you are using it with springs thread or application I will try to simulate this meanwhile can u show the full stack trace

_________________
Regards
Akash Miital
http://akash.thegrassroots.co.in/hibernate/


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