简体   繁体   中英

JPA; Setting 'transaction-type' on annotation

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!

Since you are running your code in an JEE compliant Application Server (ie 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM