捕获“ORA-00001: 违反唯一约束条件”异常并抛出业务相关的错误信息

荒凉一梦 提交于 2019-11-30 00:43:18

1.背景:T表F字段唯一索引,保存F字段重复的数据后台异常(附后),不做处理MVC框架包装异常后,前台显示"Could not execute JDBC batch update; nested exception is org.hibernate.exception...",报错信息不友好,故主动捕获后跑出友好业务相关信息"XXXXXXXXXX重复"。

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

3.异常详情:

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.解决过程:

so easy...直接捕获呗,从异常信息里找唯一约束索引号,如果找到不ok了嘛,代码见下:

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...捕获不了呀,系统仍然抛底层的错误信息。

oh!e.getMessage()得到的是"Could not execute JDBC batch update; nested exception is org.hibernate.exception",获取不到唯一约束索引号,这什么状况啊?

oh!错误信息在里面了,好吧,再往里一步,代码如下:

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!

5.总结:难的只是目前陌生的东西,碰过面的就不是难的了。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!