Configuring a (Oracle) datasource on Glassfish html

Configuring a (Oracle) datasource on Glassfish

With Java EE 5 being final, application servers need to be upgraded to support features like EJB 3.0. In some cases, loading the Java EE 5 jars may be sufficient. But in most cases a fully Jave EE 5 compliant application server is needed. Enters Glassfish, the free, open
source
application server which implements
the newest features in the Java EE 5 platform
.

One of the issues I had with Glassfish was configuring a datasource so my web application could use it. This blog will tell you how I did it. I am dealing with configuring an Oracle datasource here, but the instructions should work similarly on other databases.

First, you’ll need Glassfish. Go to the Glassfish project page and download it. See the instructions at the bottom of the downloads page for how to install Glassfish. Next, start up the default domain on the Glassfish server. See the Glassfish Quick Start Guide for more instructions if this next command fails

asadmin start-domain
domain1

After having started you can check and verify that the installation and startup were done correctly by opening a browser and browsing to

http://localhost:4848

or to the correct host if you decided to install Glassfish elsewhere. This will open the login screen to the Glassfish admin console.

In order to get the Oracle (or your favourite database) loaded in Glassfish, Glassfish needs to be shut down again. To do this, issue

asadmin stop-domain
domain1

Next, copy the database driver jar to <glassfish install dir>/lib. In case of Oracle, the Glassfish Administration Guide asks for the ojdbc14.jar file. Once copied to the lib dir, start Glassfish again.

Reload the admin console and login again. Expand the Resources menu item and then the JDBC menu item in the menu to the left. The JDBC menu contains two submenus, being JDBC Resources and Connection Pools. The JDBC Resource is exposed to (web) applications through JNDI. In order to create a JDBC Resource, you’ll need a Connection Pool first.

To define an Oracle Connection Pool, click on connection Pools in th menu to the left. Next, click the New button to enter the Connection Pool wizard. The first thing to do is specify the Connection Pool Name, Resource Type and Database Vendor. The Resource Type should be javax.sql.ConnectionPoolDatasource. Select the database vendor from the select box. Please note that many database vendors are specified in the list, but for most of them the database drivers are missing! In case of Oracle, select Oracle (duh) and click the Next button.

In the General Settings section, make sure the Datasource Classname is set to either oracle.jdbc.pool.OracleDataSource (which is the default) or oracle.jdbc.xa.client.OracleXADataSource. Set all the other parameters as you like and scroll down to the Additional Properties section.
In this section, three or four properties are needed. All others can be discarded. The three needed ones are User, Password and URL.
The fourth, optional, property is xa-driver-does-not-support-non-tx-operations. This property is only needed if both non-XA and XA connections are retrieved from the same connection pool. Might degrade performance. See chater 2 of the Glassfish Administration Guide.
So, specify the username, password and url. The last one is of the form jdbc:oracle:thin:@<host>:<port>:<sid> where <port> usually is 1521.
After specifying these properties, click Finish. This will bring you back to the connection Pool overview. Click on the Connection Pool name you just created and then the Ping button to see if the connection can be established. If you see Ping Succeeded you’ve set up the connection pool correctly.

Now click on the JDBC Resources menu item in the menu to the left and then on the New button to create a new JDBC Resource. Enter the name for the JDBC Resource and bear in mind that JNDI JDBC datasource names usually start with “jdbc/” so enter a name like “jdbc/hr” or so. Also select the Connection Pool name you just created and successfully pinged from the select box. Hit the OK button and you’re done.

16 Comments

  1. Leonardo Derks December 6, 2011
  2. Michele A Smith November 19, 2010
  3. saket jha January 6, 2010
  4. Bertan June 5, 2009
  5. Aryan July 23, 2008
  6. Khaled Mahmoud October 20, 2007
  7. E Pitt August 17, 2007
  8. Alfred Huang July 10, 2007
  9. Craig Cruden June 1, 2007
  10. Craig Cruden June 1, 2007
  11. James Cummings April 30, 2007
  12. Boris Derzhavets December 7, 2006
  13. Adam Constabaris October 26, 2006
  14. cambazz August 7, 2006
  15. Wouter van Reeven July 20, 2006
  16. Eduardo Pelegri-Llopart July 16, 2006