Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't a jdbc connection pool created?

I'm developing a simple Java EE application with an EAR-file including JAR- and WAR-files. In the EAR-project under EarContent/META-INF I have the following 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>

In the EJB-project under META-INF I have the following 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>

I have a running Derby server and the port number is correct. The application is working except that no database is created. What can be wrong? This approach worked with Glassfish 3.1.

Hank

like image 587
iHank Avatar asked Dec 13 '15 21:12

iHank


2 Answers

I was struggling with a similar problem a month ago. In the very end, I had following state in my XMLs (in comparison to yours):

glassfish-resources.xml

  • I added URL and driverClass properties to glassfish-resources.xml
  • Not sure if it is mandatory to do so, but I had different values for jndi-name and pool-name
  • I used mysql so my jdbc-connection-pool tag had these two attributes: res-type="javax.sql.DataSource",
    datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
  • No createDatabase property

Edit: here is my glassfish-resources (it was actually auto generated by netbeans, I just added proper credentials and resource names)

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

According to the current GF documentation, glassfish-resources.xml is only used for generation of server resources (connection pool and JDBC resource), and database generation is easily managed from persistence.xml.

like image 192
Vanja Lee Avatar answered Nov 15 '22 22:11

Vanja Lee


It seems that Glassfish v4 configuration of jdbc datasource changed a bit from v3. You need to specify connectionAttributes property to create the database. Property createDatabase is not documented and probably ignored.

Property connectionAttributes is not well documented either, but you may find an example in Glassfish 4 PDF documentation (Administration Guide - section Administering JDBC connectino pools), or in sources of Payara server, which is derived from Glassfish 4.

like image 28
OndroMih Avatar answered Nov 15 '22 22:11

OndroMih