我在save()期间遇到util.JDBCExceptionReporter问题。
码:
membership.withTransaction { status ->
try{
def membership = membership.findByUserId(userId)
if(!membership){
membership = new membership(userId: userId, email: userEmail, baseLevel: membership.findByName(membershipName.MEMBER)).save(flush: true)
}
}catch(org.springframework.dao.DataIntegrityViolationException e){
status.setRollbackOnly()
return false
}catch(all){
println "Insert Membership Exception.\n User Id: $userId\n " + e
}
当我创建两个线程来运行此代码时,它将引发错误:
2014-05-06 12:53:07,034 [Actor Thread 5] ERROR util.JDBCExceptionReporter - Duplicate entry 'test@gmail.com' for key 'email'
当两个线程执行相同的插入操作时,我不想每次都显示此错误,因为第一个线程将成功通过并插入,所以我并不在乎第二个。
我的问题是如何捕获util.JDBCExceptionReporter?
谢谢。
最佳答案
正如lsidroGH所说,util.JDBCExceptionReporter
也不异常(exception),它是一条日志消息。它同时记录SQLExceptions
和SQLWarnings
。您的代码没有问题,因为一个线程将调用save()
返回true
,而另一个线程的save()
将获得false
。
如果您不希望此消息出现在日志中,则需要将org.hibernate.util.JDBCExceptionReporter
的日志级别从ERROR
增加到FATAL
,但这可能会排除您希望记录的有效异常。最好的选择是在代码正常工作时忽略它。
关于exception - Groovy Gorm在save()上捕获util.JDBCExceptionReporter错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23500819/