-->
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: PostgreSQL error throwing off hibernate session object?
PostPosted: Fri Oct 07, 2005 12:59 am 
Newbie

Joined: Fri Oct 07, 2005 12:24 am
Posts: 3
Location: Houston, TX
Hi all,
just getting my feet wet in Hibernate but very excited about it... I've created a few JUnit test to get used to programming with hibernate but have run into a weird behavior that I can't figure out. I've included two tests below.
- The first one expectedly fails because it violates a PK constraint.
- The second one should run successfully, and does, provided the first test has not been run...
- However, when I run the first test then the second, the second one fails... It looks like my calls to HibernateUtil.closeSession() aren't working because the underlying JDBC connection appears to still be in an error state when I try and run the second test having reached the error state through the primary key violation on the first test...

Any ideas?
Thanks,
James

Hibernate version: 3.0.5

Mapping documents:
Code:
<class name="org.jandersen.bliki.beans.User" table="siteuser">

        <id name="username" type="string" unsaved-value="null" >
            <column name="username" sql-type="varchar(100)" not-null="true"/>
        </id>

      <property name="email"/>
        <property name="name"/>
      <property name="imgName" column="img_name"/>
      <property name="location"/>
      <property name="password" not-null="true"/>
       
        <property name="rssfeed"/>

      <property name="website"/>
      <property name="createDate" column="create_date"/>
    </class>

Code between sessionFactory.openSession() and session.close():
Note: the HibernateUtil class used below is copied straight from the documentation...
Code:
public void testNewUser() {
      Session session = HibernateUtil.currentSession();
      
      try {
         Transaction tx = session.beginTransaction();
          User juser = new User("fred");
          juser.setLocation("Bollywood!");
          juser.setPassword("foobar");
          session.save(juser);
          System.out.println("Done");
          tx.commit();
          HibernateUtil.closeSession();
         assertTrue(true);
      } catch (Exception e) {
         fail(e.getMessage());
      }
   }

   public void testSetUserLocation() {
      Session session = HibernateUtil.currentSession();
      try {
         Transaction tx = session.beginTransaction();
          User juser = (User) session.load(User.class,"james");
          juser.setLocation("Germany!");
          System.out.println("Done");
         tx.commit();
         HibernateUtil.closeSession();
         assertTrue(true);
      } catch (Exception e) {
         e.printStackTrace();
         fail(e.getMessage());
      }
   }


Full stack trace of any exception that occurs:
Code:
23:38:34,941  WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: null
23:38:34,941 ERROR JDBCExceptionReporter:72 - Batch entry 0 insert into public.siteuser (email, name, img_name, location, password, rssfeed, website, create_date, username) values (NULL, fred, NULL, Bollywood!, foobar, NULL, NULL, NULL, fred) was aborted.  Call getNextException to see the cause.
23:38:34,951  WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: 23505
23:38:34,951 ERROR JDBCExceptionReporter:72 - ERROR: duplicate key violates unique constraint "PK_siteuser"
23:38:34,962 ERROR AbstractFlushingEventListener:277 - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:181)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
   at org.jandersen.bliki.junit.DAOTest.testNewUser(DAOTest.java:65)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into public.siteuser (email, name, img_name, location, password, rssfeed, website, create_date, username) values (NULL, fred, NULL, Bollywood!, foobar, NULL, NULL, NULL, fred) was aborted.  Call getNextException to see the cause.
   at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2387)
   at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1257)
   at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:334)
   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2446)
   at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
   at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
   ... 23 more
23:38:34,992  WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: 25P02
23:38:34,992 ERROR JDBCExceptionReporter:72 - ERROR: current transaction is aborted, commands ignored until end of transaction block
23:38:34,992  INFO DefaultLoadEventListener:85 - Error performing load command
org.hibernate.exception.GenericJDBCException: could not load an entity: [org.jandersen.bliki.beans.User#james]
   at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:82)
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:70)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.loader.Loader.loadEntity(Loader.java:1359)
   at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:116)
   at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:101)
   at org.hibernate.persister.entity.BasicEntityPersister.load(BasicEntityPersister.java:2471)
   at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:351)
   at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:332)
   at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:113)
   at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:75)
   at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:643)
   at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:59)
   at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
   at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
   at org.jandersen.bliki.beans.User$$EnhancerByCGLIB$$99498555.setLocation(<generated>)
   at org.jandersen.bliki.junit.DAOTest.testSetUserLocation(DAOTest.java:78)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.SQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block
   at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1471)
   at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1256)
   at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:175)
   at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:388)
   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:329)
   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:239)
   at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:120)
   at org.hibernate.loader.Loader.getResultSet(Loader.java:1272)
   at org.hibernate.loader.Loader.doQuery(Loader.java:391)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
   at org.hibernate.loader.Loader.loadEntity(Loader.java:1345)
   ... 28 more

Name and version of the database you are using:
PostgreSQL 8.0 running on Linux

The generated SQL (show_sql=true):
This is the SQL statement that generates the primary key violation (expected).
Code:
Hibernate: insert into public.siteuser (email, name, img_name, location, password, rssfeed, website, create_date, username) values (?, ?, ?, ?, ?, ?, ?, ?, ?)


This is the SQL statement (just a select) that generates the following JDBC error:
ERROR: current transaction is aborted, commands ignored until end of transaction block
Code:
select user0_.username as username0_, user0_.email as email0_0_, user0_.name as name0_0_, user0_.img_name as img4_0_0_, user0_.location as location0_0_, user0_.password as password0_0_, user0_.rssfeed as rssfeed0_0_, user0_.website as website0_0_, user0_.create_date as create9_0_0_ from public.siteuser user0_ where user0_.username=?

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: close it
PostPosted: Fri Oct 07, 2005 1:59 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
http://www.hibernate.org/168.html

Quote:
A HibernateException means your have to rollback your transaction and close the Session.


That is why t2 does not work after failure of t1

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 11:57 pm 
Newbie

Joined: Fri Oct 07, 2005 12:24 am
Posts: 3
Location: Houston, TX
Thanks, I updated my failing test to be the following:

Code:
   public void testNewUser() {
      Session session = HibernateUtil.currentSession();
      Transaction tx = session.beginTransaction();
      try {
         User juser = new User("fred");
          juser.setLocation("Bollywood!");
          juser.setPassword("foobar");
          session.save(juser);
          System.out.println("Done");
          tx.commit();
          HibernateUtil.closeSession();
         assertTrue(true);
      } catch (Exception e) {
         tx.rollback();
         HibernateUtil.closeSession();
         fail(e.getMessage());
      }
   }


Now, with the rollback(), it no longer interferes with subsequent tests. Next time I'll try and do a better job scanning the documentation!


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.