捕獲「ORA-00001: 違反惟一約束條件」異常並拋出業務相關的錯誤信息

1.背景:T表F字段惟一索引,保存F字段重複的數據後臺異常(附後),不作處理MVC框架包裝異常後,前臺顯示"Could not execute JDBC batch update; nested exception is org.hibernate.exception...",報錯信息不友好,故主動捕獲後跑出友好業務相關信息"XXXXXXXXXX重複"。java

2.框架:Hibernate+Spring+公司MVC。web

3.異常詳情:spring

Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
        at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:636)
        at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
        at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
        at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
        at org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:890)
        ......
        at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2086)
        at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1406)
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
        at org.springframework.orm.hibernate3.HibernateTemplate$28.doInHibernate(HibernateTemplate.java:892)
        at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
        ... 60 more
Caused by: java.sql.BatchUpdateException: ORA-00001: 違反惟一約束條件 (IDX_T_A)

        at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:566)
        at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:9365)
        at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:210)
        at weblogic.jdbc.wrapper.PreparedStatement.executeBatch(PreparedStatement.java:191)
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
        ... 67 more

4.解決過程:sql

so easy...直接捕獲唄,從異常信息裏找惟一約束索引號,若是找到不ok了嘛,代碼見下:oracle

try {
	busiDAO.save(bean);
} catch(RuntimeException e) {
	String errMsg = e.getMessage();
	if(StringUtils.isNotBlank(errMsg) && errMsg.indexOf("IDX_T_A")!=-1) {
		throw new RuntimeException("XXXXXXXXXX重複!");
	}
	throw e;
}

no...no...捕獲不了呀,系統仍然拋底層的錯誤信息。app

oh!e.getMessage()獲得的是"Could not execute JDBC batch update; nested exception is org.hibernate.exception",獲取不到惟一約束索引號,這什麼情況啊?框架

oh!錯誤信息在裏面了,好吧,再往裏一步,代碼以下:hibernate

try {
	busiDAO.save(bean);
} catch(RuntimeException e) {
	Throwable cause = e.getCause();
	if(cause instanceof org.hibernate.exception.ConstraintViolationException) {
		String errMsg = ((org.hibernate.exception.ConstraintViolationException)cause).getSQLException().getMessage();
		if(StringUtils.isNotBlank(errMsg) && errMsg.indexOf("IDX_T_A")!=-1) {
			throw new RuntimeException("XXXXXXXXXX重複!");
		}
	}
	throw e;
}

ok!code

5.總結:難的只是目前陌生的東西,碰過面的就不是難的了。orm

相關文章
相關標籤/搜索