0

I'm trying to write a Java EE application that uses JPA to access a database. Until now I just used the @Entity annotation and left everything else to the default state (for example the persistence.xml file was using _TimerPool as the jta-data-source, and I didn't create any db).

So I wanted to try and use an actual database. I went into the Services screen, JavaBD > Create new database, set it up with a name and password.

The DB's url: jdbc:derby://localhost:1527/Prova

Then I created the persistence.xml file for my application through Glassfish's wizard:

  <persistence-unit name="JobsPU" transaction-type="JTA">
    <jta-data-source>java:app/Prova</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
      <property name="javax.persistence.schema-generation.database.target" value="database-and-scripts"/>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/Prova"/>
      <property name="javax.persistence.jdbc.user" value="paolo"/>
      <property name="javax.persistence.jdbc.password" value="paolo"/>
    </properties>
  </persistence-unit>

And when I try to deploy I get this exception:

    Grave:   Exception while preparing the app : Invalid resource : { ResourceInfo : (jndiName=java:app/Prova__pm), (applicationName=Jobs) }
com.sun.appserv.connectors.internal.api.ConnectorRuntimeException: Invalid resource : { ResourceInfo : (jndiName=java:app/Prova__pm), (applicationName=Jobs) }

Seems to be related to the JNDI naming. Of what I honestly don't know, I'm still trying to learn. If I go to Glassfish's console, under the JNDI listing I can't see anything that seems to be related to my database (not in JDBC Connection Pools nor in JDBC Resources). What should I do?

Thank you very much in advance for any help.

Paul
  • 473
  • 6
  • 19

1 Answers1

0

To configure PersistenceUnit for EE container you have to use jta-data-source and transaction-type="JTA". For jta-data-source you have to specify JNDI name of JDBC ConnectionPool which have to be configured in EE container (glassfish server in your case). There is tutorial How to set up a JDBC Connection Pool on Glassfish. Also in this case PersistenceUnit properties like

 <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/Prova"/>
      <property name="javax.persistence.jdbc.user" value="paolo"/>
      <property name="javax.persistence.jdbc.password" value="paolo"/>

will be ignored for EE container (see this documentation). To use this properties you can configure PersistenceUnit for SE environment. Before this i recommend you to read article differences between RESOURCE_LOCAL and JTA persistence contexts. Configuration for SE application will be look like:

<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
    <provider>
        org.eclipse.persistence.jpa.PersistenceProvider
    </provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/Prova"/>
        <property name="javax.persistence.jdbc.user" value="paolo"/>
        <property name="javax.persistence.jdbc.password" value="paolo"/>
    </properties>
</persistence-unit>
Artem
  • 371
  • 4
  • 11