2015-12-13 40 views
9

我正在開發一個包含JAR和WAR文件的EAR文件的簡單Java EE應用程序。在根據EarContent/META-INF的EAR項目,我有以下的GlassFish-resources.xml中:爲什麼不創建一個jdbc連接池?

<?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。

漢克

+0

你可以發佈你的stacktrace嗎? – Abdelhak

+0

我沒有收到任何錯誤消息,因此沒有堆棧跟蹤。 – iHank

回答

1

看來,JDBC數據源的Glassfish的V4配置從V3改變了一點。您需要指定connectionAttributes屬性來創建數據庫。屬性createDatabase沒有記錄,可能被忽略。

屬性connectionAttributes不據可查任一,但你會發現在Glassfish的4 PDF文檔的例子(Administration Guide - 部分治理JDBC connectino池),或在Payara server,這是從Glassfish的4

+0

我試圖插入connectionAttributes屬性,但它沒有工作... – iHank

+0

還有更多建議嗎? – iHank

1

衍生來源一個月前我曾遇到類似的問題。在最後一刻,我只好按照我的個XML狀態(相比於你的):

與GlassFish resources.xml中

  • 我加入URLdriverClass屬性glassfish-resources.xml
  • 不知道這是強制性的,但我有不同的值 jndi-namepool-name
  • 我用mysql,所以我的jdbc-connection-pool標籤有這兩個attrib茨: res-type="javax.sql.DataSource",
    datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
  • 沒有createDatabase財產

編輯:這裏是我的GlassFish資源(它實際上是由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輕鬆管理。

+0

你能分享一下你的glassfish-resources.xml嗎?看起來像Glassfish服務器不忽略我的glassfish-resources.xml,但一些屬性丟失。 – iHank

+0

沒問題,完成了! –