gpt4 book ai didi

java - 阿基利安 : Transaction is required to perform this operation

转载 作者:行者123 更新时间:2023-11-29 03:10:35 25 4
gpt4 key购买 nike

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

  1. 嵌入式 wildfly 将部署在项目的/target 文件夹中
  2. 将部署 MSSQL 数据库驱动程序并将其注册为驱动程序
  3. arquillian 创建一个包含所有必需依赖项的 .war 文件
  4. arquillian将我项目的.war部署到embedded wildfly上,并在embedded 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);

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

最佳答案

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

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

@Transactional

关于java - 阿基利安 : Transaction is required to perform this operation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29599974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com