Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to catch SQLException in the hibernate ?

843785Nov 13 2008 — edited Nov 13 2008
Hi ~ , I am a newbie to java , recently I do a project that need to use hibernate to do insert data into database(db2) , but my program always encounter a DB deadlock problem , which throw an exception indicated that
"{code}DB2 SQL error: SQLCODE: -911, SQLSTATE: 40001, SQLERRMC: 2{code}"
so, in this case , i want to try catch block handle this exception , and the code is as follows:
public static void main(String[] args){

try {

Configuration cfg =

new Configuration().configure();

SessionFactory sf = cfg.buildSessionFactory();

Session session = sf.openSession();

Transaction tx = session.beginTransaction();



try {

................

session.save(obj1);

session.save(obj2);

................

} 



tx.commit();



session.close(); 



}catch (SQLException se) { 

// ***** here it indicate an error that "This exception is never thrown from the try statement body" *****

}







}catch (HibernateException e) {

e.printStackTrace();

} 

}
but in fact , while running the programe , it does throw a SQLException that abend the program , how would I not be able to catch it ? , here I provide the error info :
Hibernate: insert into TBCUSRPQ (CUST_NAME, CUST_SALUT, LAST_UPDT_BY, LANG, VER, FNL_RSK_LVL_CDE, SYS_RSK_LVL_CDE, CUS_RSK_LVL_CDE, NO_OF_ANS, ANS_REF_ID, DTL_INFO, BANK_NO, CTRY_CDE, RPQ_GP, CUST_ID_TYPE, CUST_ID_NO, LAST_UPDT_DT, LAST_UPDT_TM) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Hibernate: insert into TBCUSANS (ANS_LIST, ANS_REF_ID, QUEST_NUM) values (?, ?, ?)

Nov 5, 2008 12:04:17 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions

WARNING: SQL Error: -911, SQLState: 40001

Nov 5, 2008 12:04:17 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions

SEVERE: DB2 SQL error: SQLCODE: -911, SQLSTATE: 40001, SQLERRMC: 2

Nov 5, 2008 12:04:17 PM net.sf.hibernate.impl.SessionImpl execute

SEVERE: Could not synchronize database state with session

net.sf.hibernate.exception.GenericJDBCException: could not insert: [com.hsbc.rpq.importer.vo.ris.CustomerAnswer#com.hsbc.rpq.importer.vo.ris.CustomerAnswer@87235980]

at net.sf.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:80)

at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:69)

at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)

at net.sf.hibernate.persister.AbstractEntityPersister.convert(AbstractEntityPersister.java:1331)

at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:472)

at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:436)

at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:37)

at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2449)

at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2435)

at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2392)

at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2261)

at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)

at com.hsbc.rpq.importer.TestDB.main(TestDB.java:78)

{color:#ff0000}Caused by: com.ibm.db2.jcc.c.SqlException: DB2 SQL error: SQLCODE: -911, SQLSTATE: 40001, SQLERRMC: 2{color}

at com.ibm.db2.jcc.c.fg.e(fg.java:1596)

at com.ibm.db2.jcc.b.gb.o(gb.java:727)

at com.ibm.db2.jcc.b.gb.g(gb.java:143)

at com.ibm.db2.jcc.b.gb.a(gb.java:39)

at com.ibm.db2.jcc.b.w.a(w.java:34)

at com.ibm.db2.jcc.b.vb.g(vb.java:139)

at com.ibm.db2.jcc.c.fg.n(fg.java:1177)

at com.ibm.db2.jcc.c.gg.eb(gg.java:1862)

at com.ibm.db2.jcc.c.gg.d(gg.java:2295)

at com.ibm.db2.jcc.c.gg.W(gg.java:457)

at com.ibm.db2.jcc.c.gg.executeUpdate(gg.java:440)

at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)

at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:462)

... 8 more
I tried a lot , but couldn't solve the problem, could anyone help me , how to catch a SQLExcepion in this case , thanks in advance !!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 11 2008
Added on Nov 13 2008
8 comments
10,461 views