-->
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.  [ 5 posts ] 
Author Message
 Post subject: How do I get my java class to detect SQLException
PostPosted: Fri Aug 20, 2004 1:30 pm 
Newbie

Joined: Fri Aug 20, 2004 9:29 am
Posts: 3
Location: London, England
I have a general beginners question - how do I detect specific exceptions when I call dao.save() in Hibernate - specifically I have a unique index on a code column on my table and when I attempt to save a non-unique value it will give me a "Duplicate key" error in the stack dump - but dao.save()/Hibernate only seems to want to return Exception. Is there a way of getting more granular errors from the call?

.. and here's the code if you want to see.....

Hibernate version:2.1

Code between sessionFactory.openSession() and session.close():
try {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction txn = session.beginTransaction();
_RootDAO.initialize();
LoginDAO dao = new LoginDAO();
Login login = new Login();
login.setLoginNm("John Smith");
login.setLoginCd("js");
dao.save(login);
txn.commit();
session.close();
} catch (Exception e) {
System.out.println("main Exception " + e.toString());
}



Full stack trace of any exception that occurs:

could not insert: [com.test.hibernate.mapping.Login] main Exception net.sf.hibernate.JDBCException: could not insert: [com.test.hibernate.mapping.Login]
20-Aug-2004 18:19:58 net.sf.hibernate.JDBCException <init>
SEVERE: could not insert: [com.test.hibernate.mapping.Login]
java.sql.SQLException: Duplicate key or integrity constraint violation, message from server: "Duplicate entry 'js' for key 3"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1977)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1163)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1272)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2236)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1741)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1588)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:528)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:432)
at net.sf.hibernate.impl.ScheduledIdentityInsertion.execute(ScheduledIdentityInsertion.java:29)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:932)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:775)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
at com.test.hibernate.mapping.base._BaseRootDAO.save(_BaseRootDAO.java:559)
at com.test.hibernate.mapping.base._BaseRootDAO.save(_BaseRootDAO.java:540)
at com.test.hibernate.mapping.base.BaseLoginDAO.save(BaseLoginDAO.java:60)
at com.mzuzu.test.hibernate.mapping.dao.LoginDAO.main(LoginDAO.java:81)
net.sf.hibernate.JDBCException: could not insert: [com.test.hibernate.mapping.Login]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 21, 2004 4:47 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
The signature of the save method is
Code:
public Serializable save(Object object)
                  throws HibernateException

catch (or cast to) a HibernateException and look for the causing exception by calling getCause().

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 8:53 am 
Newbie

Joined: Fri Aug 20, 2004 9:29 am
Posts: 3
Location: London, England
Thanks - that works - although I've opted to ignore save() and go for saveOrUpdate() method in _RootDAO.java - and I've added some deadlock handling too....

Code:
protected void saveOrUpdate(Object obj) throws HibernateDuplicateKeyException, HibernateException {
      Transaction t = null;
      Session s = null;

      boolean deadlock = true;

      while (deadlock) {
          deadlock = false;
         try {
            s = getSession();
            t = beginTransaction(s);
            saveOrUpdate(obj, s);
            commitTransaction(t);
         } catch (HibernateException e) {
            if (null != t)
               t.rollback();
            String error = e.getCause().toString();
            if (error.indexOf("Deadlock") != -1) {
               deadlock = true;
            }
            if (error.indexOf("Duplicate key or integrity constraint violation") != -1) {
               throw new HibernateDuplicateKeyException(error);
            }
            throw e;
         } finally {
            closeSession(s);
         }
      } // while
   }


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 9:20 am 
Regular
Regular

Joined: Thu Aug 05, 2004 2:27 am
Posts: 54
Location: South Africa
sorry I miss the point of this loop, maybe you can clear it up for me.

after catching the exception and rolling back, you have
Code:
if (error.indexOf("Deadlock") != -1) {
               deadlock = true;
            }


then you throw e;
so what exactly does the boolean deadlock do?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 24, 2004 9:31 am 
Newbie

Joined: Fri Aug 20, 2004 9:29 am
Posts: 3
Location: London, England
whoops - you're right ... it should read...

Code:
   protected void saveOrUpdate(Object obj) throws HibernateDuplicateKeyException, HibernateException {
      Transaction t = null;
      Session s = null;

      boolean deadlock = true;

      while (deadlock) {
          deadlock = false;
         try {
            s = getSession();
            t = beginTransaction(s);
            saveOrUpdate(obj, s);
            commitTransaction(t);
         } catch (HibernateException e) {
            if (null != t)
               t.rollback();
            String error = e.getCause().toString();
            if (error.indexOf("Duplicate key or integrity constraint violation") != -1) {
               throw new HibernateDuplicateKeyException(error);
            }
            if (error.indexOf("Deadlock") != -1) {
               deadlock = true;
            }
            else {
                throw e;
            }
         } finally {
            closeSession(s);
         }
      } // while
   }


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