简体   繁体   English

我的grails项目使用domain.save(flush:true).save()遇到错误“ TransactionRequiredException:no transaction”,它可以保存但正在更新

[英]My grails project facing error“TransactionRequiredException:no transaction” using domain.save(flush:true).save() it can is saving but updating

package com.fhjony.ocbt


import grails.web.servlet.mvc.GrailsParameterMap


class MemberService {

def save(GrailsParameterMap params) {
    Member member = new Member(params)
    def response = AppUtil.saveResponse(false, member)
    if (member.validate()) {
        member.save(true)
        if (!member.hasErrors()){
            response.isSuccess = true
        }
    }
    return response
}


def update(Member member, GrailsParameterMap params) {

    member.properties = params
    def response = AppUtil.saveResponse(false, member)
    if (member.validate()) {
        member.save(flush: true)
        if (!member.hasErrors()){
            response.isSuccess = true
        }
    }
    return response
}


def getById(Serializable id) {
    return Member.get(id)
}


def list(GrailsParameterMap params) {
    params.max = params.max ?: GlobalConfig.itemPerPage()
    List<Member> memberList = Member.createCriteria().list(params) {
        if (params?.colName && params?.colValue) {
            like(params.colName, "%" + params.colValue + "%")
        }
        if (!params.sort) {
            order("id", "desc")
        }
    }
    return [list: memberList, count: memberList.totalCount]
}


def delete(Member member) {
    try {

        member.delete(flush: true,failOnError:true)
    } catch (Exception e) {
        println(e.getMessage())
        return false
    }
    return true
}
}

Error message: 错误信息:

URI /member/update Class
javax.persistence.TransactionRequiredException Message null Caused by
no transaction is in progress
     Line | Method
 ->>  211 | invoke           in org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  |    188 | invoke           in org.grails.core.DefaultGrailsControllerClass |     90 | handle . . . .
 . in org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter |   1039
 | doDispatch       in
 org.springframework.web.servlet.DispatcherServlet |    942 | doService
 . . .  in     '' |   1005 | processRequest   in
 org.springframework.web.servlet.FrameworkServlet |    908 | doPost . .
 . . . in     '' |    882 | service          in     '' |     77 |
 doFilterInternal in

You want your database interactions to be happening in a transactional context. 您希望数据库交互在事务上下文中进行。 One simple piece of that is you can mark your service class with @grails.gorm.transactions.Transactional . 一个简单的步骤是,您可以使用@grails.gorm.transactions.Transactional标记您的服务类。

Separate from that, and this isn't really related to your question, but passing GrailsParameterMap map around as a method argument is an unusual thing to do. 与此分开,这与您的问题并没有真正的关系,但是将GrailsParameterMap映射作为方法参数传递是一件不寻常的事情。 What the right thing to do is depends on some factors in your app and you may want to pass values into your service rather than the whole map but if you really want the whole map in the service, one way to get there is by way of WebAttributes . 正确的做法取决于应用程序中的某些因素,您可能希望将值传递到服务中而不是整个地图中,但是如果您确实希望在服务中使用整个地图,那么一种到达那里的方法是WebAttributes

import grails.gorm.transactions.Transactional
import grails.web.api.WebAttributes

@Transactional
class MemberService implements WebAttributes {

    def serviceMethod() {
        // you can access params here because 
        // WebAttributes provides access to it
        Member member = new Member(params)

        // ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 执行domain.save()时,Grails触发查找并保存查询 - Grails firing find and save query when performing domain.save() 没有使用事务注释的活动事务错误,保存将无效 - Getting save is not valid without active transaction error on using transaction annotation Grails 2.4.2域最多只能保存8个字段 - Grails 2.4.2 Domain will not save more than 8 fields 错误javax.persistence.TransactionRequiredException:没有事务正在进行 - Error javax.persistence.TransactionRequiredException: no transaction is in progress 没有保存的grails域 - grails domain without saving it 使用 JPA hibernate 更新数据库时,有没有办法消除“TransactionRequiredException:执行更新/删除查询”错误? - Is there a way to eliminate the 'TransactionRequiredException: Executing an update/delete query' error when updating a database using JPA hibernate? grails不会保存但没有错误 - grails won't save but have no error Grails save方法给出错误拒绝值 - Grails save method gives error rejected value 尝试使JPA / Hibernate与REST配合使用— TransactionRequiredException:没有事务正在进行中错误 - Trying to Get JPA/Hibernate working with REST — TransactionRequiredException: No Transaction is in progress error 交易需要接收,没有正在进行的交易 - Transactionrequiredexception, no transaction in progress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM