Search code examples
jpawebsphere-libertyopen-liberty

JPA Exception : No externally managed transaction is currently active for this thread


Exception is thrown when trying to Insert/Update/Delete with executeUpdate(). Select query works fine.I have tried all the suggestions from previous similar error mentioned in stack-overflow. Appreciate any guidance.

Environment : Websphere Liberty : 17.0.0.2, Eclipselink 2.6.4, JPA 2.1

Features enabled on Liberty server

<featureManager>
    <feature>adminCenter-1.0</feature>
    <feature>beanValidation-1.1</feature>
    <feature>cdi-1.2</feature>
    <feature>concurrent-1.0</feature>
    <feature>ejbLite-3.2</feature>
    <feature>el-3.0</feature>
    <feature>jsf-2.2</feature>
    <feature>jsp-2.3</feature>
    <feature>localConnector-1.0</feature>
    <feature>servlet-3.1</feature>
    <feature>jpa-2.1</feature>
    <!--The following features are available in Liberty base and above. -->
    <feature>jaxb-2.2</feature>
</featureManager>

Peristence.xml

<?xml version="1.0" encoding="UTF-8"?>
 <persistence version="2.1"
   xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
   http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="BlueeCron" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/BlueeUPMDataSource</jta-data-source>
    <mapping-file>META-INF/queries.xml</mapping-file>
    <class>com.bcbsnc.providers.models.BlueEReqst</class>
    <class>com.bcbsnc.providers.models.BlueERespn</class>
    <properties>
        <property name="eclipselink.logging.level" value="ALL" />
        <property name="eclipselink.logging.level.sql" value="FINE" />
        <property name="eclipselink.logging.parameters" value="true" />
    </properties>
</persistence-unit>

@Stateless
@Repository("emJPADao")
public class JPADao {

EntityManager entityManager = Persistence.createEntityManagerFactory("BlueeCron").createEntityManager();

public Integer purgeBxTables() {
    Integer rowsDeleted = 0;
    try {
        Integer noOfDays = Integer.parseInt(this.getConfigurationData("PurgeBXTablesPeriod"));
        rowsDeleted = entityManager.createNamedQuery("PURGE_BX_TABLES").setParameter("noOfDays", getTimeStamp(noOfDays, false)).executeUpdate();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        if(entityManager.isOpen())
            entityManager.close();
    }

} }

Logs at server startup
Launching defaultServer (WebSphere Application Server 17.0.0.2/wlp-1.0.17.cl170220170523-1818) on IBM J9 VM, version pwa6480sr4fp5-20170421_01 (SR4 FP5) (en_US)

[AUDIT ] CWWKF0012I: The server installed the following features: [jsp-2.3, ejbLite-3.2, servlet-3.1, jsf-2.2, beanValidation-1.1, ssl-1.0, jndi-1.0, jca-1.7, jdbc-4.2, localConnector-1.0, appSecurity-2.0, jaxrs-2.0, restConnector-1.0, el-3.0, jaxrsClient-2.0, concurrent-1.0, wmqJmsClient-2.0, jaxb-2.2, json-1.0, jpaContainer-2.1, adminCenter-1.0, cdi-1.2, distributedMap-1.0, jpa-2.1].

[AUDIT ] CWWKF0011I: The server defaultServer is ready to run a smarter planet.

[EL Info]: server: 2017-10-19 10:23:13.215--ServerSession(1864654006)--Detected server platform: org.eclipse.persistence.platform.server.was.WebSphere_Liberty_Platform. S

Exception :
[err] javax.persistence.TransactionRequiredException: Exception Description: No externally managed transaction is currently active for this thread

[err] at org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper.throwCheckTransactionFailedException(JTATransactionWrapper.java:94)

[err] at org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper.checkForTransaction(JTATransactionWrapper.java:54)

[err] at org.eclipse.persistence.internal.jpa.EntityManagerImpl.checkForTransaction(EntityManagerImpl.java:2054)

[err] at org.eclipse.persistence.internal.jpa.QueryImpl.executeUpdate(QueryImpl.java:291)

[err] at com.bcbsnc.providers.dao.JPADao.purgeBxTables(JPADao.java:49)


Solution

  • The executeUpdate() method requires for the EntityManager to be enlisted with a transaction - a global transaction in this case since you have defined a JTA-type persistence unit. You have chosen to use JPA's JSE bootstrapping approach (using Persistence.createEntityManagerFactory() instead of injection via @PersistenceContext or @PersistenceUnit) -- while I don't endorse using the JSE bootstrapping method in an EE application, it's not dis-allowed by the spec.

    However, I believe the problem you are hitting is the fact that what you have effectively here is an application-managed persistence context, and thus your application is responsible for its enlistment with the global transaction (which would have been begun automatically by the EJB container when purgeBxTables() was called, as I do not see any annotations declaring it as a bean-managed-transaction session bean) which requires calling EntityMangager.joinTransaction().

    An application-managed EntityManager will only join the global transaction automatically when the EntityManager is first created. Which is not the case for your application since the EntityManager is created when the bean class is constructed. Otherwise, the joinTransaction() method invocation is required in order for an EntityManager to join a new transaction.

    Your application will need to call em.joinTransaction() before you call executeUpdate().

    Using a container managed persistence context (using @PersistenceContext to inject an EntityManager) would have had the EntityManager automatically join the global transaction (unless you override the default Transaction SynchronizationType to UNSYNCHRONIZED.)