Reputation: 84
I have a problem to catch util.JDBCExceptionReporter during save(). Code:
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
}
When I create two thread to run this code, it throw a error:
2014-05-06 12:53:07,034 [Actor Thread 5] ERROR util.JDBCExceptionReporter - Duplicate entry '[email protected]' for key 'email'
I don't want to show this error every time when their has two threads doing the same insert, because the first thread will go through and insert successfully, so I don't really care about second one.
My question is how to catch util.JDBCExceptionReporter?
Thanks.
Upvotes: 1
Views: 311
Reputation: 19682
As lsidroGH said, util.JDBCExceptionReporter
is not an exception, it's a log message. It logs both SQLExceptions
and SQLWarnings
. There is no problem with your code, as one thread will have a save()
call that returns true
and the other thread's save()
will get false
.
If you don't want this message to show up in your logs, you will need to increase your log level for org.hibernate.util.JDBCExceptionReporter
from ERROR
to FATAL
but this will potentially exclude valid exceptions you would want logged. Your best bet is to ignore it, as your code works.
Upvotes: 0
Reputation: 2037
Just guessing:
By default Grails doesn't throw exceptions when saving. To throw integrity exceptions you have to use save(failOnError: true)
.
So in this case, it's just an internal trace (util.JDBCExceptionReporter
is not an exception).
In your case, instead of capturing exceptions I'd use validate
before saving so you can get the integrity errors before trying to save.
Upvotes: 1