Spring的LocalContainerEntityManagerFactoryBean可以在J2SE环境中使用吗?

发布于 2024-10-31 20:51:09 字数 526 浏览 1 评论 0原文


我在 Glassfish 中使用 Spring,并且需要对其进行配置,以便它也可以在容器外部工作,主要用于开发目的。
我不确定并且找不到答案的是,我是否可以在没有容器的情况下使用 LocalContainerEntityManagerFactoryBean 类。
从它的名字来看,LocalContainer,似乎我可以,但是在 docs 它说:

创建 JPA 的 FactoryBean EntityManagerFactory根据 JPA 的标准容器引导程序 合同

所以我不确定这个问题。

谢谢,
一泰

I'm using Spring in Glassfish and I have the need to configure it so it also works outside of the container, mainly for development purposes.
What I'm uncertain of, and couldn't find the answer to, was whether I can use the LocalContainerEntityManagerFactoryBean class without a container.
From its name, LocalContainer, it seems I can but in the docs it says:

FactoryBean that creates a JPA
EntityManagerFactory according to
JPA's standard container bootstrap
contract

so I'm uncertain about this issue.

Thanks,
Ittai

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

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

发布评论

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

评论(2

○愚か者の日 2024-11-07 20:51:09

我只是想指出 Spring 支持在容器外部运行 JPA 内容,并且不需要任何事务管理器的方式。要问的问题是您是否正在使用 Spring 的声明式事务管理(例如“@Transactional”)。

如果是,那么您需要提供“PlatformTransactionManager”的实现。在这里,您仍然不需要使用完整的 JTA 支持(如上例中 Atomikos 提供的那样)。只要您不使用“XA”做任何事情,您就可以简单地使用 JpaTransactionManager 实例(它需要对实体管理器工厂的引用) “等等。如果你正在做 XA,那么 Atomikos、Bitronix 或任何其他选项都可以。你可以看看这个例子 http://blog.springsource.com/2011/08/15/configuring-spring-and-jta- without-full-java-ee/ 演示了如何使用 JTA(例如,使用 JPA 和 JMS),

因此,重申一下,如果您只是执行简单的 JPA(连接到一个数据库),那么您就不需要这样做。不需要 JTA,并且您绝对不需要 GlassFish 如果您需要 XA,那么您仍然可以使用上面建议的第三方 JTA 提供程序,并且您仍然不需要 Glassfish

最后,如果您确实愿意的话 。如果要同时维护 GlassFish + JTA 和一个仅在本地工作的单独 JPA,以便在更快的容器上进行快速开发,您可能会考虑即将推出的 Spring 3.1,它具有“配置文件”功能,允许您有条件地为每个环境定义 Bean(例如,“生产、 ”或“开发”或“云”,或者任何你想要的。)

I just wanted to note that Spring supports running the JPA stuff outside of a container, and doesn't require anything in the way of a transaction manager. The question to ask is whether you are using Spring's declarative transaction management (e.g., "@Transactional").

If you are, then you need to provide an implementation of "PlatformTransactionManager." Here still, you do NOT need to use full on JTA support (as provided by Atomikos in the above example. YOu can simply use a JpaTransactionManager instance (which expects a reference to the entity manager factory) provided you are not doing anything with "XA" etc. If you are doing XA, then Atomikos, or Bitronix or any of a number of other options are just fine. You might look at this example http://blog.springsource.com/2011/08/15/configuring-spring-and-jta-without-full-java-ee/ which demonstrates how to use JTA (with JPA and JMS, for example).

So, reiterating, if you're just doing simple JPA (connecting to one database) then you don't need JTA, and you definitely don't need GlassFish. If you need XA, then you can still use a third party JTA provider as the responder above suggested, and you still don't need Glassfish.

Finally, if you truly wish to maintain both GlassFish + JTA, and a separate JPA that works only locally for rapid development on a faster container, you might consider the imminent Spring 3.1, which features "profiles" to allow you to conditionally define beans per environment (e.g., "production," or "dev," or "cloud," or whatever you'd like.)

酒儿 2024-11-07 20:51:09

是的,这是可能的,但你需要提供一个事务管理器(如 Atomikos)。其余配置相同。
这是一个例子:

<bean id="userTransactionService" class="com.atomikos.icatch.config.UserTransactionServiceImp"
    init-method="init" destroy-method="shutdownForce">
</bean>

<bean id="AtomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
    init-method="init" destroy-method="close" depends-on="userTransactionService">
    <property name="forceShutdown" value="true" />
</bean>

<bean id="AtomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"
    depends-on="userTransactionService">
    <property name="transactionTimeout" value="300" />
</bean>

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"
    depends-on="userTransactionService">
    <property name="transactionManager" ref="AtomikosTransactionManager" />
    <property name="userTransaction" ref="AtomikosUserTransaction" />
</bean>

<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
....
</bean>

Yes, it's possible, but you need to provide a transaction manager (like Atomikos). The rest of the configuration is the same.
This is an example:

<bean id="userTransactionService" class="com.atomikos.icatch.config.UserTransactionServiceImp"
    init-method="init" destroy-method="shutdownForce">
</bean>

<bean id="AtomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
    init-method="init" destroy-method="close" depends-on="userTransactionService">
    <property name="forceShutdown" value="true" />
</bean>

<bean id="AtomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"
    depends-on="userTransactionService">
    <property name="transactionTimeout" value="300" />
</bean>

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"
    depends-on="userTransactionService">
    <property name="transactionManager" ref="AtomikosTransactionManager" />
    <property name="userTransaction" ref="AtomikosUserTransaction" />
</bean>

<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
....
</bean>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文