Pierre Pierre - 6 months ago 122
Java Question

Java persistence API: Glassfish tries to use derby instead of MySQL?

I wrote the following persistence.xml for a glassfish webapp:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
version="2.1">

<persistence-unit name="mypu" transaction-type="RESOURCE_LOCAL">
<description>my</description>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>my.OntClass</class>

<properties>

<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/user_lindenb"/>
<property name="javax.persistence.jdbc.user" value="appuser"/>
<property name="javax.persistence.jdbc.password" value="apppassword"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="eclipselink.weaving" value="false"/><!-- http://java.dzone.com/articles/jpa-performance-optimization -->
<property name="eclipselink.target-database" value="MySQL"/>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.logging.parameters" value="false"/>
</properties>
</persistence-unit>

</persistence>


The MySQL server is running and I can connect to the MySQL server from a servlet using a:

@Resource (name="jdbc/MysqlDS" )
javax.sql.DataSource mysqlDS;


Now, I want to use the persistence API. But when I want to deploy my application it raises the following error:

remote failure: Error occurred during deployment: Exception while preparing the app : Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused.


As far as I understand, it tries to connect to Derby instead of MySQL?

glassfish log:
(...)
Caused by: org.apache.derby.client.am.DisconnectException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused.
at org.apache.derby.client.net.NetAgent.<init>(Unknown Source)
at org.apache.derby.client.net.NetConnection.newAgent_(Unknown Source)
at org.apache.derby.client.am.Connection.initConnection(Unknown Source)
at org.apache.derby.client.am.Connection.<init>(Unknown Source)
at org.apache.derby.client.net.NetConnection.<init>(Unknown Source)
at org.apache.derby.client.net.NetConnection40.<init>(Unknown Source)
at org.apache.derby.client.net.ClientJDBCObjectFactoryImpl40.newNetConnection(Unknown Source)
at org.apache.derby.jdbc.ClientDataSource.getConnectionX(Unknown Source)
... 71 more
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at javax.net.DefaultSocketFactory.createSocket(SocketFactory.java:271)
at org.apache.derby.client.net.OpenSocketAction.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 79 more
(...)


How can I fix this?

Thanks.

Answer

replaced

 <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

by

 <jta-data-source>jdbc/MysqlDS</jta-data-source>

seems to work now...

Comments