0

I've inherited an application and the persistence.xml has this:

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

The code uses @PersistenceUnit and @PersistenceContext but I'm reading the for transaction-type="RESOURCE_LOCAL" only @PersistenceUnit should be used. The app appears to be functioning properly and the majority of the time @PersistenceContext is used so I'm confused as to why they didn't use transaction-type="JTA".

I'm reluctant to change working code but we have noted performance issues so I'm wondering if this mixed usage can be contributing to that.

1 Answers1

1

Actually, it's completely independent notions.

  1. As for transaction types. As spring documentation says

Typically you need an application server’s JTA capability only if your application needs to handle transactions across multiple resources, which is not a requirement for many applications. Many high-end applications use a single, highly scalable database (such as Oracle RAC) instead.

  1. As for @PersistenceUnit and @PersistenceContext.

When you need to use EntityManagerFactory, you should use @PersistenceUnit. When you need to use EntityManager, you should use @PersistenceContext.

By the way, JPA 2 allow you to obtain an EntityManagerFactory from an existing EntityManager. See method EntityManager.getEntityManagerFactory()

See also this.

SternK
  • 11,649
  • 22
  • 32
  • 46