withTransaction Doesn't Do Rollbacks Correctly
In my Person controller Delete action, I expect that if an DataIntegrityViolationException occurs, then the associated Authority records would be restored, but they are not. I really shouldn't even need to do an explicit rollback anyway, but with or without it, the rollback doesn't happen. It appears that withTransaction rollbacks do not work. Or am I doing something wrong? I'm using Grails 1.3.7 and MySQL (innodb).
Person.withTransaction { status ->
Authority.findAll().each { it.removeFromPeople(person) }
try {
person.delete()
flash.message = "person.deleted"
flash.args = [params.id]
flash.defaultMessage = "User ${params.id} deleted"
redirect(action: "list")
}
catch (org.springframework.dao.DataIntegrityViolationException e) {
status.setRollbackOnly()
flash.message = "开发者_JAVA技巧person.not.deleted"
flash.args = [params.id]
flash.defaultMessage = "User ${params.id} could not be deleted"
redirect(action: "show", id: params.id)
}
}
UPDATE
I realized that some of my tables where not innodb. I corrected that and at first it looked like the problem was solved. I could try to delete the user record, get the error and then go and login as that user. Before, the login failed with this error, meaning that the Authority records had not been rolled back.
ERROR springsecurity.GrailsDaoImpl - User [admin9] has no GrantedAuthority
So all looked good. Then I restarted my application, tried to login and got the error again. So even though the rollback appeared to work, the records where never persisted to the database.
Can it be that you're executing this code from another transaction? Maybe, from a service method, that is transactional by default?
MySQL does not support nested transactions.
精彩评论