0 Replies Latest reply on Nov 16, 2014 4:02 AM by rene.zoller

    Wildfly 8.1, Error: 'A JTA EntityManager cannot use getTransaction()'

    rene.zoller

      Hello community,

       

      I'm trying to get JPA testing (like described here: http://arquillian.org/guides/testing_java_persistence/) running with wildfly 8.1, but I always receive an "A JTA EntityManager cannot use getTransaction()" Error. This is what I've got so far:

      My pom.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
          <modelVersion>4.0.0</modelVersion>
      
          <groupId>com.myexample</groupId>
          <artifactId>bits-ng</artifactId>
          <version>1.0-SNAPSHOT</version>
          <packaging>war</packaging>
          <name>BITS web application</name>
      
          <properties>
      
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      
              <!-- JBoss dependency versions -->
              <version.wildfly.maven.plugin>1.0.2.Final</version.wildfly.maven.plugin>
      
              <!-- Define the version of the JBoss BOMs we want to import to specify 
                  tested stacks. -->
              <version.jboss.bom>8.1.0.Final</version.jboss.bom>
              <version.arquillian.container>8.1.0.Final</version.arquillian.container>
              <version.shrinkwrap>1.1.2</version.shrinkwrap>
              <version.mockito>1.10.8</version.mockito>
      
              <!-- other plugin versions -->
              <version.compiler.plugin>3.1</version.compiler.plugin>
              <version.surefire.plugin>2.16</version.surefire.plugin>
              <version.war.plugin>2.1.1</version.war.plugin>
      
              <!-- maven-compiler-plugin -->
              <maven.compiler.target>1.7</maven.compiler.target>
              <maven.compiler.source>1.7</maven.compiler.source>
      
              <!-- Test -->
              <version.testng>5.14.10</version.testng>
              <version.log4j>1.2.17</version.log4j>
      
              <!-- Third party dependency versions -->
              <version.jdbc.postgresql>9.3-1102-jdbc4</version.jdbc.postgresql>
              <version.org.picketlink>2.5.2.Final</version.org.picketlink>
              <version.joda-time>2.5</version.joda-time>
      
              <!-- Apache commons dependency version -->
              <version.apache.commons.lang3>3.3.2</version.apache.commons.lang3>
              <version.apache.commons.fileupload>1.3.1</version.apache.commons.fileupload>
      
          </properties>
      
          <dependencyManagement>
              <dependencies>
                  <dependency>
                      <groupId>org.wildfly.bom</groupId>
                      <artifactId>jboss-javaee-7.0-with-tools</artifactId>
                      <version>${version.jboss.bom}</version>
                      <type>pom</type>
                      <scope>import</scope>
                  </dependency>
                  <dependency>
                      <groupId>org.wildfly.bom</groupId>
                      <artifactId>jboss-javaee-7.0-with-hibernate</artifactId>
                      <version>${version.jboss.bom}</version>
                      <type>pom</type>
                      <scope>import</scope>
                  </dependency>
                  <!-- Dependency Management for PicketLink -->
                  <dependency>
                      <groupId>org.wildfly.bom</groupId>
                      <artifactId>jboss-javaee-7.0-with-security</artifactId>
                      <version>${version.jboss.bom}</version>
                      <type>pom</type>
                      <scope>import</scope>
                  </dependency>
                  <!-- Dependency Management for Arquillian -->
                  <dependency>
                      <groupId>org.jboss.arquillian</groupId>
                      <artifactId>arquillian-bom</artifactId>
                      <version>1.1.5.Final</version>
                      <scope>import</scope>
                      <type>pom</type>
                  </dependency>
              </dependencies>
          </dependencyManagement>
      
          <dependencies>
      
              <dependency>
                  <groupId>javax.enterprise</groupId>
                  <artifactId>cdi-api</artifactId>
                  <scope>provided</scope>
              </dependency>
      
              <dependency>
                  <groupId>org.jboss.spec.javax.annotation</groupId>
                  <artifactId>jboss-annotations-api_1.2_spec</artifactId>
                  <scope>provided</scope>
              </dependency>
      
              <dependency>
                  <groupId>org.jboss.resteasy</groupId>
                  <artifactId>jaxrs-api</artifactId>
                  <scope>provided</scope>
              </dependency>
      
              <dependency>
                  <groupId>org.hibernate.javax.persistence</groupId>
                  <artifactId>hibernate-jpa-2.1-api</artifactId>
                  <scope>provided</scope>
              </dependency>
      
              <dependency>
                  <groupId>org.jboss.spec.javax.transaction</groupId>
                  <artifactId>jboss-transaction-api_1.2_spec</artifactId>
                  <scope>provided</scope>
              </dependency>
      
              <dependency>
                  <groupId>org.jboss.spec.javax.ejb</groupId>
                  <artifactId>jboss-ejb-api_3.2_spec</artifactId>
                  <scope>provided</scope>
              </dependency>
      
              <dependency>
                  <groupId>org.hibernate</groupId>
                  <artifactId>hibernate-validator</artifactId>
                  <scope>provided</scope>
                  <exclusions>
                      <exclusion>
                          <groupId>org.slf4j</groupId>
                          <artifactId>slf4j-api</artifactId>
                      </exclusion>
                  </exclusions>
              </dependency>
      
              <dependency>
                  <groupId>org.jboss.spec.javax.faces</groupId>
                  <artifactId>jboss-jsf-api_2.2_spec</artifactId>
                  <scope>provided</scope>
              </dependency>
      
              <dependency>
                  <groupId>org.hibernate</groupId>
                  <artifactId>hibernate-jpamodelgen</artifactId>
                  <scope>provided</scope>
              </dependency>
      
              <dependency>
                  <groupId>org.hibernate</groupId>
                  <artifactId>hibernate-validator-annotation-processor</artifactId>
                  <scope>provided</scope>
              </dependency>
      
              <dependency>
                  <groupId>joda-time</groupId>
                  <artifactId>joda-time</artifactId>
                  <version>${version.joda-time}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.apache.commons</groupId>
                  <artifactId>commons-lang3</artifactId>
                  <version>${version.apache.commons.lang3}</version>
              </dependency>
      
              <dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>4.8.1</version>
                  <scope>test</scope>
              </dependency>
      
              <dependency>
                  <groupId>org.mockito</groupId>
                  <artifactId>mockito-core</artifactId>
                  <version>${version.mockito}</version>
                  <scope>test</scope>
              </dependency>
      
              <dependency>
                  <groupId>log4j</groupId>
                  <artifactId>log4j</artifactId>
                  <version>${version.log4j}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.jboss.arquillian.junit</groupId>
                  <artifactId>arquillian-junit-container</artifactId>
                  <scope>test</scope>
              </dependency>
      
              <dependency>
                  <groupId>org.wildfly</groupId>
                  <artifactId>wildfly-arquillian-container-embedded</artifactId>
                  <scope>test</scope>
              </dependency>
      
              <dependency>
                  <groupId>org.wildfly</groupId>
                  <artifactId>wildfly-embedded</artifactId>
                  <scope>test</scope>
              </dependency>
      
          </dependencies>
      
          <build>
              <finalName>${project.artifactId}</finalName>
              <plugins>
                  <plugin>
                      <artifactId>maven-compiler-plugin</artifactId>
                      <version>${version.compiler.plugin}</version>
                      <configuration>
                          <source>${maven.compiler.source}</source>
                          <target>${maven.compiler.target}</target>
                      </configuration>
                  </plugin>
                  <plugin>
                      <artifactId>maven-war-plugin</artifactId>
                      <version>${version.war.plugin}</version>
                      <configuration>
                          <!-- Java EE 7 doesn't require web.xml, Maven needs to catch up! -->
                          <failOnMissingWebXml>false</failOnMissingWebXml>
                      </configuration>
                  </plugin>
                  <!-- The WildFly plugin deploys your war to a local WildFly container -->
                  <!-- To use, run: mvn package wildfly:deploy -->
                  <plugin>
                      <groupId>org.wildfly.plugins</groupId>
                      <artifactId>wildfly-maven-plugin</artifactId>
                      <version>${version.wildfly.maven.plugin}</version>
                  </plugin>
              </plugins>
          </build>
      
          <profiles>
              <profile>
                  <id>default</id>
                  <activation>
                      <activeByDefault>true</activeByDefault>
                  </activation>
                  <build>
                      <plugins>
                          <plugin>
                              <artifactId>maven-surefire-plugin</artifactId>
                              <version>${version.surefire.plugin}</version>
                              <configuration>
                                  <skip>true</skip>
                              </configuration>
                          </plugin>
                      </plugins>
                  </build>
              </profile>
      
              <profile>
                  <id>wildfy81-embedded</id>
                  <!-- the dependencies for Wildfly 8.1  -->
                  <dependencies>
                      <dependency>
                          <groupId>org.wildfly</groupId>
                          <artifactId>wildfly-arquillian-container-embedded</artifactId>
                          <version>${version.jboss.bom}</version>
                      </dependency>
                      <dependency>
                          <groupId>org.wildfly</groupId>
                          <artifactId>wildfly-embedded</artifactId>
                          <version>${version.jboss.bom}</version>
                      </dependency>
                      <dependency>
                          <groupId>org.jboss.shrinkwrap.resolver</groupId>
                          <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
                          <scope>test</scope>
                      </dependency>
                  </dependencies>
      
                  <build>
                      <plugins>
                          <plugin>
                              <groupId>org.apache.maven.plugins</groupId>
                              <artifactId>maven-dependency-plugin</artifactId>
                              <version>2.9</version>
                              <executions>
                                  <execution>
                                      <id>unpack</id>
                                      <phase>process-test-classes</phase>
                                      <goals>
                                          <goal>unpack</goal>
                                      </goals>
                                      <configuration>
                                          <artifactItems>
                                              <artifactItem>
                                                  <groupId>org.wildfly</groupId>
                                                  <artifactId>wildfly-dist</artifactId>
                                                  <version>8.1.0.Final</version>
                                                  <type>zip</type>
                                                  <overWrite>false</overWrite>
                                                  <outputDirectory>target</outputDirectory>
                                              </artifactItem>
                                          </artifactItems>
                                      </configuration>
                                  </execution>
                              </executions>
                          </plugin>
                          <plugin>
                              <groupId>org.apache.maven.plugins</groupId>
                              <artifactId>maven-surefire-plugin</artifactId>
                              <version>${version.surefire.plugin}</version>
                              <configuration>
                                  <!-- Fork every test because it will launch a separate AS instance -->
                                  <forkMode>always</forkMode>
                                  <systemPropertyVariables>
                                      <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
                                      <!-- the maven dependency plugin will have already downloaded the server on /target -->
                                      <jboss.home>${project.basedir}/target/wildfly-8.1.0.Final</jboss.home>
                                      <module.path>${project.basedir}/target/wildfly-8.1.0.Final/modules</module.path>
                                  </systemPropertyVariables>
                                  <redirectTestOutputToFile>false</redirectTestOutputToFile>
                              </configuration>
                          </plugin>
                      </plugins>
                  </build>
              </profile>
      
          </profiles>
      </project>
      

       

      My persistence.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <persistence 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"
                   version="2.1">
      
          <persistence-unit name="test">
              <jta-data-source>jdbc/arquillian</jta-data-source>
              <properties>
                  <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
                  <property name="hibernate.show_sql" value="true"/>
              </properties>
          </persistence-unit>
      
      </persistence>
      

       

      My test-ds.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <datasources xmlns="http://www.jboss.org/ironjacamar/schema"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="
              http://www.jboss.org/ironjacamar/schema
              http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
          <datasource enabled="true"
                      jndi-name="jdbc/arquillian"
                      pool-name="ArquillianEmbeddedH2Pool">
              <connection-url>jdbc:h2:mem:arquillian;DB_CLOSE_DELAY=-1</connection-url>
              <driver>h2</driver>
          </datasource>
      </datasources>
      

       

      And finally my Test

      package com.myexample.bits.domain;
      
      import org.apache.log4j.Logger;
      import org.jboss.arquillian.container.test.api.Deployment;
      import org.jboss.arquillian.junit.Arquillian;
      import org.jboss.shrinkwrap.api.Archive;
      import org.jboss.shrinkwrap.api.ShrinkWrap;
      import org.jboss.shrinkwrap.api.asset.EmptyAsset;
      import org.jboss.shrinkwrap.api.spec.WebArchive;
      import org.junit.After;
      import org.junit.Assert;
      import org.junit.Before;
      import org.junit.Test;
      import org.junit.runner.RunWith;
      import com.myexample.bits.domain.enums.Gender;
      import com.myexample.bits.domain.jpa.BaseEntity;
      import com.myexample.bits.test.TestMavenDependeciesProvider;
      
      import javax.inject.Inject;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      import javax.transaction.Transactional;
      import javax.transaction.UserTransaction;
      
      @RunWith(Arquillian.class)
      public class PersonPersistenceJUnitTest {
      
          final static Logger logger = Logger.getLogger(PersonPersistenceJUnitTest.class);
      
          @PersistenceContext
          EntityManager em;
      
          @Inject
          UserTransaction utx;
      
          @Deployment
          public static Archive<?> createDeployment() {
                  WebArchive war = ShrinkWrap.create(WebArchive.class, "test.war")
                      .addClass(BaseEntity.class)
                      .addClass(Person.class)
                      .addClass(Gender.class)
                      .addClass(MediaWrapper.class)
                      .addAsLibraries(TestMavenDependeciesProvider.JODATIME)
                      .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                      .addAsWebInfResource("test-ds.xml")
                      .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
      
              logger.info(war.toString(true));
              return war;
          }
      
          @Before
          public void preparePersistenceTest() throws Exception {
              Assert.assertNotNull(em);
              clearData();
              //insertData();
              startTransaction();
          }
      
          private void clearData() throws Exception {
              logger.debug("Dumping old records...");
              utx.begin();
              em.createQuery("delete from Person").executeUpdate();
              utx.commit();
          }
      
          private void startTransaction() throws Exception {
              em.getTransaction().begin();
          }
      
          @After
          public void commitTransaction() throws Exception {
              em.getTransaction().commit();
          }
      
          @Test
          public void insertPerson() throws Exception {
              logger.info("Try to insert Person..");
          }
      
      }
      

       

      Any suggestion what could be wrong?

       

      René