-->
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: ID (primary key) is incremented even though save is failed.
PostPosted: Wed May 26, 2010 4:38 pm 
Newbie

Joined: Wed May 26, 2010 3:55 pm
Posts: 2
I have this strange behavior. I would appreciate if you can help me out. I have a user table in mysql database. In my application, I am creating new users. Here is the scenario: Let's say user X comes and creates an entry in database and ID (primary key, auto incremented) of 1 is assigned. Now the user X tries to create an account again; however, username is unique, so what happens is since the user enters the same user name, I get a duplicate hibernate exception which is correct behavior. But, when the user Y comes and creates an account, now the ID is 3 assigned instead of 2. I can replicate this every time. I tried session refresh, evict, and none of them is working. What is the problem? Thank you so much in advance for your time.

In my class, I have the following method:
public void registerUser(User user)throws Exception
{
try{

begin();
getSession().save(user);

commit();

logger.info("User created and entered in database");
SessionFactoryUtil.close();

}

catch( HibernateException e ){
rollback();

logger.info("exception is occured");
throw new Exception("Hibernate exception is occured");
}

}

}
package hibernate.domain.util;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class SessionFactoryUtil {

private static final Logger log = Logger.getAnonymousLogger();
private static final ThreadLocal<Session> session = new ThreadLocal<Session>();
private static final SessionFactory sessionFactory =
new AnnotationConfiguration().configure().buildSessionFactory();

protected SessionFactoryUtil(){}

public static Session getSession()
{
Session session = (Session) SessionFactoryUtil.session.get();
if (session == null)
{
session = sessionFactory.openSession();
SessionFactoryUtil.session.set(session);
}
return session;
}

protected void begin()
{
getSession().beginTransaction();
}

protected void commit()
{
getSession().getTransaction().commit();
}

protected void rollback()
{
try
{
getSession().getTransaction().rollback();
}
catch( HibernateException e )
{
log.log(Level.WARNING,"Cannot rollback",e);
}

try
{
getSession().close();
}
catch( HibernateException e )
{
log.log(Level.WARNING,"Cannot close",e);
}
SessionFactoryUtil.session.set(null);
}

public static void close()
{
getSession().close();
SessionFactoryUtil.session.set(null);
}


}


Top
 Profile  
 
 Post subject: Re: ID (primary key) is incremented even though save is failed.
PostPosted: Wed May 26, 2010 5:00 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
It is expected behavior of MySQL auto-increment columns. At least for InnoDb tables. Quote from http://dev.mysql.com/doc/refman/5.1/en/ ... dling.html
Quote:
In all lock modes (0, 1, and 2), if a transaction that generated auto-increment values rolls back, those auto-increment values are “lost.” Once a value is generated for an auto-increment column, it cannot be rolled back...


Top
 Profile  
 
 Post subject: Re: ID (primary key) is incremented even though save is failed.
PostPosted: Wed May 26, 2010 5:49 pm 
Newbie

Joined: Wed May 26, 2010 3:55 pm
Posts: 2
Thank you so much for your help!


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.