首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在web.xml或其他地方指定可移植的数据源?

如何在web.xml或其他地方指定可移植的数据源?
EN

Stack Overflow用户
提问于 2017-10-07 10:09:22
回答 1查看 841关注 0票数 1

我刚开始使用Arquillian,并注意到必须在特定于服务器的文件(jboss-ds.xmlglassfish-resources.xml等)中指定要与JPA中的JTA一起使用的数据源,但是在Java >6中,应该可以在web.xml (或ejb-jar.xmlapplication.xmlapplication-client.xml)中指定它,例如

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    [...]
    <data-source>
        <name>jdbc/project1</name>
        <class-name>org.apache.derby.jdbc.EmbeddedDataSource</class-name>
        <server-name>localhost</server-name>
        <database-name>project1</database-name>
        <user>project1</user>
        <password>project1</password>
        <property>
            <name>connectionAttributes</name>
            <value>create=true</value>
        </property>
        <transactional>true</transactional>
        <isolation-level>TRANSACTION_READ_COMMITTED</isolation-level>
        <initial-pool-size>2</initial-pool-size>
        <max-pool-size>10</max-pool-size>
        <min-pool-size>5</min-pool-size>
        <max-statements>0</max-statements>
    </data-source>
</web-app>

并按如下方式使用它:

代码语言:javascript
复制
@Deployment
public static Archive<?> createDeployment() {
    WebArchive retValue = ShrinkWrap.create(WebArchive.class)
            .addClasses(MyManagedBean.class, SaveController.class, DefaultSaveController.class)
            .setWebXML("web.xml")
            .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    Stream.of(Maven.resolver().loadPomFromFile("pom.xml").importRuntimeDependencies().resolve().withTransitivity().as(JavaArchive.class)).forEach(archive -> retValue.addAsLibrary(archive));
    return retValue;
}

这将避免冗余。然而,数据源不可用,例如,由于Caused by: com.sun.appserv.connectors.internal.api.ConnectorRuntimeException: Invalid resource : jdbc/project1__pm导致GlassFish失败。

https://github.com/krichter722/arquillian-data-source-in-web-xml上有一个MCVE。

Arquillian似乎创建了一个可能有用的数据源,但我想测试我的生产环境的确切类型,因为这些测试已经非常接近集成。

EN

回答 1

Stack Overflow用户

发布于 2018-01-05 11:33:09

我建议遵循Arquillian JPA tutorial并使用您遵循的glassfish-resourcess.xml.If,您将看到以下maven项目的文件结构。

一旦你有了这个包和文件结构,然后通过添加<testResources>标签将你的测试资源指向pom.xml

pom.xml

代码语言:javascript
复制
<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>service</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>project-service</artifactId>
    <packaging>jar</packaging>
    
   <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jboss.arquillian</groupId>
                <artifactId>arquillian-bom</artifactId>
                <version>1.0.0.Final</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>infohub-entities</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.arquillian.container</groupId>
            <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.main.extras</groupId>
            <artifactId>glassfish-embedded-web</artifactId>
            <version>5.0</version>
        </dependency>
       
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
      
    </dependencies>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <name>project-service</name>
    
    <build>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
            </testResource>
            <testResource>
                <directory>src/test/resources-glassfish-embedded</directory>
            </testResource>
        </testResources>
    </build>
</project>

然后,您可以在glassfish-resources.xml中创建可移植的数据源配置。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
  <jdbc-resource enabled="true" jndi-name="jdbc/datasource" object-type="user" pool-name="connectionPool">
    <description/>
  </jdbc-resource>
  <jdbc-connection-pool allow-non-component-callers="false"
                    associate-with-thread="false"
                    connection-creation-retry-attempts="0"
                    connection-creation-retry-interval-in-seconds="10"
                    connection-leak-reclaim="false"
                    connection-leak-timeout-in-seconds="0"
                    connection-validation-method="auto-commit"
                    datasource-classname="oracle.jdbc.pool.OracleDataSource"
                    fail-all-connections="false"
                    idle-timeout-in-seconds="300"
                    is-connection-validation-required="false"
                    is-isolation-level-guaranteed="true"
                    lazy-connection-association="false"
                    lazy-connection-enlistment="false"
                    match-connections="false"
                    max-connection-usage-count="0"
                    max-pool-size="32" max-wait-time-in-millis="60000"
                    name="connectionPool"
                    non-transactional-connections="false"
                    pool-resize-quantity="2"
                    res-type="javax.sql.DataSource"
                    statement-timeout-in-seconds="-1"
                    steady-pool-size="8"
                    validate-atmost-once-period-in-seconds="0"
                    wrap-jdbc-objects="false">
    <property name="URL" value="jdbc:oracle:thin:@//url:port"/>
    <property name="User" value="username"/>
    <property name="Password" value="password"/>
  </jdbc-connection-pool>
</resources>

并指向arquillian.xml中的数据源

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns="http://jboss.org/schema/arquillian"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://jboss.org/schema/arquillian
        http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
    <container qualifier="glassfish-embedded" default="true">
        <configuration>
            <property name="resourcesXml">
                src/test/resources-glassfish-embedded/glassfish-resources.xml
            </property>
        </configuration>
    </container>
</arquillian>

因此您的数据源是可移植的,可以在每次测试开始时使用。要获得完整的解决方案,请访问Arquillian JPA Tutorial。或Arquillian Blog

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46616014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档