首页 > 解决方案 > 如何强制 Grails GORM 从数据库中检索信息(.refresh() 不起作用)

问题描述

我有一个问题,在 Grails 中,我在数据库记录上设置了一个标志——另一个进程看到了这个标志并更新了记录状态,然后我在 grails 中循环遍历这条记录以获取更改的状态。

问题是 grails 永远不会再次从数据库中检索记录,并且总是将我第一次获取的记录返回给我。我试过了.refresh() .withNewTransaction().withNewSession但都没有成功。

所以我的主要问题是如何强制 gorm 从数据库中重新获取我的实例

以下是我的代码不起作用:

    MyDomain.withNewTransaction {
        MyDomain myDomainInstance = MyDomain.get(params.id);

        myDomainInstance.opstatus = 9 /*REQUEST CANCEL OPERATION*/

        myDomainInstance.save();
        if (myDomainInstance.hasErrors()) {
            log.info "Cancel transaction error !: ";
            flash.message = message(code: 'error.occured');
            render view: "index"
            return
        }
    }

 MyDomain myDomainInstance = MyDomain.get(params.id);
        while (myDomainInstance.status == Status.SUCCESS.value /*the value should have changed to failed from the other process*/) {
            sleep(500);
            myDomainInstance.refresh();
        }

我添加到日志中

我正在刷新

2018-07-11 16:25:51,850 [http-bio-8080-exec-8] TRACE internal.DefaultRefreshEventListener  - Refreshing 
2018-07-11 16:25:52,180 [http-bio-8080-exec-8] TRACE internal.DefaultLoadEventListener  - Loading entity: [MyDomain#1113507]
2018-07-11 16:25:52,180 [http-bio-8080-exec-8] TRACE internal.DefaultLoadEventListener  - Attempting to resolve: [MyDomain#1113507]
2018-07-11 16:25:52,180 [http-bio-8080-exec-8] TRACE internal.DefaultLoadEventListener  - Resolved object in session cache: [MyDomain#1113507]

我猜我需要以某种方式清除我的会话缓存?我怎样才能做到这一点

标签: grailsgrails-orm

解决方案


推荐阅读