gpt4 book ai didi

java - 为什么不创建 jdbc 连接池?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:24:58 25 4
gpt4 key购买 nike

我正在开发一个简单的 Java EE 应用程序,其中包含一个包含 JAR 和 WAR 文件的 EAR 文件。在 EarContent/META-INF 下的 EAR 项目中,我有以下 glassfish-resources.xml:

<?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-connection-pool name="java:app/jdbc/test" res-type="javax.sql.XADataSource" datasource-classname="org.apache.derby.jdbc.ClientXADataSource">
<property name="serverName" value="localhost"/>
<property name="portNumber" value="1527"/>
<property name="databaseName" value="test"/>
<property name="createDatabase" value="create"/>
<property name="user" value="APP"/>
<property name="password" value="APP"/>
</jdbc-connection-pool>
<jdbc-resource jndi-name="java:app/jdbc/test" pool-name="java:app/jdbc/test"/>
</resources>

在 META-INF 下的 EJB 项目中,我有以下 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="events" transaction-type="JTA">
<description>Manages events, users and comments</description>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:app/jdbc/test</jta-data-source>
<class>com.hank.entity.Question</class>
<class>com.hank.entity.QuizWalk</class>
<class>com.hank.entity.User</class>
<class>com.hank.entity.QuizWalkParticipants</class>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.logging.connection" value="false"/>
<property name="eclipselink.logging.level.sql" value="ALL"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.logging.session" value="false"/>
<property name="eclipselink.logging.thread" value="false"/>
<property name="eclipselink.logging.timestamp" value="false"/>
</properties>
</persistence-unit>
</persistence>

我有一个正在运行的 Derby 服务器并且端口号是正确的。该应用程序正在运行,只是没有创建数据库。有什么问题?这种方法适用于 Glassfish 3.1。

汉克

最佳答案

我在一个月前遇到过类似的问题。最后,我的 XML 中有以下状态(与您的相比):

glassfish-resources.xml

  • 我添加了 URLdriverClass属性为 glassfish-resources.xml
  • 不确定是否必须这样做,但我有不同的值(value)观 jndi-namepool-name
  • 我使用 mysql 所以我的 jdbc-connection-pool标记具有以下两个属性: res-type="javax.sql.DataSource",<br/>
    datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
  • 没有createDatabase属性(property)

编辑:这是我的 glassfish-resources(它实际上是由 netbeans 自动生成的,我只是添加了正确的凭据和资源名称)

<?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-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="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
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="MY_POOL"
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="serverName" value="localhost"/>
<property name="portNumber" value="3306"/>
<property name="databaseName" value="nippon"/>
<property name="User" value="root"/>
<property name="Password" value="root"/>
<property name="URL" value="jdbc:mysql://localhost:3306/nippon"/>
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true" jndi-name="JNDI_NAME" object-type="user" pool-name="MY_POOL"/>
</resources>

persistence.xml

<persistence version="2.1" 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_1.xsd">
<persistence-unit name="UNIT_NAME" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>JNDI_NAME</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>

根据当前的 GF 文档,glassfish-resources.xml仅用于生成服务器资源(连接池和 JDBC 资源),并且可以从 persistence.xml 轻松管理数据库生成.

关于java - 为什么不创建 jdbc 连接池?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34256544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com