Oracle WebLogic Server 12c: Configuring a JDBC Data Source


Options



Before You Begin

Purpose

This tutorial shows how to use the Oracle WebLogic Server 12c administration console to configure a JDBC data source.

Time to Complete

Approximately 1/2 hour

Background

A data source is an object that enables a Java Database Connectivity (JDBC) client to obtain a database connection. The data source has a collection of database connections called a connection pool. An application can request a connection from the data source, use the connection to access the database, and then close the connection. Rather than actually close the connection, however, the data source places it back in the connection pool to be used again. Data sources can be created as part of the configuration of an instance of WebLogic Server. When that server comes up, (or the data source is deployed), the server creates database connections to fill the connection pool of its data source.

Scenario

This tutorial covers using the Oracle WebLogic Server 12c (12.2.1) administration console to create a data source and its connection pool. The connections are to an Oracle Database.

Context

Before starting this tutorial, you should have completed the Oracle by Example tutorials titled Oracle WebLogic Server 12c: Installing WebLogic Server and Creating a Domain and Oracle WebLogic Server 12c: Configuring Managed Servers.

What Do You Need?

The following is a list of software requirements:

  • Oracle WebLogic Server 12c (12.2.1)
  • Oracle Database 12c or later (Express Edition may work as well but has not been tested.
    Note: In this tutorial Oracle Database XE 11.2 is used.

Setting Up the Schema in the Oracle Database

To set up the database schema required by the JDBC client application, perform the following steps:

  1. Download the jdbc_obe.zip file to the machine where your WebLogic Server domain and servers are. Extract the file contents to a local drive. In this tutorial, the files are extracted into the /u01/jdbc_obe/testds directory. The zip file contains three files:

    • testds_oracle.sql
      A SQL script to configure the required schema in the Oracle Database
    • testds.war
      A simple web application used to test the configured data source
    • deploy_testds.py
      A WebLogic Scripting Tool (WLST) script used to deploy the web application

  2. Log in to the system as the user that started the Oracle Database. Open a Terminal window and navigate to the location of the SQL script file, testds_oracle.sql. In this tutorial that is:

      cd /u01/jdbc_obe/testds  

    Navigate to the testds Directory
  3. Make sure that the database environment variables are set by printing them out:

      echo $ORACLE_HOME
    /u01/app/oracle/product/12.1.0/dbhome 1
       echo $ORACLE_SID
    orcl

    Environment Variables Verification.

    Note: The values of your environment variables may be different. If there are no values printed, then set the variables to the proper values with:

      export ORACLE_HOME=<valid_value>
         export ORACLE_SID=<valid_value>

  4. Then invoke SQL Plus as the sysdba user:

      $ORACLE_HOME/bin/sqlplus / as sysdba

    SQL Plus is invoked.

  5. Connect to the database as the system user. You can give the password and the name of the database as you connect. In this tutorial the command is:

      connect system/welcome1 as sysdba

    Connect as system user.
  6. Then run the SQL script with this command:

      @testds_oracle.sql

    Run the testds_oracle.sql scipt.

    The script, testds_oracle.sql, creates a user called DBTESTER with the password welcome1. It grants that user the abilities to create sessions, tables, and so on. It then connects to the database as that user and creates three tables: EMPLOYEE, WLS_CATALOG_ITEMS, and WLS_CLIENT_INFO. Finally, it inserts rows into those tables and exits.

Starting WebLogic Server

    To start the administration server and the server1 managed server of the domain, perform the following steps:

  1. If Node Manager is not already running, it needs to be started. To start it, in a Terminal window, navigate to the domain directory, and enter the command:

      ./startNodeManager.sh

    Navigate to the domain directory and start Node Manager
  2. If the administration server of the domain is not already running, it needs to be started. To start it, in another Terminal window, navigate to the domain directory, then cd into the bin subdirectory. Enter the command:

      ./startWebLogic.sh

    Start WebLogic Server.

  3. The managed server server1 should also be running. If it is not already running, start it. In another Terminal window, navigate to the domain directory, then cd into the bin subdirectory. Note: Use the host name and port of the administration server of your domain. Enter the command:

      ./startManagedWebLogic.sh server1 http://localhost:7001

    This runs the script to start the managed server named server1. Also notice the argument that gives the URL of the domain's administration server:
    http://localhost:7001.

    Start Managed Server server1

  4. When prompted for a username and password, enter the credentials of the domain administrator. Enter username and password

Configuring a JDBC Data Source

To configure a JDBC data source by using the WebLogic Server administration console, perform the following steps:

  1. After the servers are up and running, access the WebLogic Server administration console. Open a web browser and enter its URL:
    http://<hostname>:<port>/console

    In this tutorial, that is: http://localhost:7001/console

    Note: Use the host name and port of the administration server of your domain.

    Enter URL to the Weblogic Server Administration Console
  2. On the Welcome screen, log in using the Username and Password entered to start the servers.

    Weblogic Server Administration Console´s Login Screen

  3. Creating a data source is a change to the domain's configuration, so the configuration must first be locked. In the Change Center, click the Lock & Edit button.

    Lock the domain´s configuration.

  4. In the administration console under Domain Structure, expand Services (by clicking the + next to it). Then click Data Sources.

    Beneath the Change Center in the Domain Structure click Data Sources under Services.

    On the right, notice that the Summary of JDBC Data Sources section appears.

  5. Under the Data Sources table heading, click the New drop-down list. Then select Generic Data Source.

    Create a New Generic Data Source.

  6. On the first page of the Create a New JDBC Data Source wizard, enter the Name of the data source as myNewDS.

    Enter the JNDI Name of the data source as myNewDS.

    Note: There is no requirement that the data source name and the JNDI name match. The JNDI name is the one to make note of, because it is used by JDBC clients to access the data source.

    Use the Database Type drop-down list to select Oracle.

    Then click Next.

    Enter JDBC Data Source Properties.

  7. On the second page of the wizard, use the Database Driver drop-down list to select
    *Oracle's Driver (Thin) for Instance connections; Versions:9.0.1 and later.

    Then click Next.

    Leave the default and click Next.

  8. On the next page of the wizard, keep all the default Transaction Options, and click Next.

    Leave the default and click Next.

  9. On the next page of the wizard, enter the Database Name. In this tutorial it is orcl Your database name may be different.

    Enter the Host Name of the server1 managed server. In this tutorial it is localhost. Use the host name where your server1 server resides.

    Leave the database Port at the default of 1521. (Unless it is different for your database.)

    Enter the Database User Name of DBTESTER. This is the user that was created by the SQL script.

    Enter the database user's password twice, in the Password and Confirm Password fields. The password is welcome1.

    Then click Next.

    Enter more JDBC Data Source Properties.

  10. On the next page of the wizard, click the Test Configuration button to check if a connection to the Database can be made, based upon the information entered.

    Verify that you can connect to the database by testing the configuration.

    If the connection test fails, use the Back button to review the entries made for the data source and correct any errors. If there are no errors in the entries and the test still fails, make sure your database is running.

    If the message "Connection test succeeded" is displayed, click Next.

    Upon successful connection to the database click Next.

  11. On the last page of the wizard, the data source is targeted. Targeting a data source to a server means that the server will manage that data source and it will be available as one of the resources of that server.

    Select the check box next to server1.

    Then click Finish.

    Click Finish to create the new data source.

  12. In the Change Center click the Activate Changes button.

    Activate the Changes.

    The message "All changes have been activated. No restarts are necessary." is displayed.

  13. In the Summary of JDBC Data Sources area, in the Data Sources table, the new data source, myNewDS, is listed.

    The new data source is now listed.

  14. To modify the configuration of the new data source, click its name, myNewDS, in the table.

    Click the new data source to open it.

  15. Under the Settings for myNewDS, click the Connection Pool tab under the Configuration tab.

    Under the Configuration tab click the Connection Pool Subtab.

    Scroll down to find the "capacity" fields.

    For Initial Capacity enter 2.

    For Maximum Capacity enter 20.

    For Minimum Capacity enter 2.

    Then click Save.

    Enter your values and then save the changes.

    In the Change Center click the Activate Changes button.

    Activate the Changes.

    The messages "All changes have been activated. No restarts are necessary." and "Settings updated successfully." are displayed.

    Connection Pool changes updated.

  16. Under Domain Structure, expand Environment and then click Servers.

    Under Environment click Servers.

  17. In the Servers table, click the server server1.

    Click the Server server1.

  18. In the Settings for server1, under the Configuration tab and the General subtab, click the link View JNDI Tree.

    Click the link View JNDI Tree.

    The JNDI tree opens in a new browser window (or tab). Notice that myNewDS appears in the JNDI tree.

    Note: Other entries in the JNDI tree of your server can be very different from what is shown here. It all depends upon what resources your server has.

    View the JNDI Tree Structure in a new browser.

Testing the JDBC Data Source

To test the JDBC data source with a simple web application, perform the following steps:

  1. Open a new Terminal window and navigate to the /server/bin directory under the WebLogic installation directory. In this tutorial, that is:
    /u01/wls1221/wlserver/server/bin

  2. Then run the setWLSEnv.sh script as follows:

      source setWLSEnv.sh

    This will set the PATH and the CLASSPATH so the WLST deployment script can be executed.

    Run the setWLSEnv.sh script.

  3. Next navigate to the directory where the testds.war file resides. This file was part of the zip file extracted earlier. In this tutorial, the zip file was extracted into the /u01/jdbc_obe/testds directory.

    See the testds.war file listed.

  4. Before running the supplied WLST script, deploy_testds.py, you may need to edit it. The first line of the script uses the connect() command. The first argument of that command is the domain administrator's username, the second argument is that user's password, and the third argument is the host and port of the administration server of the domain. Make sure the values for these arguments are correct for your domain before running this script.

    Make any changes required to the script, and save the file.

    Edit and save the deploy_testds.py file.

  5. Run the deploy_testds.py script to deploy the web application in the testds.war file and target it to the server1 server. Do this by entering the following command:

      java weblogic.WLST deploy_testds.py

    Run the deploy_testds.py file.

    You should see a message that the deploy operation has succeeded.

    Note: You can ignore the warning about Context.close(). That warning is always displayed when running a WLST script and is not an issue.

  6. To verify the deployment was successful, go the the administration console, and under Domain Structure, click Deployments. Then in the Deployments table, find TestDS with the "Active" State.

    TestDS is Active.

  7. To use the deployed application, in another web browser, enter the host and port for the server1 managed server, followed by /testds.

    In this tutorial, the URL entered is:
    http://localhost:7003/testds

    URL to TestDS.

  8. When the application comes up, enter the following:

    Data Source Name: myNewDS
    Table Name: EMPLOYEE
    Username: weblogic (use your domain administrator's username)
    Password: welcome1. (use your domain administrator's password)
  9. Then click the Test Data Source button.

    Test the Data Source.

    The rows in the EMPLOYEE table are displayed below the fields.

    The EMPLOYEES table and its values are listed.

    The application uses the Data Source Name entered (myNewDS) as the JNDI name to look up the data source from server1, retrieves a database connection from that data source, and executes the SQL to select all the rows in the table entered in the Table Name field (EMPLOYEE).

    If you want to test the data source again, try a different table in the Table Name field. The other two tables are WLS_CATALOG_ITEMS and WLS_CLIENT_INFO.

Want to learn more?

In this tutorial, you learned how to:

    • Run a SQL script to set up a schema in an Oracle database
    • Start instances of WebLogic Server
    • Configure a JDBC data source by using the administration console
    • Test that data source by using a simple web application

    Resources

    Credits

    • Lead Curriculum Developer: Bill Bell
    • Original OBE Created By: TJ Palazzolo