Glassfish and PostgreSQL

All | Personal | Sun
20060221 Tuesday February 21, 2006

Pramod Gopinath's Weblog

Glassfish and Postgresql database.


What were the versions of the database and jdbc driver used ?
The version of the postgres database used was 8.0.1 and the jdbc driver was postgresql-8.2dev-500.jdbc3.jar.

What are the jdbc parameters for connecting to a postgresql database ?
If you are planning to connect to the postgres database running on another machine ensure that the database allows for TCP/IP connections to it. Refer the chapter titled "Client Authentication" in the Postgresql manual to enable clients to connect to your postgresql database.
The jdbc connection parameters for Postgres are as follows :

driverName "org.postgresql.Driver"
ConnectionUrl "jdbc:postgresql://<ip-address/machineName>:<port>/<dbName>"
userName "<userNameAllowedForDB>"
password "<password>"


How do you configure Glassfish to connect to a Postgresql database ?
Ensure that the jdbc connection pool is properly configured in Glassfish. The ideal way would be to use the admin gui (http://localhost:4848) screen to achieve this. But unless the fix for  issue is made you would need to do this steps using the appropriate asadmin create-jdbc-connection-pool command e.g

asadmin create-jdbc-connection-pool --user admin --passwordfile passwords.txt --host localhost --port 4848  --datasourceclassname org.postgresql.ds.PGSimpleDataSource --restype javax.sql.DataSource --property portNumber=5432:password=postgres:user=postgres:serverName=localhost:databaseName=testDB PostgresPool

After the jdbc connection pool has been added to domain.xml, restart the server. You can the add a jdbc resource by using the admin gui.


What are some of known Issues/Workarounds/Recommendations ?
This is the first cut at providing support for Postgresql database with glassfish. Please file issues if and when you find them. Here are some issues that we are aware of at this point of time and suggested workarounds. This is not a exhaustive list. We have added stuff that were seen during my tests using Postgresql database.

    1. Postgresql stores the table and column names in lower case, unless you have quoted the names. This is important when dealing with native queries and SqlResultSetMapping annotation. Ensure that you have specified the correct names to get the results back from the database.

    2. At this point of time we do not support the following postgresql specific datatypes :
        Monetary Types, Geometric Types, Network Address Types, Arrays, Composite Types, Object Identifier Types and Pseudo Types.

    3. Postgresql has a datatype called "SERIAL" for which the database creates an implicit sequence. The name of the sequence is <tableName>_<serialColumnName>_seq.
        Lets now relate it to EJB3 and Glassfish.
        If the user has defined the following in their Employee entity class
        @Id
        @GeneratedValue=(strategy=SEQUENCE)
        @Column(name="CUST_ID")
        public Long getId() { return id; }
       
and used java2db feature, the table created in postgresql database would have the CUST_ID defined as SERIAL.
        In this first implementation support for Postgresql database in Glassfish, the recommendation is to define the sequence name too.
        So change your code sample listed above to now read

        @Id
        @GeneratedValue(strategy=SEQUENCE, generator="EMPLOYEE_SEQUENCE_GENERATOR")
        @SequenceGenerator(name="EMPLOYEE_SEQUENCE_GENERATOR", sequenceName="EMPLOYEE_CUST_ID_SEQ")
        @Column(name="CUST_ID")
        public Long getId() { return id; }
        
Sequence name that would be used (w/o the schema name). This information would be used by glassfish to get the id value after an insert statement. Until we make changes to the code base please use this workaround. Posted by pramodg ( Feb 21 2006, 02:53:18 PM PST ) Permalink Comments [10]

Trackback URL: http://blogs.sun.com/Glassfish_PostgreSQL/entry/introducing_support_for_postgresql_in
Comments:

In this first implementation support for Postgresql database in Glassfish, the recommendation is to define the sequence name too.

But in future versions TopLink will use postgresql sequences (with their correct names) by default?

Posted by Roger Keays on May 30, 2006 at 10:48 PM PDT #

I successfully installed postgres support for glassfish, and the ping command from the web interface is working BUT what do I have to write in my persistence.xml? I am getting mad about this file!

Posted by Max on November 28, 2006 at 02:22 AM PST #

Max, here's the persistence.xml from my app, courtesy of much auto-generation by netbeans 5.5. Notice the "jdbc/PgRsrc_Dev" which is a call to the jndi resource that you need to set up in glassfish that points to the the postgresql conneciton pool you've created and used in your ping. In my case, this command did the trick:

asadmin create-jdbc-resource --connectionpoolid PgPool_Dev jdbc/PgRsrc_Dev

...but you can do this from the admin browser interface too.

Now, for the persistence.xml...

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="WVL-ejbPU" transaction-type="JTA">
<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
<jta-data-source>jdbc/PgRsrc_Dev</jta-data-source>
<properties>
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>


You would, of course, change the "cdrop-and-create-tables" directive to "update" later in your app development.

I hope this helped.

Posted by Dick Goldman on November 30, 2006 at 09:57 PM PST #

Hello, first nice article :-). I have a question regarding the constraint that postgres can not handle geometric types. We are using Postgis (the geospatial extension of Postgres) in our daily business. The types of the extension would also be not supported right ? Is there any possiblity or way to integrate this functionalty in glassfish resp. to enable glassfish to use it. best regards

Posted by Sebastian Puhl on January 09, 2007 at 08:36 AM PST #

I have also question about geometric types, is it planned to support these? In what timeframe? Any tips how to add this support myself?

Posted by Rigmor Ukuhe on February 01, 2007 at 06:21 AM PST #

how can i create a postgresql connection pool without asadmin interface and specify it in the persistence.xml file?

Posted by Ketan Maheshwari on April 29, 2007 at 09:43 AM PDT #

How can I set up a connection pool with SSL on. The JDBC URL I need to use is:
jdbc:postgresql://marina/encuesta_db?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory

I try to add two additional properties for ssl but glassfish ignore that.

Thank for all

Marcelo

Posted by Marcelo D. Ré on October 09, 2007 at 05:56 AM PDT #

Hi I am trying to connect to a postgres database and getting error. Connection failed. "wrong driver: org.postgresql.Driver for url: jdbc.postgresql://localhost:5432/postgres"

I am using the latest driver and also tried with other drivers.
connecion string "jdbc.postgresql://localhost:5432/postgres"
Jar file = "postgresql-8.2-507.jdbc3.jar"
Driver = org.postgresql.Driver

This works in the Sun Enterprise studio 8.2 but not in the java creator 2 update 1. with Postgres 8.2 database.

Is there any other jar file or driver that I should try?

Posted by madhura on January 11, 2008 at 01:58 PM PST #

Thanks!!!

Working with glassfish integrated into Netbeans 6.1 and using a Postgresql 8.2 database on another server. I kept on getting the errors:

"RAR5038:Unexpected exception while creating resource for pool post-gre-sqlPool. Exception : No PasswordCredential found"

It turned out "JUST" add the password field

http://localhost:4848/
Go to the connectionpools
Select the Postgresql connection pool
Go to the additional properties and add the attribute:
password
Fill in your password and you're all set and done to test your app.

Wessel

Posted by Wessel de Roode on May 17, 2008 at 03:58 PM PDT #

Works like a charm. Thank you for the hint on the sequenceName parameter.
-Toddy

Posted by Toddy Malen on July 07, 2008 at 09:19 AM PDT #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed

Calendar

RSS Feeds

Search

Links

Navigation

Referers