为什么不创建jdbc连接池?

时间:2015-12-13 21:24:30

标签: java java-ee jdbc glassfish

我正在使用包含JAR和WAR文件的EAR文件开发一个简单的Java EE应用程序。在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。

汉克

2 个答案:

答案 0 :(得分:1)

看来,jdbc数据源的Glassfish v4配置从v3稍微改变了一下。您需要指定$('.add'+n).click(function(){ //place code here }); 属性来创建数据库。属性connectionAttributes未记录,可能已被忽略。

属性createDatabase也没有详细记录,但您可以在Glassfish 4 PDF文档(Administration Guide - 管理JDBC connectino池中)或Payara server的源中找到示例,它源自Glassfish 4。

答案 1 :(得分:1)

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

<强>的glassfish-resources.xml中

  • 我将URLdriverClass属性添加到glassfish-resources.xml
  • 不确定是否必须这样做,但我有不同的值 jndi-namepool-name
  • 我使用了mysql,所以我的jdbc-connection-pool标签有这两个属性: res-type="javax.sql.DataSource",
    datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
  • 没有createDatabase属性

编辑:这是我的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轻松管理数据库生成。