2

I have a JAX-RS restful service which needs to access a MySQL database. I am trying to do this using CDI and a entity manager. However, when I publish the app, it appears that the incorrect persistence unit is being used (it's trying to connect on port 1527 instead of 3306).

The exception that is caught by the try/catch is:

javax.servlet.ServletException: 
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): 
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: connect.
Error Code: 0

Here is the restful service:

@Path("/databases")
@Stateless
public class DatabaseResource {

    @PersistenceUnit(unitName = "beta.example.services")
    EntityManagerFactory entityManagerFactory;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response list() {
        try {

            EntityManager entityManager = entityManagerFactory.createEntityManager();
            Connection connection = entityManager.unwrap(java.sql.Connection.class);

            ...

            return Response.ok().build();

        } catch (SchemaCrawlerException e) {
            return Response.status(500).entity(e.getMessage()).build();
        }
    }
}

The persistence unit (located in src/META-INF):

<?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="beta.example.services">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/" />
            <property name="javax.persistence.jdbc.user" value="test" />
            <property name="javax.persistence.jdbc.password" value="test" />
        </properties>
    </persistence-unit>
</persistence>

Any help would be appreciated

5
  • I would check the database connection. Are you able to connect to the database from the same machine as application using any other JDBC tools, like DbVisualizer?
    – DRCB
    Commented May 14, 2012 at 8:56
  • I am currently running the app on the same machine and able to connect. I initially set it up using the eclipse wizards (and can still ping the database). The strange part is that it doesn't even seem like its trying to connect to the correct database. It's trying to connect on port 1527 (which is derby I believe). I've also checked that when published the persistence.xml file exists at '/WEB-INF/classes/META-INF'
    – Paul Adams
    Commented May 14, 2012 at 9:02
  • Which appserver? Have you configured the datasource in the server config?
    – zeller
    Commented May 14, 2012 at 9:08
  • I am using Glassfish v3.1.2. I have setup the connection pool and resources to connect to that database (but that was for a different project). However, I haven't connected the persistence config to those settings. What would I need to change to connect the two?
    – Paul Adams
    Commented May 14, 2012 at 9:21
  • 2
    It seems you haven't defined the database name under the properties tag (niether in the connection string). Otherwise this should work, unless maybe the two persistence units have the same name (the derby and the mysql)
    – zeller
    Commented May 14, 2012 at 19:49

1 Answer 1

2

Ok, a persistence unit can be configured in two modes : RESOURCE_LOCAL and JTA.
Ex:

<persistence-unit name="beta.example.services" transaction-type="JTA">

VS

<persistence-unit name="beta.example.services" transaction-type="RESOURCE_LOCAL">

JTA is the default value. Properties like "javax.persistence.jdbc.*" will only be read when using "RESOURCE_LOCAL". When using JTA, the transaction manager of glassfish will be used. That means in this case you must specify JNDI name like this :

<jta-data-source>youJNDIName</jta-data-source>

What I think might be happening is that you use the default "JTA" transaction-type, but since you dont specify any jta-datasource, it might try to use the glassfish default one (wich point to derby).

Persistence unit as RESOURCE_LOCAL or JTA?

http://openejb.apache.org/jpa-concepts.html

Looking at how you use the entity manager, setting transaction-type to RESOURCE_LOCAL seems to be the solution for you.

Not the answer you're looking for? Browse other questions tagged or ask your own question.