Hibernate, EJB, javax.persistence.TransactionRequiredException: no transaction is in progress
<p>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.</p>

<p><strong>persistence.xml</strong></p>

<pre><code>&lt;?xml version="1.0"?&gt;

&lt;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"
&gt;
  &lt;persistence-unit name="simple-unit" transaction-type="JTA"&gt;

    &lt;description&gt;Simple JPA unit.&lt;/description&gt;
    &lt;jta-data-source&gt;datasource-core&lt;/jta-data-source&gt;
    &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
    &lt;class&gt;simple.entity.SimpleEntity&lt;/class&gt;

    &lt;properties&gt;
      &lt;property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/&gt;
      &lt;property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/&gt;
    &lt;/properties&gt;

  &lt;/persistence-unit&gt;

&lt;/persistence&gt;
</code></pre>

<p><strong>ejb-jar.xml</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;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"
&gt;

  &lt;enterprise-beans&gt;

        &lt;session&gt;

            &lt;ejb-name&gt;SimpleTransactionBean&lt;/ejb-name&gt;
            &lt;transaction-type&gt;Container&lt;/transaction-type&gt;

        &lt;/session&gt;

  &lt;/enterprise-beans&gt;

&lt;/ejb-jar&gt;
</code></pre>

<p><strong>SimpleTransactionBean.java</strong></p>

<pre><code>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();
  }
}
</code></pre>

<p><strong>JSF Bean which calls the EJB</strong></p>

<pre><code>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);
  }
}
</code></pre>

<p><strong>The Exception Stacktrace</strong></p>

<pre><code>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
</code></pre>

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