Creating a Connection Pool to PostgreSQL on Glassfish V3
I recently created a new connection pool to PostgreSQL on Glassfish and I would like to share the steps I followed with you. Obviously, you need PostgreSQL and Glassfish installed on your machine and a database already created in PostgreSQL. If you didn’t configure your new PostgreSQL installation yet, follow the steps I described at this previous post and come back here to continue with the connection pool. The necessary steps are:
-
We need the PostgreSQL JDBC Driver, since Glassfish and its deployed applications are writen in Java. Drivers are available for download at http://jdbc.postgresql.org. For this experiment choose the JDBC4 driver.
-
Download the driver file
postgresql-<version>.jdbc4.jar
and copy it to the diretory[glassfish_home]/glassfish/domains/domain1/lib/
. -
Restart Glassfish in order to make it load the new database driver. I thought that adopting an OSGI architecture Glassfish would never need restarts again, but I was wrong. At least, the restarting process is faster than V2.
-
Enter in the administration console and go to
Resources/JDBC/Connection Pools
. -
Create a new connection pool with the name
[database_name]Pool
, select the resource typejavax.sql.ConnectionPoolDataSource
, select the database vendorPostgreSQL
and click next. -
Select the datasource classname
org.postgresql.ds.PGConnectionPoolDataSource
and inform the following additional properties:DatabaseName=[database-name]
Password=*******
PortNumber=5432
(this is the default port but make sure that you are using the correct one)ServerName=[server-name|ip]
User=<database-username>
. -
Click “Finish” to save the new connection pool.
-
Go to the list of connection pools again and select the new one that you just created.
-
Click on “Ping” to check if the connection was correctly configured. The message “Ping Succeeded” means that the connection is working fine.
In order to be able to use this connection pool in JEE applications, we have to create a JNDI name for it:
-
Go to
Resources/JDBC/JDBC Resources
. -
Click on “New” and set the JNDI Name
jdbc/[database_name]
, select the connection pool created above and click “Ok” to finish. This JNDI name will be used by applications to access the PostgreSQL database.
These instructions may work with Glassfish V2 as well, since its database configuration is quite similar.
I’m being very specific in terms of chosen technologies, but if you have a slightly different configuration and these steps are not working yet, please comment your issues below, describing also your current configuration/context. Maybe, we can figure it out.