Configuring JNDI Data Source connecting to Oracle DB in GlassFish Server

This post shows how to configure JNDI data source connecting to Oracle database in GlassFish server.  Although this post is written to help readers of  RESTful Java Web Services - Second Edition to run the examples shipped along with the book, the steps discussed here are generic in nature and applicable for any Maven based project that you want to run from NetBeans IDE.

NetBeans  is a free IDE  for developing and running Java applications, including enterprise applications(Java EE). NetBeans comes with an integrated GlassFish Server to deploy and run your Java EE web applications. If you are building a Java EE web application from scratch using NetBeans, the IDE will automatically deploys the data source used in the application(if any) to GlassFish server, and thereby freeing you from the responsibilities of  defining the data source in the target server.

  • What if  you want to run a Java EE example, shipped by third party,  from NetBeans IDE? 
  • How do you define JNDI data source used in this example in the integrated GlassFish server? 

This post details the steps that you may want to follow for running a Maven based JAX-RS application that uses JNDI data source connecting to Oracle database  from NetBeans IDE.

Prerequisite:
Here are the steps for defining JNDI data source connecting to Oracle database in integrated GlassFish server (which comes with NetBeans IDE).

1.  Copy the ojdbc7.jar that you downloaded to the lib folder in default domain. The lib folder is located at :  <glassfish-4.1>/glassfish/domains/domain1/lib


2.  Open NetBeans IDE. In the main menu, choose Windows | Services. In the Services tab, right click on default Server and choose Start. This is shown in the following diagram:



Alternatively, yous can start GlassFish server using command line utility:
<glassfish-4.1>/bin/asadmin start-domain domain1 --verbose


3.  GlassFish starts on port 8080 by default. While starting server sometimes you may see port already in use exception (java.net.BindException: Address already in use: JVM_Bind )  - if you have any other application already listening on default port used by GlassFish(8080). In such cases, you can modify the default port used by GlassFish as follows:
  • Go to <glassfish-4.1>/glassfish/domains/domain1/config
  • Open domain.xml in a text editor 
  • Find "8080" (appears under network-listeners element) and replace it with an unused port number. In the following example http-listener-1 (line 2)  is configured to use 28080 as port:
1
2
3
4
5
6
7
8
<network-listeners>
   <network-listener protocol="http-listener-1" port="28080" name="http-listener-1" thread-pool="http-thread-pool" transport="tcp">
   </network-listener>
   <network-listener protocol="http-listener-2" port="8181" name="http-listener-2" thread-pool="http-thread-pool" transport="tcp">
   </network-listener>
   <network-listener protocol="admin-listener" port="4848" name="admin-listener" thread-pool="admin-thread-pool" transport="tcp">
   </network-listener>
</network-listeners>

4.  Once GlassFish is up and running,  Open the admin console in the browser by navigating to: http://localhost:4848/  and then in the home page, navigate to Resources | JDBC | JDBC Connection Pools. Add a new JDBC Connection Pool(e.g HRDSPool ). Choose the the Resource Type as javax.sql.DataSource and Database Driver Vendor as Oracle.  This is shown in following screenshot.


5.  Choose Additional Properties tab and enter User(e.g hr), Password(e.g hr), URL(e.g jdbc:oracle:thin:@localhost:1521:XE), and DataSourceName(e.g OracleDataSource) as shown in the following screenshot. Save all changes.



6.  To define JNDI data source, go to  Resources | JDBC | JDBC Resources. Add a new JDBC resource, set the JNDI name(e.g HRDS ) and then choose the JDBC connection pool that we created in last step as value for Pool Name(e.g: HRDSPool )

7.  Save changes by clicking OK.

8.  Now go back to NetBeans IDE and open the JAX-RS maven project that you want to run from NetBeans IDE. You can open the project by choosing File | Open Project. The following JAX-RS example  application uses JPA to  read and write data to Oracle DB.


 9.  We need to modify the data source name used in this application with the one that we defined in Step 6,. To do this, open the src/main/resources/persistence.xml in the editor and set the Data Source to the JNDI name that we defined in Step 6 (e.g HRDS). This is shown in the following screenshot. This example connects to HR database schema via the the HRDS data source.


10.   We are done with data source configuration.  To run the project, right click the project and choose Run:


To learn more on configuring GlassFish server, visit the following link: https://glassfish.java.net/docs/4.0/administration-guide.pdf

Comments

Disclaimer

The views expressed on this blog are my own and do not necessarily reflect the views of my employer.