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
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
After having started you can check and verify that the installation and startup were done correctly by opening a browser and browsing to
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
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.
- Setting up Oracle JNDI Datasource on Tomcat
- Using GlassFish Reference Implementation of EJB 3.0 Persistence with JDeveloper 10.1.3EA
- Starting with EJB 3.0 Persistence in Eclipse 3.1 using the GlassFish Reference Implementation
- Getting Started with EJB 3.0 Persistence out-of-container using the Reference Implementation (GlassFish)
- Diving deeper into EJB 3.0 Persistence with GlassFish (RI) – still out of container