Monday, January 12, 2015

GlassFish: Connection Pool Setup for PostgreSQL

In this post we are gonna explain how to make a JDBC data source registered over JNDI to access it from a web app.  After a DataSource object is registered within JNDI, an application can use the JNDI API to access it and connect to the datasource it represents.

You need install PostgreSQL and Glassfish, in this example I installed:  

glassfish 4.0
psql (PostgreSQL) 9.1.14

We will explain the required settings step-by-step:
    • Copy the previous driver into [glassfish_home]/glassfish/domains/domain1/lib/. In this example /opt/glassfish4/glassfish/domains/domain1/lib/
    • Access to the administration panel in Glassfish (http://localhost:4848). Go to the tab Resources/JDBC/JDBC Connection Pools:
      • Create a new connection pool and select the resource type javax.sql.DataSource, database vendor PostgreSQL and click next.
      • As datasource classname enter org.postgresql.ds.PGSimpleDataSource and insert the next additional properties:
                      ServerName (
                      Url  (something similar to this one: jdbc:postgresql://
                      PortNumber (5432 is the default port)

      • You can test the connection using the button "Ping".
    • In order to be able to use this connection pool in java applications, it needs a JNDI registration. Go to the tab Resources/JDBC/JDBC Resources and configure the new resource. Set the JNDI name jdbc/[database_name] and select the connection pool previously created. This JNDI name will be used by applications to access the PostgreSQL database.

    No comments:

    Post a Comment