【问题标题】:Arquillian: Transaction is required to perform this operationArquillian:执行此操作需要事务
【发布时间】:2015-04-13 07:28:59
【问题描述】:

我正在尝试使用 arquillian- 运行我们的 JPA 单元测试,并嵌入一个 Wildfly。 到目前为止,当我对项目进行清理和构建时,我得到了以下步骤:

  1. 嵌入的 wildfly 将部署在项目的 /target 文件夹中
  2. MSSQL 数据库驱动程序将被部署并注册为驱动程序
  3. arquillian 创建一个包含所有必需依赖项的 .war 文件
  4. arquillian 将我项目的 .war 部署到嵌入式 wildfly 并开始对嵌入式 wildfly 进行单元测试

现在我的问题:当我在单元测试中调用 entitymanager 上的 .create() 或 .delete() 方法时,出现以下异常:

Caused by: javax.persistence.TransactionRequiredException: JBAS011469: Transaction is required to perform this operation (either use a transaction or extended persistence context)

这些是我的 pom.xml 的重要依赖项:

           <dependency>
            <groupId>org.jboss.arquillian</groupId>
            <artifactId>arquillian-bom</artifactId>
            <version>1.1.7.Final</version>
            <scope>test</scope>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupId>org.jboss.arquillian.test</groupId>
            <artifactId>arquillian-test-spi</artifactId>
            <version>1.1.7.Final</version>
        </dependency>


        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <version>1.1.7.Final</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.wildfly</groupId>
            <artifactId>wildfly-arquillian-container-embedded</artifactId>
            <version>8.2.0.Final</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.wildfly</groupId>
            <artifactId>wildfly-embedded</artifactId>
            <version>8.2.0.Final</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.wildfly.plugins</groupId>
            <artifactId>wildfly-maven-plugin</artifactId>
            <version>1.0.2.Final</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-depchain</artifactId>
            <version>2.1.1</version>
            <scope>test</scope>
            <type>pom</type>
        </dependency>



</dependencies>
    <!-- Plugins -->
    <build>
        <plugins>

            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <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.2.0.Final</version>
                                    <type>zip</type>
                                    <overWrite>false</overWrite>
                                    <outputDirectory>target</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>

                    <execution>
                        <id>copy-db-driver</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.microsoft</groupId>
                                    <artifactId>sqljdbc</artifactId>
                                    <version>4.0.2206.100</version>
                                    <outputDirectory>target/wildfly-8.2.0.Final/standalone/deployments</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.wildfly.plugins</groupId>
                <artifactId>wildfly-maven-plugin</artifactId>
                <version>1.0.2.Final</version>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>

                    <execution>
                        <id>datasource</id>
                        <phase>package</phase>
                        <goals>
                            <goal>add-resource</goal>
                        </goals>
                        <configuration>
                            <address>subsystem=datasources,data-source=tests</address>
                            <resources>
                                <resource>
                                    <properties>
                                        <connection-url>jdbc:sqlserver://SERVERADRESS_CENSORED;instanceName=web;databaseName=TMCDB</connection-url>
                                        <jndi-name>java:jdbc/TMCDB</jndi-name>
                                        <enabled>true</enabled>
                                        <enable>true</enable>
                                        <user-name>USER_CENSORED</user-name>
                                        <password>PW_CENSORED</password>
                                        <driver-name>sqljdbc-4.0.2206.100.jar</driver-name>
                                        <use-ccm>false</use-ccm>
                                    </properties>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

在我的 persistence.xml 中,我设置了 transaction-type="JTA"。

这是 .war 文件由 arquillian/shrinkwrap 创建的部分。 这东西用 Arquillian 的 @Deployment 标签注解:

PomEquippedResolveStage loadPomFromFile = Maven.resolver().loadPomFromFile("pom.xml");
        File[] asFile = loadPomFromFile.importRuntimeAndTestDependencies().resolve().withTransitivity().asFile();


        //MavenStrategyStage asFile = loadPomFromFile.importRuntimeAndTestDependencies().resolve();

        WebArchive webArchive = ShrinkWrap.create(WebArchive.class)
                .addAsLibraries(asFile)
                .addPackages(true, "de.companyXYZ")
                .addPackages(true, "org.springframework.beans.factory.config")
                // Adding persistence unit
                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                // Add ms-sql driver for the embedded wildfly
                .addAsWebInfResource("wildfly-ds-driver.xml")
                // Add datasource for the embedded wildfly
                .addAsWebInfResource("wildfly-ds.xml")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");

        webArchive.as(ZipExporter.class).exportTo(new File("./target/test-package.war"), true);

那么,有人可以帮我解决这个问题吗? 我不明白为什么我会得到这个异常——因为野蝇应该做事务管理——这就是我对他的期望......

【问题讨论】:

  • 能否请您介绍一下您是如何获得 entityManager 的?
  • 我正在使用依赖注入获取 entityManager - 容器应该给我一个实例:@Inject @PersistenceType(PERSISTENCE_UNIT) private EntityManager em;所以在代码中,我只是调用像 em.create(projectEntity); 这样的东西。嵌入的wildfly会给我一个依赖注入的实体管理器实例......
  • 请在您的问题中添加一些测试代码,以便我们首先看到您的测试在做什么。

标签: java jpa wildfly jta jboss-arquillian


【解决方案1】:

我添加,这解决了我的交易问题

import org.jboss.arquillian.transaction.api.annotation.Transactional;

    @Transactional

【讨论】:

  • 你在哪里添加的?在方法上,您在哪里调用 .create() ?还是实体类?
  • 就在测试方法的顶部,就像在常规方法上注释常规事务一样。
猜你喜欢
  • 2016-06-18
  • 1970-01-01
  • 2016-08-10
  • 2011-03-29
  • 2020-04-24
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
  • 2020-05-03
相关资源
最近更新 更多