【问题标题】:Can @PersistenceUnit and @PersistenceContext be used in the same application@PersistenceUnit 和 @PersistenceContext 可以在同一个应用程序中使用吗
【发布时间】:2020-02-27 03:47:11
【问题描述】:

我继承了一个应用程序,persistence.xml 有这个:

    <persistence-unit name="nxPersistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>someDSName</non-jta-data-source>

代码使用 @PersistenceUnit 和 @PersistenceContext 但我正在阅读 for transaction-type="RESOURCE_LOCAL" 仅应使用 @PersistenceUnit。该应用程序似乎运行正常,并且大部分时间都使用@PersistenceContext,所以我对他们为什么不使用 transaction-type="JTA" 感到困惑。

我不愿意更改工作代码,但我们已经注意到性能问题,所以我想知道这种混合使用是否会导致这种情况。

【问题讨论】:

    标签: spring hibernate


    【解决方案1】:

    其实是完全独立的概念。

    1. 至于交易类型。正如spring documentation 所说

    通常,只有当您的应用程序需要处理跨多个资源的事务时,您才需要应用程序服务器的 JTA 功能,而这对许多应用程序来说不是必需的。许多高端应用程序使用单一的、高度可扩展的数据库(例如 Oracle RAC)。

    1. 至于@PersistenceUnit@PersistenceContext

    当你需要使用EntityManagerFactory时,你应该使用@PersistenceUnit。 当你需要使用EntityManager时,你应该使用@PersistenceContext

    顺便说一句,JPA 2 允许您从现有的 EntityManager 获取 EntityManagerFactory。见方法EntityManager.getEntityManagerFactory()

    另见this

    【讨论】:

      猜你喜欢
      • 2014-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多