title: Deltaspike Data Repositories in Wildfly 8.2 with JTA and Container Managed Transaction (CMT)
body: <p>I am using DeltaSpike Data Repositories for the daos in a project that uses JTA datasources. I recently migrated to Wildfly 8.2 and I got some problems with them:</p>
  
  <p>As described in DeltaSpike documentation, I configured  BeanManagedUserTransactionStrategy as my TransactionStrategy, writing <code>globalAlternatives.org.apache.deltaspike.jpa.spi.transaction.TransactionStrategy =org.apache.deltaspike.jpa.impl.transaction.BeanManagedUserTransactionStrategy</code> into <code>/META-INF/apache-deltaspike.properties</code></p>
  
  <p>With this configuration, consider a <code>Role</code>, a <code>@Repository RoleDao</code>, a <code>TestStatelessClass</code> that inject the RoleDao and print a line, and a resteasy API that inject the service:</p>
  
  <p><strong>Role.java</strong></p>
  
  <pre><code>@Entity
  public class Role implements Serializable {
  
  @Id
  private Long id;
  
  @Column(unique=true, nullable=false)
  private String role;
  // ...
  }
  </code></pre>
  
  <p><strong>RoleDao.java</strong></p>
  
  <pre><code>import org.apache.deltaspike.data.api.EntityRepository;
  import org.apache.deltaspike.data.api.Repository;
  @Repository
  @Dependent
  public interface RoleDao extends EntityRepository&lt;Role, Long&gt; {
  
  Role findById(long id);
  }
  </code></pre>
  
  <p><strong>TestStatelessClass.java</strong></p>
  
  <pre><code>@LocalBean
  @Stateless
  public class TestStatelessClass {
      @Inject RoleDao roleDao;
  
      public TestStatelessClass() {
      }
  
      public void execute() {
          System.out.println("Stateless: Count roles: " + roleDao.count());
      }
  }
  </code></pre>
  
  <p><strong>TestAPI.java</strong></p>
  
  <pre><code>@Path("test")
  //@Stateless
  public class TestAPI {
  
      @Inject TestStatelessClass tsc;
  
      @GET
      @Produces("application/json")
      @NoCache
      public List&lt;String&gt; test() throws Exception {
  
          tsc.execute();
  
          return null;
      }
  }
  </code></pre>
  
  <p>When tsc.execute() is called, the following exception take place:</p>
  
  <pre><code>18:39:57,425 ERROR [org.jboss.as.ejb3.invocation] (default task-19) JBAS014134: EJB Invocation failed on component TestStatelessClass for method public void example.scheduler.TestStatelessClass.execute(): javax.ejb.EJBException: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
  ...
  Caused by: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
  ...
  Caused by: javax.ejb.EJBException: javax.enterprise.inject.CreationException
  ...
  Caused by: javax.enterprise.inject.CreationException
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.7.0_55]
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) [rt.jar:1.7.0_55]
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) [rt.jar:1.7.0_55]
      at java.lang.reflect.Constructor.newInstance(Unknown Source) [rt.jar:1.7.0_55]
      at java.lang.Class.newInstance(Unknown Source) [rt.jar:1.7.0_55]
      at org.jboss.weld.security.NewInstanceAction.run(NewInstanceAction.java:33) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.7.0_55]
      at org.jboss.weld.injection.Exceptions.rethrowException(Exceptions.java:40) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.Exceptions.rethrowException(Exceptions.java:50) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.Exceptions.rethrowException(Exceptions.java:54) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:96) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:370) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:381) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.producer.ResourceInjector$1.proceed(ResourceInjector.java:70) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:72) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:121) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:150) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:69) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:742) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:840) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:370) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:381) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.producer.ResourceInjector$1.proceed(ResourceInjector.java:70) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.producer.ResourceInjector.inject(ResourceInjector.java:72) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.producer.BasicInjectionTarget.inject(BasicInjectionTarget.java:121) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:150) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:69) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:742) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:762) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.util.ForwardingBeanManager.getReference(ForwardingBeanManager.java:61) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.bean.builtin.BeanManagerProxy.getReference(BeanManagerProxy.java:89) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.apache.deltaspike.partialbean.impl.PartialBeanLifecycle.createHandlerInstance(PartialBeanLifecycle.java:133) [deltaspike-partial-bean-module-impl-1.3.0.jar:1.3.0]
      at org.apache.deltaspike.partialbean.impl.PartialBeanLifecycle.create(PartialBeanLifecycle.java:68) [deltaspike-partial-bean-module-impl-1.3.0.jar:1.3.0]
      at org.apache.deltaspike.core.util.bean.ImmutableBean.create(ImmutableBean.java:72) [deltaspike-core-api-1.3.0.jar:1.3.0]
      at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:69) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:742) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:840) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:370) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:381) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.producer.DefaultInjector$1.proceed(DefaultInjector.java:71) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.producer.DefaultInjector.inject(DefaultInjector.java:73) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.producer.StatelessSessionBeanInjector.inject(StatelessSessionBeanInjector.java:58) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.weld.injection.producer.ejb.SessionBeanInjectionTarget.inject(SessionBeanInjectionTarget.java:140) [weld-core-impl-2.2.6.Final.jar:2014-10-03 10:05]
      at org.jboss.as.weld.injection.WeldInjectionContext.inject(WeldInjectionContext.java:39) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]
      at org.jboss.as.weld.injection.WeldInjectionInterceptor.processInvocation(WeldInjectionInterceptor.java:51) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]
      at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
      at org.jboss.as.ee.component.AroundConstructInterceptorFactory$1.processInvocation(AroundConstructInterceptorFactory.java:28)
      at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
      at org.jboss.as.weld.injection.WeldInterceptorInjectionInterceptor.processInvocation(WeldInterceptorInjectionInterceptor.java:56) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]
      at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
      at org.jboss.as.weld.ejb.Jsr299BindingsCreateInterceptor.processInvocation(Jsr299BindingsCreateInterceptor.java:94) [wildfly-weld-8.2.0.Final.jar:8.2.0.Final]
      at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
      at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)
      at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
      at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:273) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
      ... 111 more
  ...
  Caused by: javax.naming.NameNotFoundException: UserTransaction [Root exception is java.lang.IllegalStateException: JBAS014237: Only session and message-driven beans with bean-managed transaction demarcation are allowed to access UserTransaction]
  ...
  Caused by: java.lang.IllegalStateException: JBAS014237: Only session and message-driven beans with bean-managed transaction demarcation are allowed to access UserTransaction
  </code></pre>
  
  <p>If instead of a Contained Managed Transaction class, I use a Stateless Bean Managed Transaction class (<code>@Stateless @TransactionManagement(TransactionManagementType.BEAN)</code>), everything works fine, but changing all the classes and control myself the transaction inside the beans would be painful. Do anyone knows the cause of this errors?</p>
  
  <p>Thank you in advance for any help!</p>
  
  <p>PS: I have some of the workarounds <a href="https://issues.apache.org/jira/browse/DELTASPIKE-552" rel="nofollow">https://issues.apache.org/jira/browse/DELTASPIKE-552</a> and <a href="https://issues.apache.org/jira/browse/DELTASPIKE-420" rel="nofollow">https://issues.apache.org/jira/browse/DELTASPIKE-420</a> but does not seem valid.</p>
  
  <p>EDIT: Some POM dependencies:</p>
  
  <pre><code>&lt;properties&gt;
      &lt;deltaspike.version&gt;1.3.0&lt;/deltaspike.version&gt;
  &lt;/properties&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.deltaspike.modules&lt;/groupId&gt;
          &lt;artifactId&gt;deltaspike-data-module-api&lt;/artifactId&gt;
          &lt;version&gt;${deltaspike.version}&lt;/version&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.deltaspike.modules&lt;/groupId&gt;
          &lt;artifactId&gt;deltaspike-data-module-impl&lt;/artifactId&gt;
          &lt;version&gt;${deltaspike.version}&lt;/version&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;javax.enterprise&lt;/groupId&gt;
          &lt;artifactId&gt;cdi-api&lt;/artifactId&gt;
          &lt;version&gt;1.2&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-jpa-2.1-api&lt;/artifactId&gt;
          &lt;version&gt;1.0.0.Final&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.jboss.resteasy&lt;/groupId&gt;
          &lt;artifactId&gt;resteasy-jaxrs&lt;/artifactId&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.jboss.spec.javax.servlet&lt;/groupId&gt;
          &lt;artifactId&gt;jboss-servlet-api_3.0_spec&lt;/artifactId&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  </code></pre>