2014-03-13 42 views
2

我有它運行,如果沒有異常被拋出預期這個非常簡單的代碼片段:聲明前述它

try { 
    session = HibernateHelper.getSessionFactory().openSession(); 
    session.beginTransaction(); 
    OracleTables.writeToLogTable(thisLogRecord); // <<<< this throws a java.sql.SQLException! 


    boolean isSuccess = OracleTables.writeToActualDataTable(thisDataRecord); 
    if (isSuccess) { 
    System.out.println("writeToActualDataTable() successful."); 
    } 
} 
catch (RuntimeException e) { 
    if (e != null && e.getMessage() != null && e.getMessage().contains("No data to write")) { 
    System.out.println("No more data to write."); 
    System.exit(0); 
    } 
    else 
    throw e; 
} 

但如果有一些約束衝突中的第一個數據庫寫入(例如,試圖插入一個NULL不允許它的字段),一個java.sql.SQLException被拋出(這很好),但由於一些奇怪的原因,OracleTables.writeToActualDataTable()語句正在執行!

org.hibernate.exception.ConstraintViolationException: could not execute statement 
     at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129) 
     at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) 
     at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) 
     at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) 
     at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136) 
     at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:58) 
     at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3067) 
     at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3509) 
     at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88) 
     at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377) 
     at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369) 
     at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:286) 
     at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339) 
     at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52) 
     at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234) 
     at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404) 
     at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) 
     at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175) 
     at com.nicecor.test.myclient.OracleTables.writeToLogTable(OracleTables.java:159) 
     at com.nicecor.test.myws.ServicePort_ServiceSoapPort_Client.main(ServicePort_ServiceSoapPort_Client.java:180) 
     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:597) 
     at com.simontuffs.onejar.Boot.run(Boot.java:340) 
     at com.simontuffs.onejar.Boot.main(Boot.java:166) 
Caused by: java.sql.SQLException: ORA-01400: cannot insert NULL into ("USR"."LOG_TABLE"."CREATE_TMSTMP") 

這很奇怪。我認爲Java中的異常會中止執行,並立即轉到catch子句。

什麼能解釋這種奇怪的行爲?

我錯過了什麼?

+4

OracleTables.writeToLogTable()可能會捕獲異常並在繼續執行時向System.err打印內容,但是我不知道如何執行代碼的更多細節。 – NESPowerGlove

+2

誰在捕捉和打印異常? – Arkadiy

+0

@NESPowerGlove OMG你是對的!請發佈這個答案,我會接受。謝謝! – WebViewer

回答

1

OracleTables.writeToLogTable()捕獲異常並在繼續執行時將內容打印到System.err中。