我正在尝试将 Derby 数据库连接到我的 Open Liberty 应用程序,但我无法让它工作。我认为我没有正确理解这个过程。这就是我所做的。
我从德比网站下载了
db-derby-10.15.2.0-bin.tar.gz
,并将其解压到我的项目的根文件夹中。
我将以下内容放入我的
server.xml
文件中:
<!-- 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>
在我的
persistence.xml
文件夹中创建了一个META-INF
文件:
<?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
文件,使其包含以下内容:
<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].
编辑: Scott Kurz 建议后的新错误消息:
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].
liberty-maven-plugin 中 copyDependencies 函数背后的想法是,您可以让插件将依赖项复制到位,而无需单独下载、存储,然后担心将此依赖项 JAR 复制到正确的位置。
所以如果你有
<!-- In the dependencies section -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.15.2.0</version>
<scope>provided</scope>
</dependency>
...
<!-- plugin configuration in build/plugins -->
<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>
您可以将其与 server.xml 配置结合使用:
<library id="derbyJDBCLib">
<fileset dir="${shared.resource.dir}/" includes="derby*.jar" />
</library>
您可以在我们的 JPA 指南示例中看到这一点:https://openliberty.io/guides/jpa-intro.html