我试图将Derby数据库与我的Open Liberty应用程序连接起来,但我无法让它工作。我想我没有正确理解这个过程。我是这么做的。
我从Derby网站下载了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>
在我的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
文件,使其包含如下内容:
<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].
说明:
Apache Derby依赖包似乎在10.14前后进行了重组。x→10.15.X时间框架,因此根据所使用的确切版本,确切的依赖项集可能略有不同。这个答案中的大部分pom.xml和server.xml配置应该可以在更改的两端工作,但是如果不能工作,请检查您正在使用的特定Derby版本所需的确切依赖项列表。
copyDependencies in liberty-maven-plugin
free -maven-plugin中copyDependencies函数背后的思想是,你可以让插件复制依赖项到合适的位置,而不必单独下载、存储,然后担心将这个依赖项JAR复制到正确的位置。
所以如果你有:
pom.xml
<!-- In the dependencies section -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.15.2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbytools</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>
<!-- Selects all artifacts under this groupId -->
<artifactId>*</artifactId>
</dependency>
</copyDependencies>
</configuration>
您可以将其与server.xml配置结合使用:
server.xml
<library id="derbyJDBCLib">
<fileset dir="${shared.resource.dir}/" includes="derby*.jar" />
</library>
附加链接您可以在我们的JPA指南示例中看到此操作:https://openliberty.io/guides/jpa-intro.html
Open Liberty有一个方便的机制来验证与数据库的连接。在更基本的服务器配置级别上,独立于任何应用程序(JDBC、JPA等)代码来验证这一点是很有帮助的。