databasejakarta-eeglassfishdatabase-connectionpersistence

Error connecting to server localhost on port 1,527 when I have provided another port, 5432 in my persistence xml


In Java EE, I have a persistence.xml provided and is packaged correctly.

<?xml version="1.0" encoding="UTF-8" ?>
<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"
             version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="ArticleManagement">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.pauljabines.portfolio.Article</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/postgres" />
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.user" value="postgres" />
            <property name="javax.persistence.jdbc.password" value="************************" />
            <property name="eclipselink.logging.level" value="ALL" />
        </properties>
    </persistence-unit>
</persistence>

Question: With these connection properties, why is glassfish throwing error and saying that it can't connect to port 1527?

Here is a bit of the stacktrace:

org.glassfish.deployment.common.DeploymentException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): 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 1,527 with message Connection refused: connect.

Solution

  • The solution for this is to create a jdbc connection pool for postgresql in the glassfish. To do this:

    1. Download postgresql jdbc driver here and place it in glassfish5\glassfish\domains\domain1\lib.
    2. Restart your glassfish.
    3. Go to glassfish admin webpage localhost:4848.
    4. Go to JDBC -> JDBC Connection Pools
    5. Click new to create one.
    6. PoolName = anything you want
    7. Resource Type = javax.sql.ConnectionPoolDataSource
    8. Datasource Classname = org.postgresql.jdbc3.Jdbc3PoolingDataSource
    9. For the properties, create these:
    10. user = postgres, your user
    11. portNumber = 5432, the port number to your database
    12. password = the password to connect to your database
    13. databaseName = portfolio, the database name to connect to
    14. servername = localhost, your servername
    15. Ping to test.
    16. Save it.
    17. Go to JDBC -> resources and edit jdbc/__default.
    18. Set pool name to the poolname of your created pool.