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.
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 :
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
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).
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.
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 :
Now you should be connected. Write some EJBs and see if they talk to the database.