1
votes

I have an application that is having an exception thrown when attempting to call flush on an EntityManager inside a stateless EJB with container-managed transactions. The exception indicates that the method is not inside a transaction but I can't understand why it would not be. I have tried adding the @TransactionAttribute annotation on the EJB method and class but it seems to have no effect. My source is below.

persistence.xml

<?xml version="1.0"?>

<persistence
  xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
  version="2.0"
>
  <persistence-unit name="simple-unit" transaction-type="JTA">

    <description>Simple JPA unit.</description>
    <jta-data-source>datasource-core</jta-data-source>
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>simple.entity.SimpleEntity</class>

    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
      <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/>
    </properties>

  </persistence-unit>

</persistence>

ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
  version="3.0"
>

  <enterprise-beans>

        <session>

            <ejb-name>SimpleTransactionBean</ejb-name>
            <transaction-type>Container</transaction-type>

        </session>

  </enterprise-beans>

</ejb-jar>

SimpleTransactionBean.java

package simple.ejb;

import javax.ejb.Stateless;

import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;

import simple.entity.SimpleEntity;


@Stateless
public class SimpleTransactionBean implements SimpleTransaction
{
  @PersistenceContext(unitName="simple-unit")
  private EntityManager entityManager;

  public void addTransaction(String transactionNumber)
  {
    SimpleEntity simpleEntity = new SimpleEntity(transactionNumber);
    this.entityManager.persist(simpleEntity);
    this.entityManager.flush();
  }
}

JSF Bean which calls the EJB

package simple.web;

import javax.ejb.EJB;

import simple.ejb.SimpleTransaction;

import simple.entity.SimpleEntity;


public class SimpleEntityAdder
{
  private String transactionNumber;

  public String getTransactionNumber() { return this.transactionNumber; }
  public void setTransactionNumber(String transactionNumber) { this.transactionNumber = transactionNumber; }

  @EJB
  private SimpleTransaction simpleTransaction;

  public void add()
  {
    this.simpleTransaction.addTransaction(this.transactionNumber);
  }
}

The Exception Stacktrace

SEVERE: javax.ejb.EJBException: EJB Exception: : javax.persistence.TransactionRequiredException: no transaction is in progress
  at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:993)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at weblogic.deployment.BasePersistenceContextProxyImpl.invoke(BasePersistenceContextProxyImpl.java:111)
  at weblogic.deployment.TransactionalEntityManagerProxyImpl.invoke(TransactionalEntityManagerProxyImpl.java:78)
  at weblogic.deployment.BasePersistenceContextProxyImpl.invoke(BasePersistenceContextProxyImpl.java:92)
  at weblogic.deployment.TransactionalEntityManagerProxyImpl.invoke(TransactionalEntityManagerProxyImpl.java:18)
  at $Proxy284.flush(Unknown Source)
  at simple.ejb.SimpleTransactionBean.addTransaction(SimpleTransactionBean.java:21)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at com.bea.core.repackaged.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
  at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
  at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
  at com.bea.core.repackaged.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131)
  at com.bea.core.repackaged.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119)
  at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
  at com.oracle.pitchfork.spi.MethodInvocationVisitorImpl.visit(MethodInvocationVisitorImpl.java:34)
  at weblogic.ejb.container.injection.EnvironmentInterceptorCallbackImpl.callback(EnvironmentInterceptorCallbackImpl.java:54)
  at com.oracle.pitchfork.spi.EnvironmentInterceptor.invoke(EnvironmentInterceptor.java:42)
  at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
  at com.bea.core.repackaged.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
  at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
  at com.bea.core.repackaged.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:131)
  at com.bea.core.repackaged.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:119)
  at com.bea.core.repackaged.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
  at com.bea.core.repackaged.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
  at $Proxy285.addTransaction(Unknown Source)
  at simple.ejb.SimpleTransactionBean_h1qa2o_SimpleTransactionImpl.__WL_invoke(Unknown Source)
  at weblogic.ejb.container.internal.SessionLocalMethodInvoker.invoke(SessionLocalMethodInvoker.java:39)
  at simple.ejb.SimpleTransactionBean_h1qa2o_SimpleTransactionImpl.addTransaction(Unknown Source)
  at simple.web.SimpleEntityAdder.add(SimpleEntityAdder.java:22)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at com.sun.el.parser.AstValue.invoke(AstValue.java:187)
  at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
  at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:149)
  at javax.faces.event.ActionEvent.processListener(ActionEvent.java:84)
  at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:773)
  at javax.faces.component.UICommand.broadcast(UICommand.java:296)
  at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:783)
  at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1248)
  at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
  at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
  at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
  at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
  at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
  at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:301)
  at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
  at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:60)
  at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
  at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:60)
  at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3748)
  at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3714)
  at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
  at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
  at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2283)
  at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2182)
  at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1491)
  at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
  at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
; nested exception is: javax.persistence.TransactionRequiredException: no transaction is in progress
javax.persistence.TransactionRequiredException: no transaction is in progress

(The entire stacktrace was too long to be included here but just repeats the last exception)

1
Does your database support transactions?Geinmachi
It's Oracle 11g so I would certainly think so. If I understand what you're asking.Rob Benton
What happens if you remove both properties altogether? Neither are required in theory.Steve C
If I take them out the same thing happens. I assume you're talking about the 2 properties in my persistence.xml? I had those in because I am deploying to weblogic 10.3 and in another project they were necessary and this example code is a subset of that project.Rob Benton

1 Answers

3
votes

When I turned on all logging for the org.hibernate.transaction package I saw a NullPointerException occurring within the hibernate code that begins like this:

java.lang.NullPointerException
        at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:76)
        at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:118)
        at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1602)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:210)
        at org.hibernate.jpa.internal.EntityManagerImpl.<init>(EntityManagerImpl.java:91)
        at org.hibernate.jpa.internal.EntityManagerFactoryImpl.internalCreateEntityManager(EntityManagerFactoryImpl.java:345)
        at org.hibernate.jpa.internal.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:313)
        at org.glassfish.persistence.jpa.JPADeployer$2.visitPUD(JPADeployer.java:451)

After doing some googling I modified my persistence.xml to change the hibernate.transaction.factory_class property and added the hibernate.transaction.jta.platform property. That solved my issue. Also note that I have added the property hibernate.classloading_use_current_tccl_as_parent for another unrelated bugfix. My final persistence.xml looks like this:

<?xml version="1.0"?>

<persistence
  xmlns="http://java.sun.com/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
  version="2.0"
>
  <persistence-unit name="simple-unit" transaction-type="JTA">

    <description>Simple JPA unit.</description>
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/datasource-core</jta-data-source>
    <class>simple.entity.SimpleEntity</class>

    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
      <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory"/>
      <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>

      <property name="hibernate.classloading.use_current_tccl_as_parent" value="false"/>
    </properties>

  </persistence-unit>

</persistence>

Note that my solution is running on glassfish here and not on weblogic. The hibernate.transaction.jta.platform property may be different for weblogic.