如何从 Grails 服务中访问 EntityManager (JPA + GAE)

发布于 2024-10-03 04:52:04 字数 972 浏览 0 评论 0原文

我在从 grails 服务中访问 EntityManager 时遇到问题:

我的设置如下...

  • 带有 AppEngine 和 GORM-JPA 插件的基本 Grails 应用程序
  • 几乎所有内容的默认设置。我还没有接触过 resources.groovy、persistence.xml 等。

有些东西很好...

  • 我只需添加“defentityManager”就可以从控制器内访问 EntityManager。

但是...

  • 我有一个服务,我试图从中访问 EntityManager,但是,我收到“java.lang.IllegalStateException:EntityManager 已关闭!”例外。
  • 插件是否在某处关闭了 EntityManager?我是否需要以某种方式更改 EntityManager 的范围?我是否需要更新一些 XML 文件以确保正确注入?

    class GoogleCalendarService 实现 InitializingBean {
        无效 afterPropertiesSet() {
        }
    
    
    
     def 实体管理器
    
    
       公共 OAuthToken getAccessToken(用户 u) {
           //无法从这里访问entityManager
           entityManager.newQuery(...) //抛出IllegalStateException
       }
    
    }

一个奇怪的注释: 由于某种原因,如果我在 Jetty 启动并运行时重新保存服务,则该服务只能访问 EntityManager 一次。如果我单击重新加载(并让控制器再次访问该服务),该服务将无法再访问 EntityManager...

I'm having trouble accessing the EntityManager from within a grails service:

My setup is as follows...

  • Basic Grails application with the AppEngine and GORM-JPA plugins
  • Default settings for pretty much everything. I haven't touched resources.groovy, persistence.xml etc.

Some things are good...

  • I am able to access the EntityManager from within controllers by simply adding "def entityManager".

But...

  • I have a service that I'm trying to access the EntityManager from, however, I get a "java.lang.IllegalStateException: EntityManager is already closed!" exception.
  • Is a plugin closing the EntityManager somewhere? Do I need to change the scope of the EntityManager somehow? Is there some XML file I need to update to ensure proper injection?

    class GoogleCalendarService implements InitializingBean {
        void afterPropertiesSet() {
        }
    
    
    
       def entityManager
    
    
       public OAuthToken getAccessToken(User u) {
           //can't access entityManager from here
           entityManager.newQuery(...) //throws an IllegalStateException
       }
    

    }

One weird note:
For some reason, if I re-save the service while Jetty is up and running the service is able to access the EntityManager just once. If I click reload (and have the controller access the service again) the service can no longer access the EntityManager...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮华 2024-10-10 04:52:04

我想正确的方法是使用 EntityManagerFactory,如下所示:

import org.springframework.orm.jpa.EntityManagerFactoryUtils

class GoogleCalendarService implements InitializingBean {
    void afterPropertiesSet() {

    }

    def entityManagerFactory
    EntityManager em

    public OAuthToken getAccessToken(User u) {
        em = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory)
        //do stuff with em
    }
}

I guess the right way to do this is use the EntityManagerFactory, like this:

import org.springframework.orm.jpa.EntityManagerFactoryUtils

class GoogleCalendarService implements InitializingBean {
    void afterPropertiesSet() {

    }

    def entityManagerFactory
    EntityManager em

    public OAuthToken getAccessToken(User u) {
        em = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory)
        //do stuff with em
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文