Ajax调用从grails show.gsp保存数据接收“Session is Closed!”例外

时间:2016-08-19 13:27:15

标签: jquery ajax hibernate session grails

我正在尝试在复选框上进行ajax调用,该复选框会在单击复选框时将用户角色保存或删除到数据库。

AJAX:

$('.roleCheckbox').click(function() {
var checked = $(this).is(':checked');

//var id = url.substring(url.lastIndexOf('/') + 1);

//alert($(this).attr("context") + "/userRole/addUserRole/");
var inputData = {userid: $(this).attr('userid'),
                    roleid: $(this).val()};

if(checked){
    //alert( $(this).attr("context") + "/userRole/addUserRole/");
    //ajax add userRole
    $.ajax({
        url: $(this).attr("context") + "/userRole/addUserRole/",
        method:"POST",
        contentType:"application/json; charset=utf-8",
        data: JSON.stringify(inputData),
        cache: false,
        success: function(data) {
            alert("success updating role " + $(this.val()));
        },
        error: function(request, status, error) {
            //alert("error updating role ");
        },
        complete: function() {
            //alert("complete");
        }
    });
}else{
    //ajax remove userRole
    alert("in unchecked condition: " + $(this).attr("context")  + "/userRole/addUserRole/");
    $.ajax({
        url: $(this).attr("context") + "/userRole/removeUserRole/",
        data: JSON.stringify(inputData),
        cache: false,
        success: function(data) {
            alert("success deleting role " + $(this.val()));
        },
        error: function(request, status, error) {
            alert("error deleting role " + $(this.val()) + " " + error);
        },
        complete: function() {

        }
    });
}
});

控制器:

 @Transactional
def addUserRole() {

    def jsonObj = request.JSON

    UserService.addUserRole(jsonObj)


}

服务:

@Transactional
def addUserRole(JSONObject jsonObj) {

    def currentSession = sessionFactory.getCurrentSession()

    UserRole.withTransaction{ status ->
        def user = User.get(jsonObj.userid)
        def role = Role.get(jsonObj.roleid)

        def userRole = new UserRole();

        userRole.user = user
        userRole.role = role

        userRole.validate();
        if(userRole.hasErrors()){
            flash.message = userRole.errors
            respond user
        }else{
            currentSession.save(userRole)
        }
    }

    currentSession.flush()

}

问题是当控制器功能返回时我得到一个错误:

org.hibernate.SessionException:会话已关闭!

Grails 3.2.0.M2

我看了一遍,无法弄清楚如何正确管理这些会话。

谢谢,

编辑:

将render(status:200)添加到控制器的末尾使得ajax调用成功返回,但这只是掩盖了“Session is Closed!”我收到的例外情况。

我尝试了使用.withTransaction而不是.withNewSession的建议 在服务中具有相同的结果。

我认为这可能需要使用spring-security-plugin做更多事情,因为即使grails scaffold创建UserRoleController方法save()也会发出“Session is Closed!”保存时例外。

您可以在运行时更新UserRole(PersonAuthority)域吗?

1 个答案:

答案 0 :(得分:0)

不要手动刷新会话。让Grails / Hibernate为您做这件事。请考虑更改为此代码:

@Transactional
def addUserRole(JSONObject jsonObj) {
    UserRole.withTransaction { status ->
        def user = User.get(jsonObj.userid)
        def role = Role.get(jsonObj.roleid)

        def userRole = new UserRole();

        userRole.user = user
        userRole.role = role

        userRole.validate()
        if (userRole.hasErrors()) {
            flash.message = userRole.errors
            return
        } else {
            currentSession.save(flush: true)
        }
    }    
}

另外,在行动的最后添加render(status: 200)