Bra*_*ads 9 mysql grails hibernate transactions grails-orm
摘要 父母可以有很多孩子.如何编写服务,如果在添加父项后添加子项时出错,则会回滚整个事务.例如,添加父p1,成功添加子c1,然后在添加子c2时发生错误,应回滚p1和c1.
详细问题
在以下代码中,对子项的name属性有一个唯一约束.因此,如果您尝试使用不同的父级添加相同的名称两次,则不应添加子记录,并且应回滚父记录.
我的问题是父记录没有被回滚.
我正在使用带有Grails 1.2-M2和Tomcat 6.018的带有InnoDB的MySQL.
数据源
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration
dataSource {
configClass = GrailsAnnotationConfiguration.class
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
dialect = org.hibernate.dialect.MySQLInnoDBDialect
zeroDateTimeBehavior="convertToNull" //Java can't convert ''0000-00-00 00:00:00' to TIMESTAMP
username = "root"
password = "12345"
loggingSql=false
}
hibernate {
cache.use_second_level_cache=true
cache.use_query_cache=true
cache.provider_class='com.opensymphony.oscache.hibernate.OSCacheProvider'
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop','update'
url = "jdbc:mysql://localhost:3306/transtest?zeroDateTimeBehavior=convertToNull"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://localhost:3306/transtest?zeroDateTimeBehavior=convertToNull"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://localhost:3306/transtest?zeroDateTimeBehavior=convertToNull"
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有以下简单的域类:
家长:
class Parent {
static hasMany = [ children : Child ]
String name
static constraints = {
name(blank:false,unique:true)
}
}
Run Code Online (Sandbox Code Playgroud)
儿童
class Child {
static belongsTo = Parent
String name
Parent parent
static constraints = {
name(blank:false,unique:true)
}
}
Run Code Online (Sandbox Code Playgroud)
简单数据输入GSP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sample title</title>
</head>
<body>
<h1>Add A Record</h1>
<g:form action="add" name="doAdd">
<table>
<tr>
<td>
Parent Name
</td>
<td>
Child Name
</td>
</tr>
<tr>
<td>
<g:textField name="parentName" />
</td>
<td>
<g:textField name="childName" />
</td>
</tr>
<tr><td><g:submitButton name="update" value="Update" /></td></tr>
</table>
</g:form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
调节器
class AddrecordController {
def addRecordsService
def index = {
redirect action:"show", params:params
}
def add = {
println "do add"
addRecordsService.addAll(params)
redirect action:"show", params:params
}
def show = {}
}
Run Code Online (Sandbox Code Playgroud)
服务
class AddRecordsService {
// boolean transactional = true //shouldn't this be all I need?
static transactional = true // this should work but still doesn't nor does it work if the line is left out completely
def addAll(params) {
println "add all"
println params
def Parent theParent = addParent(params.parentName)
def Child theChild = addChild(params.childName,theParent)
println theParent
println theChild
}
def addParent(pName) {
println "add parent: ${pName}"
def theParent = new Parent(name:pName)
theParent.save()
return theParent
}
def addChild(cName,Parent theParent) {
println "add child: ${cName}"
def theChild = new Child(name:cName,parent:theParent)
theChild.save()
return theChild
}
}
Run Code Online (Sandbox Code Playgroud)
您还需要确保在服务中抛出RuntimeException,以便自动回滚事务.
所以我会这样做:
def addParent(pName) {
println "add parent: ${pName}"
def theParent = new Parent(name:pName)
if(!theParent.save()){
throw new RuntimeException('unable to save parent')
}
return theParent
}
def addChild(cName,Parent theParent) {
println "add child: ${cName}"
def theChild = new Child(name:cName,parent:theParent)
theChild.save()
if(!child.save()){
throw new RuntimeException('unable to save child')
}
return theChild
}
Run Code Online (Sandbox Code Playgroud)
然后捕获控制器中的异常并呈现错误.
另一种方法是转换自动事务并使用Parent.withTransaction并在出现验证错误时手动标记事务以进行回滚.