Step 1.
Download the latest jdbc driver: http://jdbc.postgresql.org/download.html
Eg: postgresql-8.4-701.jdbc4.jar
Step 2.
Copy the downloaded archive postgresql-x.x.-xxx.jdbc4.jar in {glassfish}/lib directory and restart glassfish:
Eg: D:\programs\glassfish3.1b25\glassfish\lib
asadmin stop-domain domain1
asadmin start-domain domain1
Step3.
Add datasource details in web.xml (Java EE6)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<data-source>
<name>java:app/env/myDataSourceName</name>
<class-name>org.postgresql.Driver</class-name>
<server-name>localhost</server-name>
<port-number>5432</port-number>
<database-name>your_database_name</database-name>
<user>user_name</user>
<password>password</password>
<property>
<name>url</name>
<value>jdbc:postgresql://localhost:5432/your_database_name</value>
</property>
</data-source>
</web-app>
Step4. Get a Connection
Context ctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:app/env/myDataSourceName");
ds.getConnection();
Great posting.
ReplyDeleteMaking a postgresql connection pool in GlassFish 3 is as easy as it was in 2.1. See here:
http://mariosgaee.blogspot.com/2009/12/configure-postegresql-connection-pool.html
marios