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.
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:
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
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:
- Download and install NetBeans IDE. To download NetBeans visit: https://netbeans.org/downloads/ . We need GlassFish server to deploy and run the samples, so choose the NetBeans IDE Download Bundles which comes with an integrated GlassFish installation(To ensure this, choose the download bundle as 'Java EE' or 'All' in download page).
- Download and install Oracle Database 11g Express Edition (Oracle Database XE). To download Oracle XE go to http://www.oracle.com/technetwork/database/database-technologies/express-edition/downloads/index.html. Details are available here: http://docs.oracle.com/cd/E17781_01/index.htm.
- To connect to Oracle database, download the JDBC driver: ojdbc7.jar (or higher). You can find the binaries here: http://www.oracle.com/technetwork/database/features/jdbc/jdbc-drivers-12c-download-1958347.html
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:
<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.
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
good work done and keep update more.i like your information's and that is very much useful for readers.
ReplyDeletePython Training in Chennai
Python Training in Tambaram
Big data training in chennai
JAVA Training in Chennai
Software testing training in chennai
Python Training in Chennai
Python Course in Chennai
I really admired your post, such great and useful information that you have mentioned here.
ReplyDeleteAutomation Training in Chennai
Automation Anywhere Training in Chennai
RPA Classes in Chennai
Blue Prism Training Chennai
Blue Prism Training Institute in Chennai
RPA Training in Chennai
UiPath Training in Chennai
Blue Prism Training in Anna Nagar