Setting up an Oracle Database in Glassfish for JEE Applications

Setting up an Oracle database connection in Glassfish 4 is relatively simple, but can be confusing and a little frustrating at times. Hopefully this mini tutorial will get you up and running quickly and painlessly.

Setup Pool

Select Resources –> JDBC –> JDBC Connection Pools and then select New.

For Step 1., enter in a name (in this example jdbc). Select Resource Type to be of type javax.sql.DataSource, and select Oracle as Database driver vendor :

dbglass1

Leave everything as is in Step 2.

Look at your database settings in your DB explorer. This example shows how my database looks like in Oracle SQL Developer

dbglass3

You need to get values from this properties screen and create a properties string that glassfish server can understand.

Click on the pool you created previously and leave everything as is in General and in Advanced tabs. However in the Additional Properties tab add three strings. The username and password (username would be SYS from the previous screenshot).

The URL is formed by adding jdbc:oracle:thin at the start. Looking at the previous screenshot, this would be followed by the username again (SYS), the characters @//, the server hostname (mydbserver), the port number (1522) and the service name (DB1).

dbglass2

Setup Resource

Save these connection pool properties. Now go to the JDBC-Resource node and select New. Select the pool in the drop-down list (In this case it would be jdbc). Give the resource a name (which is also its JNDI name). In this example we will call it myJDBC.

dbglass4

 

Configure Persistence XML

Finally we just need to reference this resource in our persistence XML file. The name we gave the resource (myJDBC) needs to be set in the XML file like so :

dbglass5

 

Now you should be connected. Write some EJBs and see if they talk to the database.

 


Posted in Uncategorized | Tagged , , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

THREE_COLUMN_PAGE