arquillian,wildfly 将数据源更改为 mysql - 错误 missing/unsatisfied 依赖项

arquillian, wildfly change datasource to mysql - error missing/unsatisfied dependencies

我有使用默认 h2/mem 数据库的 arquillian 测试。现在我想切换到我的 sql。 我先把mysql-connector-java-5.1.33-bin.jar复制到standalone/deployments。日志确认正确部署。我还尝试通过 wildfly 的管理控制台设置 mysql 数据源并且它工作正常(我进行了连接测试但没有添加数据源)。 我想将数据源设置保留在项目的测试中。所以我将数据源添加到 src/test/resources/test-ds.xml:

<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 jndi-name="java:jboss/datasources/moritzTimetrackerTestDSmysql"
    pool-name="moritzTimetracker-test" enabled="true" use-java-context="true">
    <connection-url>jdbc:mysql://localhost:3306/moritztimetracker</connection-url>
    <driver>mysql-connector-java-5.1.33-bin.jar</driver>
    <security>
        <user-name>user</user-name>
        <password>pass</password>
    </security>
</datasource>

src/test/resources/META-INF/test-persistence.xml 看起来像这样:

<persistence version="2.1"
   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">
   <persistence-unit name="primary">
      <jta-data-source>java:jboss/datasources/moritzTimetrackerTestDSmysql</jta-data-source>
      <properties>
         <!-- Properties for Hibernate -->
         <property name="hibernate.hbm2ddl.auto" value="create-drop" />
         <property name="hibernate.show_sql" value="false" />
      </properties>
   </persistence-unit>
</persistence>

但是如果我 运行 测试我得到异常:

org.jboss.arquillian.container.spi.client.container.DeploymentException: Cannot deploy: test.war
...
Caused by: java.lang.Exception: {"JBAS014771: Services with missing/unavailable dependencies" => ["jboss.data-source.java:jboss/datasources/moritzTimetrackerTestDSmysql is missing [jboss.jdbc-driver.mysql-connector-java-5_1_33-bin_jar]"]}

日志说:

JBAS014775:    New missing/unsatisfied dependencies:
      service jboss.data-source.java:jboss/datasources/moritzTimetrackerTestDSmysql (missing) dependents: [service jboss.data-source.reference-factory.java:jboss/datasources/moritzTimetrackerTestDSmysql] 
      service jboss.data-source.reference-factory.java:jboss/datasources/moritzTimetrackerTestDSmysql (missing) dependents: [service jboss.naming.context.java.jboss.datasources.moritzTimetrackerTestDSmysql] 
      service jboss.jdbc-driver.mysql-connector-java-5_1_33-bin_jar (missing) dependents: [service jboss.data-source.java:jboss/datasources/moritzTimetrackerTestDSmysql] 
      service jboss.persistenceunit."test.war#primary".__FIRST_PHASE__ (missing) dependents: [service jboss.deployment.unit."test.war".POST_MODULE] 

如何正确设置 mysql 为数据源?

您的设置似乎是正确的(我的意思是数据源和持久性 xml)。

但请检查您是否在每个测试用例中随应用一起部署 test-ds.xml 文件。我的意思是:

@Deployment
public static Archive<?> createDeployment() {
    return ShrinkWrap.create(WebArchive.class, "test.war")
            .addAsResource("META-INF/test-persistence.xml", "META-INF/persistence.xml")
            .addAsWebInfResource("test-ds.xml")  /* CRUCIAL STEP */
            ...
}

通过这种方式,您的数据源定义随 EJB 的每个微部署一起部署。

由于记录非常糟糕,我在通过管理 Web 界面创建数据源后查看了 standalone.xml。以这种方式设置 mysql 数据源工作正常:

<datasource jndi-name="java:jboss/datasources/testDSmysql"
    pool-name="test" enabled="true" use-java-context="true">
    <connection-url>jdbc:mysql://localhost:3306/db</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <driver>mysql-connector-java-5.1.33-bin.jar_com.mysql.jdbc.Driver_5_1</driver>
    <security>
        <user-name>user</user-name>
        <password>pass</password>
    </security>
</datasource>

参见 "drive-class" 和 "driver" - 它与您在文档中阅读的内容有很大不同?!