|
| 1 | +/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.*/ |
| 2 | +/* |
| 3 | + DESCRIPTION |
| 4 | + The sample code shows how Java applications can connect to the Oracle |
| 5 | + Database using Database Resident Connection Pool (DRCP) as the server |
| 6 | + side connection pool which can be shared across multiple middle tiers |
| 7 | + or clients. DRCP pools database server processes and sessions (the |
| 8 | + combination is known as a "pooled server"). A Connection Broker manages |
| 9 | + the "pooled servers" in the database instance. Upon a request from |
| 10 | + the client, the connection broker picks the appropriate "pooled server" and |
| 11 | + hands-off the client to that pooled server. The client directly |
| 12 | + communicates with the "pooled server" for all its database activity. |
| 13 | + The "pooled server" is handed back to the connection broker when the |
| 14 | + client releases it. |
| 15 | + |
| 16 | + DRCP can be used with any third party client-side connection pool such as |
| 17 | + DBCP, C3PO etc., Third party client side connection pools must attach and |
| 18 | + detach connections explicitly to the connection broker through |
| 19 | + attachServerConnection() and detachServerConnection(). They should also |
| 20 | + set Connection Class as shown in the sample. |
| 21 | + |
| 22 | + Use-case for DRCP: DRCP should be used in applications when multiple middle |
| 23 | + tiers are connected to the same database, DRCP allows you to share |
| 24 | + server-side resources between the middle-tier's independent connection |
| 25 | + pools. For more details on DRCP refer to JDBC Developer's guide |
| 26 | + (https://docs.oracle.com/database/121/JJDBC/toc.htm) |
| 27 | + |
| 28 | + |
| 29 | + PRE-REQUISITE: DRCP should be configured at the server side before using DRCP. |
| 30 | + Refer to JDBC Developers Reference Guide for more details. The sample DRCP URL |
| 31 | + shown below refers to the client side configuration of DRCP. |
| 32 | +
|
| 33 | + Step 1: Enter the Database details in this file. |
| 34 | + DB_USER, DB_PASSWORD and DRCP_URL are required. |
| 35 | + A Sample DRCP URL is shown below. (server=POOLED) identifies |
| 36 | + that DRCP is enabled on the server side. |
| 37 | + DRCP_URL = jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST= |
| 38 | + (ADDRESS=(PROTOCOL=tcp)(HOST=proddbcluster)(PORT=5521))) |
| 39 | + (CONNECT_DATA=(SERVICE_NAME=proddb)(server=POOLED))) |
| 40 | + Step 2: Run the sample with "ant DRCPSample" |
| 41 | + |
| 42 | + NOTES |
| 43 | + Use JDK 1.7 and above |
| 44 | +
|
| 45 | + MODIFIED (MM/DD/YY) |
| 46 | + nbsundar 03/02/15 - Creation |
| 47 | + */ |
| 48 | +import java.sql.Connection; |
| 49 | +import java.sql.Statement; |
| 50 | +import java.sql.ResultSet; |
| 51 | +import java.sql.SQLException; |
| 52 | +import java.util.Properties; |
| 53 | + |
| 54 | +import oracle.jdbc.OracleConnection; |
| 55 | +import oracle.jdbc.pool.OracleDataSource; |
| 56 | +/* |
| 57 | + * The method shows how to use DRCP when a third party client side connection |
| 58 | + * pool is used. Make sure that connection URL used is correct and DRCP is |
| 59 | + * configured both at the server side and client side. |
| 60 | + */ |
| 61 | +public class DRCPSample { |
| 62 | + final static String DRCP_URL= "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=myhost)(PORT=1521)(PROTOCOL=tcp))(CONNECT_DATA=(SERVICE_NAME=myorcldbservicename)(server=POOLED)))"; |
| 63 | + final static String DB_USER = "hr"; |
| 64 | + final static String DB_PASSWORD = "hr"; |
| 65 | + |
| 66 | + static public void main(String args[]) throws SQLException { |
| 67 | + // Create an OracleDataSource instance and set properties |
| 68 | + OracleDataSource ods = new OracleDataSource(); |
| 69 | + ods.setUser(DB_USER); |
| 70 | + ods.setPassword(DB_PASSWORD); |
| 71 | + // Make sure to use the correct DRCP URL. Refer to sample DRCP URL. |
| 72 | + ods.setURL(DRCP_URL); |
| 73 | + // "Connection class" allows dedicating a subset of pooled server to |
| 74 | + // a specific application. Several connection classes may be |
| 75 | + // used for different applications |
| 76 | + Properties connproperty = new Properties(); |
| 77 | + connproperty.setProperty("oracle.jdbc.DRCPConnectionClass", |
| 78 | + "DRCP_connect_class"); |
| 79 | + ods.setConnectionProperties(connproperty); |
| 80 | + |
| 81 | + // AutoCloseable: Closes a resource that is no longer needed. |
| 82 | + // With AutoCloseable, the connection is closed automatically. |
| 83 | + try (OracleConnection connection = (OracleConnection) (ods.getConnection())) { |
| 84 | + System.out.println("DRCP enabled: " + connection.isDRCPEnabled()); |
| 85 | + // Explicitly attaching the connection before its use |
| 86 | + // Required when the client side connection pool is not UCP |
| 87 | + connection.attachServerConnection(); |
| 88 | + // Perform any database operation |
| 89 | + doSQLWork(connection); |
| 90 | + // Explicitly detaching the connection |
| 91 | + // Required when the client side connection pool is not UCP |
| 92 | + connection.detachServerConnection((String) null); |
| 93 | + } |
| 94 | + } |
| 95 | + /* |
| 96 | + * Displays system date(sysdate). Shows a simple database operation. |
| 97 | + */ |
| 98 | + public static void doSQLWork(Connection connection) throws SQLException { |
| 99 | + // Statement and ResultSet are AutoCloseable by this syntax |
| 100 | + try (Statement statement = connection.createStatement()) { |
| 101 | + try (ResultSet resultSet = statement |
| 102 | + .executeQuery("select SYSDATE from DUAL")) { |
| 103 | + while (resultSet.next()) |
| 104 | + System.out.print("Today's date is " + resultSet.getString(1) + " "); |
| 105 | + } |
| 106 | + } |
| 107 | + System.out.println("\n"); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | + |
0 commit comments