-->
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.  [ 7 posts ] 
Author Message
 Post subject: fluch() + commit() = Exception?
PostPosted: Mon Jan 17, 2005 7:08 pm 
Newbie

Joined: Mon Nov 29, 2004 2:00 pm
Posts: 7
We have some code during out users login that records the time that they logged in.

The code looks similar to the following:

Code:
try
{
  Session hbmsession = SessionFactory.openSession();
  Transaction tx = null;
  try
  {
    List l = hbmsession.find("select u from User u where username=:username or email='"+username+"'",
                username, Hibernate.STRING);

    ...
    if (passesPassword && passesDomain)
    {
      log.debug("Found Valid User");

      tx = hbmsession.beginTransaction();
      // update the last login time in the user record

      Date date = new Date();
      user.setLastLogin(d);
      hbmsession.update(u);
      hbmsession.flush();

      log.debug("Saving user login");
      // create a new login record
      UserLogin login = new UserLogin();
      login.setUserid(user.getId());
      login.setLoginTime(date);
      hbmsession.save(login);
      hbmsession.flush();
      tx.commit();
    }
  }
  catch(Exception e)
  {
    if (tx != null)
    {
      tx.rollback();
    }
    log.debug("Exception: " + e, e);
  }
  finally
  {
    hbmsession.close();
  }
}
catch (Exception e)
{
  log.debug("Exception "+e.getMessage(), e);
}


The code throws the following exception at the hbmsession.flush(); line.

Code:
net.sf.hibernate.exception.ConstraintViolationException: could not insert: [com.knowledgefactor.gen.UserLogin#46]
        at net.sf.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:73)
        at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30)
        at net.sf.hibernate.persister.AbstractEntityPersister.convert(AbstractEntityPersister.java:1332)
        at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:474)
        at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:438)
        at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:37)
        at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2438)
        at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2391)
        at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2260)
        at com.knowledgefactor.tag.LogonAction.execute(LogonAction.java:173)
        at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
        at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
        at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
        at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:257)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
        at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:195)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
        at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
        at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:206)
        at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:324)
        at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:395)
        at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:673)
        at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:615)
        at org.apache.jk.common.SocketConnection.runIt(ChannelSocket.java:786)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
        at java.lang.Thread.run(Thread.java:536)
Caused by: java.sql.SQLException: Duplicate key or integrity constraint violation message from server: "Duplicate entry '46' for key 1"
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
        at com.mysql.jdbc.Connection.execSQL(Connection.java:2251)
        at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1772)
        at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1619)
        at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:233)
        at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:233)
        at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
        at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:464)
        ... 38 more



Here is the mapping for our UserLogin

Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<!--
    Created by the Middlegen Hibernate plugin

    http://boss.bekk.no/boss/middlegen/

    http://hibernate.sourceforge.net/
-->

<class
    name="com.knowledgefactor.gen.UserLogin"
    table="user_logins">

    <id
        name="id"
        type="java.lang.Integer"
        column="id">

        <generator class="increment" />
    </id>

    <property
        name="userid"
        type="java.lang.Integer"
        column="userid"
        length="11"
    />

    <property
        name="loginTime"
        type="java.sql.Timestamp"
        column="login_time"
        length="19"
    />

    <!-- associations -->
</class>

</hibernate-mapping>


Does anyone know why we would be throwing an insert exception? Is it a bad idea to do a flush and then do a commit on a transaction?

Thanks for your time,

Nate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 17, 2005 7:12 pm 
Newbie

Joined: Mon Nov 29, 2004 2:00 pm
Posts: 7
Sorry, the topic should read

flush + commit = exception?

Sorry.

Nate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 3:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Did you read the part of the execption where it states:

Caused by: java.sql.SQLException: Duplicate key or integrity constraint violation message from server: "Duplicate entry '46' for key 1"

you have a duplicate key somewhere.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 1:35 pm 
Newbie

Joined: Mon Nov 29, 2004 2:00 pm
Posts: 7
Thanks for pointing out that exception. I did see it (the exception) and after re-reading my first post...I guess I wasn't very clear...

I do know that there is an insert problem and it is happening on the second flush() statement. I am hoping that someone might be able to say, "well, having two flush() statements in one session is a bad idea..." or "The code does look pretty good, but what is the statement for creating the database table look like?"

BTW, here is the code that created the user_logins table if it is needed...

Code:
CREATE TABLE `user_logins` (
  `id` int(11) NOT NULL auto_increment,
  `userid` int(11) default NULL,
  `login_time` datetime default NULL,
  PRIMARY KEY  (`id`),
  FOREIGN KEY (userid) REFERENCES users(id) on update restrict
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


Thanks again for your time,

Nate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 2:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you use hibernate "increment" generator with a database auto-increment - that will at some point collide.

I think you should rather use "identity" to have hibernate ask the database for which identifier it has assigned when auto incrementing.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 3:44 pm 
Newbie

Joined: Mon Nov 29, 2004 2:00 pm
Posts: 7
That is a good catch on the generator type and we are looking to change that value. However, I am a little confused on which generator type to use.

After reading the document at:
http://www.hibernate.org/hib_docs/refer ... -generator
I am not sure of the difference between the "identity" generator and the "native" generator. Can anyone shed some light on the differences?

Thanks for your time,

Nate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 3:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Quote from the doc: native picks identity, sequence or hilo depending upon the capabilities of the underlying database. - what is unclear about that?


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