Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] JEE Application Managed EntityManager Error

Jehanzeb,
    Hi, try using a different (non-JTA - the default) persistence unit for the app managed EMF.
    For the 2nd EM "applicationManagedEm".  Is the persistence unit specified as "RESOURCE_LOCAL" so you can begin/commit transactions?  If you deploy a persistence unit to the EE container with transaction-type defaulted - it will default to JTA on the EJB container.
    In your code "applicationManagedEm = emf.createEntityManager()" you are getting the application managed persistence unit from the same named "Constants.PERSISTENCE_UNIT" persistence unit that is used to inject a container managed one - I would expect that you require 2 separate persistence-unit elements in your persistence.xml - we can test this by hacking the EAR application on the EclipseLink WebLogic JPA tutorial.

    Experimenting with JPA is interesting, so I tried your mixed EM approach using configuration 1) below and got the same IAE as yourself if I use the same persistence-unit for both the application and container managed EM's - therefore you are using a JTA configuration for your application managed EM.
    If however, I switch to using a 2nd persistence-unit that is not JTA and use this to create a non-injected EMF and EM - user transaction control is OK - but this is SE on EE.

    Note that the the transaction is different between the 2 persistence units.
>JTA
applicationManagedEm    EntityManagerImpl  (id=11505)   
    transaction    JTATransactionWrapper  (id=11514)   
        txnController    WebLogicTransactionController  (id=11544)   

>RESOURCE_LOCAL
applicationManagedEm    EntityManagerImpl  (id=11635)   
    transaction    EntityTransactionWrapper  (id=11638)   
        entityTransaction    null   


>Working:
---------------
    @PersistenceContext(unitName="example", type=PersistenceContextType.TRANSACTION)   
    private EntityManager entityManager;
    private EntityManager applicationManagedEm;
    @PersistenceUnit(unitName="example2")
    private EntityManagerFactory emf;

and use 2 separate persistence units (a JTA and non-JTA one)
    <persistence-unit name="example">
...
    </persistence-unit>
    <persistence-unit name="example2" transaction-type="RESOURCE_LOCAL">
...
            <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/>
            <property name="javax.persistence.jdbc.platform" value=...
            <property name="javax.persistence.jdbc.url" value=...
            <property name="javax.persistence.jdbc.user" value=...
            <property name="javax.persistence.jdbc.password" value=...
    </persistence-unit>

applicationManagedEm    EntityManagerImpl  (id=11635)   
    beginEarlyTransaction    false   
    cacheStoreBypass    false   
    closeOnCommit    false   
    commitWithoutPersistRules    false   
    connectionPolicy    ConnectionPolicy  (id=11636)   
    extendedPersistenceContext    null   
    factory    EntityManagerFactoryImpl  (id=11630)   
    flushClearCache    "DropInvalidate" (id=11510)   
    flushMode    FlushModeType  (id=11511)   
    isOpen    true   
    persistOnCommit    true   
    properties    null   
    referenceMode    ReferenceMode  (id=11512)   
    serverSession    ServerSession  (id=11637)   
    shouldValidateExistence    false   
    transaction    EntityTransactionWrapper  (id=11638)   
        entityManager    EntityManagerImpl  (id=11635)   
        entityTransaction    null   
        localUOW    null   
        txnKey    null   

[EL Finest]: 2010-03-30 11:52:00.594--ServerSession(9847848)--Thread(Thread[[ACTIVE] ExecuteThread: '18' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads])--End deploying Persistence Unit example2; state Deployed; factoryCount 1

Active Transaction: false



>Not working
-----------------

@Local
//@Stateless
@Stateful
public class ApplicationService implements ApplicationServiceLocal {
    @PersistenceContext(unitName="example", type=PersistenceContextType.TRANSACTION)   
    private EntityManager entityManager;
    private EntityManager applicationManagedEm;
    @PersistenceUnit(unitName="example")
    private EntityManagerFactory emf;

    and I get
e    IllegalStateException  (id=11527)   
    backtrace     (id=11529)   
    cause    IllegalStateException  (id=11527)   
    detailMessage    "\r\nException Description: Cannot use an EntityTransaction while using JTA." (id=11530)   
    noBackTrace    0   
    stackTrace    null   
    stackTraceHolder    null   

    When I call
            applicationManagedEm = emf.createEntityManager();
            System.out.println("Active Transaction: "
            + applicationManagedEm.getTransaction().isActive());  //ERROR SEE BELOW

    on the following EM that uses the WLS tx controller
applicationManagedEm    EntityManagerImpl  (id=11505)   
    beginEarlyTransaction    false   
    cacheStoreBypass    false   
    closeOnCommit    false   
    commitWithoutPersistRules    false   
    connectionPolicy    ConnectionPolicy  (id=11509)   
    extendedPersistenceContext    null   
    factory    EntityManagerFactoryImpl  (id=11499)   
    flushClearCache    "DropInvalidate" (id=11510)   
    flushMode    FlushModeType  (id=11511)   
    isOpen    true   
    persistOnCommit    true   
    properties    null   
    referenceMode    ReferenceMode  (id=11512)   
    serverSession    ServerSession  (id=11513)   
    shouldValidateExistence    false   
    transaction    JTATransactionWrapper  (id=11514)   
        entityManager    EntityManagerImpl  (id=11505)   
        localUOW    null   
        txnController    WebLogicTransactionController  (id=11544)   
        txnKey    null   



    As I understand it you have 3 choices that work out of 4.

    1) Container injected and container managed transactions (default on EE)
    persistence.xml
        <persistence-unit name="example" transaction-type="JTA">
       or
        <persistence-unit name="example">
    @Stateless SB
        @PersistenceContext(unitName="example", type=PersistenceContextType.TRANSACTION)
        private EntityManager entityManager;

        or
        @PersistenceContext(unitName="example")
        private EntityManager entityManager;


    2) Container injected and should be application managed but on the container it is container managed if you use a TX datasource
    persistence.xml
        <persistence-unit name="example" transaction-type="JTA">
            <jta-data-source>localJTA</jta-data-source>
    @Stateful SB
        @PersistenceContext(unitName="example", type=PersistenceContextType.EXTENDED)
        private EntityManager entityManager;

    For an example, an attempt to manually perform a transaction call like "entityManager.getTransaction().isActive() or .begin()" on the SFSB will cause an ISE similar to the one you experience
        e    IllegalStateException  (id=11232)   
        cause    IllegalStateException  (id=11232)   
        detailMessage    "The method public abstract javax.persistence.EntityTransaction javax.persistence.EntityManager.getTransaction() cannot be invoked in the context of a JTA EntityManager." (id=11235)  

    because the entityManager still has a transaction manager set
entityManager    $Proxy76  (id=11315)   
    h    ExtendedEntityManagerProxyImpl  (id=11330)   
        appName    "org.eclipse.persistence.example.jpa.server.weblogic.enterpriseEAR" (id=11335)   
        closeMethod    Method  (id=11336)   
        moduleName    "org.eclipse.persistence.example.jpa.server.weblogic.enterpriseEJB.jar" (id=11337)   
        persistenceUnit    PersistenceUnitInfoImpl  (id=11338)   
        persistenceUnitName    "org.eclipse.persistence.example.jpa.server.weblogic.enterpriseEAR#org.eclipse.persistence.example.jpa.server.weblogic.enterpriseEJB.jar#example#example" (id=11339)   
        transactionAccessMethod    Method  (id=11340)   
        transactionalMethods    HashSet<E>  (id=11341)   
        txHelper    TransactionHelperImpl  (id=11342)   
        txRegistry    ServerTransactionManagerImpl  (id=11343)   
        unqualifiedPersistenceUnitName    "example" (id=11345)   



    3) Container injected but application managed transactions (SE on EE) - the following will require all database properties set in the persistence unit.
    persistence.xml
        <persistence-unit name="example" transaction-type="RESOURCE_LOCAL">
    @Stateful SB
        @PersistenceContext(unitName="example", type=PersistenceContextType.EXTENDED)
        private EntityManager entityManager;

<30-Mar-2010 11:22:27 o'clock AM EDT> <Warning> <HTTP> <BEA-101372> <There was a failure in application ServletContext@17921694[app:org.eclipse.persistence.example.jpa.server.weblogic.enterpriseEAR module:enterprise path:/enterprise spec-version:2.5] when attempting to inject dependencies into org.eclipse.persistence.example.jpa.server.weblogic.enterprise.presentation.FrontController. Dependencies will not be injected into this class.
Caused By: com.bea.core.repackaged.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.eclipse.persistence.example.jpa.server.business.ApplicationServiceLocal] is defined: No beans of type org.eclipse.persistence.example.jpa.server.business.ApplicationServiceLocal; owner=com.bea.core.repackaged.springframework.context.support.GenericApplicationContext@4dd89d: display name [com.bea.core.repackaged.springframework.context.support.GenericApplicationContext@4dd89d]; startup date [Tue Mar 30 11:20:08 EDT 2010]; parent: com.bea.core.repackaged.springframework.context.support.GenericApplicationContext@4d5373
    at com.bea.core.repackaged.springframework.jee.inject.Jsr250Metadata.getUniqueInstanceOfType(Jsr250Metadata.java:305)



    4) Application created and application managed transactions (SE on EE)
        persistence.xml
        <persistence-unit name="example" transaction-type="RESOURCE_LOCAL">
    slsb.java
        @PersistenceContext(unitName="example", type=PersistenceContextType.TRANSACTION)
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("example");
        EntityManager entityManager = emf.createEntityManager();           


    Jehanzeb,  The defaults got me thinking that the "No unique bean of type" in your 3rd mail yesterday is reproducable for any type of configuration exception that results in the SB not being created.  For example, I can replicate it if I use a RESOURCE_LOCAL persistence unit on the container and omit the DB properties.

<30-Mar-2010 11:22:27 o'clock AM EDT> <Warning> <HTTP> <BEA-101372> <There was a failure in application ServletContext@17921694[app:org.eclipse.persistence.example.jpa.server.weblogic.enterpriseEAR module:enterprise path:/enterprise spec-version:2.5] when attempting to inject dependencies into org.eclipse.persistence.example.jpa.server.weblogic.enterprise.presentation.FrontController. Dependencies will not be injected into this class.
Caused By: com.bea.core.repackaged.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.eclipse.persistence.example.jpa.server.business.ApplicationServiceLocal] is defined: No beans of type org.eclipse.persistence.example.jpa.server.business.ApplicationServiceLocal; owner=com.bea.core.repackaged.springframework.context.support.GenericApplicationContext@4dd89d: display name [com.bea.core.repackaged.springframework.context.support.GenericApplicationContext@4dd89d]; startup date [Tue Mar 30 11:20:08 EDT 2010]; parent: com.bea.core.repackaged.springframework.context.support.GenericApplicationContext@4d5373
    at com.bea.core.repackaged.springframework.jee.inject.Jsr250Metadata.getUniqueInstanceOfType(Jsr250Metadata.java:305)


    thank you
    /michael


jz wrote:
Hi All,

I am trying to get application managed entity manager along with container managed. But getting below error:

        @PersistenceContext(unitName = Constants.PERSISTENCE_UNIT)
private EntityManager containerManagedEm;

private EntityManager applicationManagedEm;

@PersistenceUnit(unitName = Constants.PERSISTENCE_UNIT)
private EntityManagerFactory emf;

        @TransactionAttribute(TransactionAttributeType.NEVER)
@PostConstruct
public void init() throws Exception {
applicationManagedEm = emf.createEntityManager();
System.out.println("Active Transaction: "
+ applicationManagedEm.getTransaction().isActive());  //ERROR SEE BELOW
}


Getting error:

Caused by: java.lang.IllegalStateException:
Exception Description: Cannot use an EntityTransaction while using JTA.
        at org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper.getTransaction(JTATransactionWrapper.java:65)
        at org.eclipse.persistence.internal.jpa.EntityManagerImpl.getTransaction(EntityManagerImpl.java:1103)
        at com.warid.campaign.service.CampaignCrudBean.init(CampaignCrudBean.java:68)
        ... 87 more

Where:
Environment: Weblogic 10.3.0, Eclipselink 2, JPA 2, Container Managed Transactions


Regards,
Jehanzeb Qayyum



_______________________________________________ eclipselink-users mailing list eclipselink-users@xxxxxxxxxxx https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top