You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Open Liberty Maven项目配置Apache Derby依赖遇连接问题求助

Apache Derby与Open Liberty连接失败问题排查

操作步骤

  • 从Derby官网下载db-derby-10.15.2.0-bin.tar.gz并解压至项目根目录;
  • 在server.xml中配置Derby库与数据源:
    <!-- Derby Library Configuration -->
    <library id="derbyJDBCLib">
        <fileset dir="db-derby-10.15.2.0-bin/lib" />
    </library>
    
    <!-- Datasource Configuration -->
    <dataSource id="derbyjpadatasource" jndiName="jdbc/my-project">
        <jdbcDriver libraryRef="derbyJDBCLib" />
        <properties.derby.embedded databaseName="myDB" createDatabase="create" />
    </dataSource>
    
  • 在META-INF目录创建persistence.xml文件:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.2"
                 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_2.xsd">
        <persistence-unit name="jpa-unit" transaction-type="JTA">
            <jta-data-source>jdbc/my-project</jta-data-source>
            <properties>
                <property name="jakarta.persistence.schema-generation.database.action"
                          value="create"/>
                <property name="jakarta.persistence.schema-generation.scripts.action"
                          value="create"/>
                <property name="jakarta.persistence.schema-generation.scripts.create-target"
                          value="createDDL.ddl"/>
            </properties>
        </persistence-unit>
    </persistence>
    
  • 在pom.xml中配置liberty-maven-plugin:
    <plugin>
            <groupId>io.openliberty.tools</groupId>
            <artifactId>liberty-maven-plugin</artifactId>
            <version>3.7.1</version>
            <configuration>
                <copyDependencies>
                    <location>${project.build.directory}/liberty/wlp/usr/shared/resources</location>
                    <dependency>
                        <groupId>org.apache.derby</groupId>
                        <artifactId>derby</artifactId>
                    </dependency>
                </copyDependencies>
            </configuration>
    </plugin>
    

问题现象

项目可编译运行,但操作持久化对象时出现错误,启动服务器时收到如下警告:

DSRA4000E: No implementations of org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource40
for dataSource [derbyjpadatasource] with library derbyJDBCLib were found.
The name or location of the JDBC driver JAR files may be incorrect
or inaccessible. Searched in: []. Searched in packages: [org.apache.derby.jdbc].

调整配置后,新的错误信息为:

DSRA4000E: No implementations of 
org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource40 were 
found for dataSource[derbyjpadatasource] with the library 
derbyJDBCLib. The name or location of the JDBC driver JAR files 
may be incorrect or inaccessible. Searched in 
[/home/(my_usr)/Desktop/Projects/proj1/target/liberty/
wlp/usr/shared/resources/derby-10.15.2.0.jar, 
/home/(my_usr)/Desktop/Projects/proj1/target/liberty/
wlp/usr/shared/resources/derbyshared-10.15.2.0.jar]. Searched in 
packages: [org.apache.derby.jdbc].

解决方案

原因分析

错误提示找不到org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource40实现类,核心问题是Derby驱动包不完整或库引用路径不一致:

  1. 同时混用了手动下载的Derby压缩包和Maven依赖的驱动,导致server.xml中配置的library指向目录与实际驱动存放位置不匹配;
  2. Derby嵌入式驱动需要derby.jar、derbytools.jar等完整包,仅复制derby.jar和derbyshared.jar缺少必要类。

修复步骤

方案一:统一使用Maven管理Derby依赖(推荐)

  1. 删除手动下载的db-derby-10.15.2.0-bin目录,避免路径混淆;
  2. 修改server.xml,将library配置指向Maven依赖复制的位置:
    <library id="derbyJDBCLib">
        <fileset dir="${shared.resource.dir}" includes="derby*.jar" />
    </library>
    
    注:${shared.resource.dir}是Open Liberty内置变量,对应target/liberty/wlp/usr/shared/resources目录;
  3. 确保pom.xml中liberty-maven-plugin的copyDependencies包含完整Derby驱动依赖,补充derbytools:
    <plugin>
        <groupId>io.openliberty.tools</groupId>
        <artifactId>liberty-maven-plugin</artifactId>
        <version>3.7.1</version>
        <configuration>
            <copyDependencies>
                <location>${project.build.directory}/liberty/wlp/usr/shared/resources</location>
                <dependency>
                    <groupId>org.apache.derby</groupId>
                    <artifactId>derby</artifactId>
                    <version>10.15.2.0</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.derby</groupId>
                    <artifactId>derbytools</artifactId>
                    <version>10.15.2.0</version>
                </dependency>
            </copyDependencies>
        </configuration>
    </plugin>
    
  4. 执行mvn clean install liberty:run重新构建启动应用。

方案二:使用手动下载的Derby包

  1. 确保db-derby-10.15.2.0-bin/lib目录下包含derby.jar、derbytools.jar、derbyshared.jar等所有JAR包;
  2. 修改server.xml的library配置,明确指定包含的JAR文件(避免遗漏):
    <library id="derbyJDBCLib">
        <fileset dir="db-derby-10.15.2.0-bin/lib" includes="derby*.jar" />
    </library>
    
  3. 删除pom.xml中liberty-maven-plugin的copyDependencies配置,避免重复复制导致冲突;
  4. 执行mvn clean install liberty:run重新启动。

额外验证

启动后可通过Open Liberty控制台的数据源配置页面(路径:https://localhost:9443/adminCenter)验证数据源是否连接成功,检查驱动类是否正确加载。


内容的提问来源于stack exchange,提问作者stitch123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 16:27:29