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驱动包不完整或库引用路径不一致:
- 同时混用了手动下载的Derby压缩包和Maven依赖的驱动,导致
server.xml中配置的library指向目录与实际驱动存放位置不匹配; - Derby嵌入式驱动需要
derby.jar、derbytools.jar等完整包,仅复制derby.jar和derbyshared.jar缺少必要类。
修复步骤
方案一:统一使用Maven管理Derby依赖(推荐)
- 删除手动下载的
db-derby-10.15.2.0-bin目录,避免路径混淆; - 修改
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目录; - 确保
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> - 执行
mvn clean install liberty:run重新构建启动应用。
方案二:使用手动下载的Derby包
- 确保
db-derby-10.15.2.0-bin/lib目录下包含derby.jar、derbytools.jar、derbyshared.jar等所有JAR包; - 修改
server.xml的library配置,明确指定包含的JAR文件(避免遗漏):<library id="derbyJDBCLib"> <fileset dir="db-derby-10.15.2.0-bin/lib" includes="derby*.jar" /> </library> - 删除
pom.xml中liberty-maven-plugin的copyDependencies配置,避免重复复制导致冲突; - 执行
mvn clean install liberty:run重新启动。
额外验证
启动后可通过Open Liberty控制台的数据源配置页面(路径:https://localhost:9443/adminCenter)验证数据源是否连接成功,检查驱动类是否正确加载。
内容的提问来源于stack exchange,提问作者stitch123
相关产品推荐
相关产品推荐

