-1

I'm using Eclipselink's implementation of JPA and this is how I'm instantiating persistence context:

@PicketLink
@PersistenceContext(unitName = "txPersistUnit.security")
private EntityManager txEmSec;

this is persistence unit defitnition:

<persistence-unit name="txPersistUnit.security" transaction-type="RESOURCE_LOCAL">
        ...
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="javax.persistence.target-database" value="PostgreSQL"/>
            <property name="eclipselink.cache.shared.default" value="true"/>
            ...
            <!-- EclipseLink should create the database schema automatically -->
            <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
            <property name="eclipselink.ddl-generation.output-mode"
                      value="database"/>
        </properties>
</persistence-unit>

so, you can see I am setting RESOURCE_LOCAL as a transaction-type but I'm getting this error when deploying:

java.io.IOException: com.sun.enterprise.admin.remote.RemoteFailureException: Error occurred during deployment: Exception while preparing the app : The persistence-context-ref-name [com.txsolutions.manager.PersistenceManager/txEmSec] in module [txAPI] resolves to a persistence unit called [txPersistUnit.security] which is of type RESOURCE_LOCAL. Only persistence units with transaction type JTA can be used as a container managed entity manager. Please verify your application.. Please see server.log for more details.

Server is Glassfish 4.0.1 Question is why is glassfish not deploying this application succesfully when transaction-type set to RESOURCE_LOCAL? I'm emphasizing that I have RESOURCE_LOCAL persistence unit in that same application on that same server deployed.

Now, when I create entity manager like this:

..declarations omitted..
factory = Persistence.createEntityManagerFactory("txPersistUnit.security");
entityManager = factory.createEntityManager();

it is created sucessfuly even with RESOURCE_LOCAL as for transaction type.

So all in all whats the difference between this two approaches?

Thanks!

greengold
  • 1,184
  • 3
  • 18
  • 43

1 Answers1

2

Since you are running your code in an JEE compliant Application Server (i.e. Glassfish), your transaction type should be JTA.

<persistence-unit name="txPersistUnit.security" transaction-type="JTA">

RESOURCE_LOCAL is generally used for Standalone Java SE applications.

Since you are using @PersistenceContext, means that you are using a Container-Managed entity manager/persistence context. Since it is container-managed, it requires you to set your transaction type to JTA.

I suggest you try using an Application-Managed persistence context. Use @PersistenceUnit to inject an instance of EntityManagerFactory, then from the factory create the entity manager. Example below.

@PersistenceUnit(unitName="txPersistUnit.security")
EntityManagerFactory emf;

....
// somewhere in your code
EntityManager em = emf.createEntityManager();
Ish
  • 3,992
  • 1
  • 17
  • 23
  • but it's not mandatory, is it? I hope you will just agree cause I have RESOURCE_LOCAL persistence unit declared and running in that very same application on that very same server. – greengold Oct 08 '15 at 20:11
  • The fact that your exception message states "Only persistence units with transaction type JTA can be used as a container managed entity manager.", means that you should set it to JTA. – Ish Oct 08 '15 at 20:16
  • In ideal world I would set it to JTA and if it deploys it's done! But it won't and I won't talk about that cause this thread is about something else. So, please, either answer my question or don't drag attention other direction. Thanks – greengold Oct 08 '15 at 20:22
  • I updated my answer, hope that will help resolve your issue. – Ish Oct 08 '15 at 20:41