Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
*.class
*.jar
classes/
1 change: 0 additions & 1 deletion java/jdbc-ucp
Submodule jdbc-ucp deleted from 5e5c92
111 changes: 111 additions & 0 deletions java/jdbc/ConnectionManagementSamples/DRCPSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.*/
/*
DESCRIPTION
The sample code shows how Java applications can connect to the Oracle
Database using Database Resident Connection Pool (DRCP) as the server
side connection pool which can be shared across multiple middle tiers
or clients. DRCP pools database server processes and sessions (the
combination is known as a "pooled server"). A Connection Broker manages
the "pooled servers" in the database instance. Upon a request from
the client, the connection broker picks the appropriate "pooled server" and
hands-off the client to that pooled server. The client directly
communicates with the "pooled server" for all its database activity.
The "pooled server" is handed back to the connection broker when the
client releases it.

DRCP can be used with any third party client-side connection pool such as
DBCP, C3PO etc., Third party client side connection pools must attach and
detach connections explicitly to the connection broker through
attachServerConnection() and detachServerConnection(). They should also
set Connection Class as shown in the sample.

Use-case for DRCP: DRCP should be used in applications when multiple middle
tiers are connected to the same database, DRCP allows you to share
server-side resources between the middle-tier's independent connection
pools. For more details on DRCP refer to JDBC Developer's guide
(https://docs.oracle.com/database/121/JJDBC/toc.htm)


PRE-REQUISITE: DRCP should be configured at the server side before using DRCP.
Refer to JDBC Developers Reference Guide for more details. The sample DRCP URL
shown below refers to the client side configuration of DRCP.

Step 1: Enter the Database details in this file.
DB_USER, DB_PASSWORD and DRCP_URL are required.
A Sample DRCP URL is shown below. (server=POOLED) identifies
that DRCP is enabled on the server side.
DRCP_URL = jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=
(ADDRESS=(PROTOCOL=tcp)(HOST=proddbcluster)(PORT=5521)))
(CONNECT_DATA=(SERVICE_NAME=proddb)(server=POOLED)))
Step 2: Run the sample with "ant DRCPSample"

NOTES
Use JDK 1.7 and above

MODIFIED (MM/DD/YY)
nbsundar 03/02/15 - Creation
*/
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import oracle.jdbc.OracleConnection;
import oracle.jdbc.pool.OracleDataSource;
/*
* The method shows how to use DRCP when a third party client side connection
* pool is used. Make sure that connection URL used is correct and DRCP is
* configured both at the server side and client side.
*/
public class DRCPSample {
final static String DRCP_URL= "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=myhost)(PORT=1521)(PROTOCOL=tcp))(CONNECT_DATA=(SERVICE_NAME=myorcldbservicename)(server=POOLED)))";
final static String DB_USER = "hr";
final static String DB_PASSWORD = "hr";

static public void main(String args[]) throws SQLException {
// Create an OracleDataSource instance and set properties
OracleDataSource ods = new OracleDataSource();
ods.setUser(DB_USER);
ods.setPassword(DB_PASSWORD);
// Make sure to use the correct DRCP URL. Refer to sample DRCP URL.
ods.setURL(DRCP_URL);
// "Connection class" allows dedicating a subset of pooled server to
// a specific application. Several connection classes may be
// used for different applications
Properties connproperty = new Properties();
connproperty.setProperty("oracle.jdbc.DRCPConnectionClass",
"DRCP_connect_class");
ods.setConnectionProperties(connproperty);

// AutoCloseable: Closes a resource that is no longer needed.
// With AutoCloseable, the connection is closed automatically.
try (OracleConnection connection = (OracleConnection) (ods.getConnection())) {
System.out.println("DRCP enabled: " + connection.isDRCPEnabled());
// Explicitly attaching the connection before its use
// Required when the client side connection pool is not UCP
connection.attachServerConnection();
// Perform any database operation
doSQLWork(connection);
// Explicitly detaching the connection
// Required when the client side connection pool is not UCP
connection.detachServerConnection((String) null);
}
}
/*
* Displays system date(sysdate). Shows a simple database operation.
*/
public static void doSQLWork(Connection connection) throws SQLException {
// Statement and ResultSet are AutoCloseable by this syntax
try (Statement statement = connection.createStatement()) {
try (ResultSet resultSet = statement
.executeQuery("select SYSDATE from DUAL")) {
while (resultSet.next())
System.out.print("Today's date is " + resultSet.getString(1) + " ");
}
}
System.out.println("\n");
}
}


100 changes: 100 additions & 0 deletions java/jdbc/ConnectionManagementSamples/DataSourceSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.*/
/*
DESCRIPTION
The code sample shows how to use the DataSource API to establish a connection
to the Database. You can specify properties with "setConnectionProperties".
This is the recommended way to create connections to the Database.

Note that an instance of oracle.jdbc.pool.OracleDataSource doesn't provide
any connection pooling. It's just a connection factory. A connection pool,
such as Universal Connection Pool (UCP), can be configured to use an
instance of oracle.jdbc.pool.OracleDataSource to create connections and
then cache them.

Step 1: Enter the Database details in this file.
DB_USER, DB_PASSWORD and DB_URL are required
Step 2: Run the sample with "ant DataSourceSample"

NOTES
Use JDK 1.7 and above

MODIFIED (MM/DD/YY)
nbsundar 02/17/15 - Creation
*/

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import oracle.jdbc.pool.OracleDataSource;
import oracle.jdbc.OracleConnection;
import java.sql.DatabaseMetaData;

public class DataSourceSample {
// The recommended format of a connection URL is the long format with the
// connection descriptor.
final static String DB_URL= "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=myhost)(PORT=1521)(PROTOCOL=tcp))(CONNECT_DATA=(SERVICE_NAME=myorcldbservicename)))";
final static String DB_USER = "hr";
final static String DB_PASSWORD = "hr";

/*
* The method gets a database connection using
* oracle.jdbc.pool.OracleDataSource. It also sets some connection
* level properties, such as,
* OracleConnection.CONNECTION_PROPERTY_DEFAULT_ROW_PREFETCH,
* OracleConnection.CONNECTION_PROPERTY_THIN_NET_CHECKSUM_TYPES, etc.,
* There are many other connection related properties. Refer to
* the OracleConnection interface to find more.
*/
public static void main(String args[]) throws SQLException {
Properties info = new Properties();
info.put(OracleConnection.CONNECTION_PROPERTY_USER_NAME, DB_USER);
info.put(OracleConnection.CONNECTION_PROPERTY_PASSWORD, DB_PASSWORD);
info.put(OracleConnection.CONNECTION_PROPERTY_DEFAULT_ROW_PREFETCH, "20");
info.put(OracleConnection.CONNECTION_PROPERTY_THIN_NET_CHECKSUM_TYPES,
"(MD5,SHA1,SHA256,SHA384,SHA512)");
info.put(OracleConnection.CONNECTION_PROPERTY_THIN_NET_CHECKSUM_LEVEL,
"REQUIRED");

OracleDataSource ods = new OracleDataSource();
ods.setURL(DB_URL);
ods.setConnectionProperties(info);

// With AutoCloseable, the connection is closed automatically.
try (OracleConnection connection = (OracleConnection) ods.getConnection()) {
// Get the JDBC driver name and version
DatabaseMetaData dbmd = connection.getMetaData();
System.out.println("Driver Name: " + dbmd.getDriverName());
System.out.println("Driver Version: " + dbmd.getDriverVersion());
// Print some connection properties
System.out.println("Default Row Prefetch Value is: " +
connection.getDefaultRowPrefetch());
System.out.println("Database Username is: " + connection.getUserName());
System.out.println();
// Perform a database operation
printEmployees(connection);
}
}
/*
* Displays first_name and last_name from the employees table.
*/
public static void printEmployees(Connection connection) throws SQLException {
// Statement and ResultSet are AutoCloseable and closed automatically.
try (Statement statement = connection.createStatement()) {
try (ResultSet resultSet = statement
.executeQuery("select first_name, last_name from employees")) {
System.out.println("FIRST_NAME" + " " + "LAST_NAME");
System.out.println("---------------------");
while (resultSet.next())
System.out.println(resultSet.getString(1) + " "
+ resultSet.getString(2) + " ");
}
}
}
}


93 changes: 93 additions & 0 deletions java/jdbc/ConnectionManagementSamples/InternalT2Driver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.*/
/*
DESCRIPTION
The server-side Type 2 (T2S) driver (aka KPRB driver) is for Java in the
database. Java running in the database session uses the KPRB driver or
T2S driver to access data, locally.
We furnish the server-side thin JDBC (aka Type 4 server driver) for
accessing data in other session in the same database or a remote Oracle
database.

Step 1: Connect to SQLPLUS using the database USER/PASSWORD.
Make sure to have InternalT2Driver.sql accessible on the
client side to execute.
Step 2: Run the SQL file after connecting to DB "@InternalT2Driver.sql"

NOTES
Use JDK 1.6 and above

MODIFIED (MM/DD/YY)
nbsundar 03/23/15 - Creation (kmensah - Contributor)
*/
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import oracle.jdbc.driver.OracleDriver;
import oracle.jdbc.pool.OracleDataSource;


public class InternalT2Driver {

static public void jrun() throws SQLException {
// For testing InternalT2Driver
// test("jdbc:oracle:kprb:@");
test("jdbc:default:connection");
}
/*
* Shows using the server side Type 2 driver a.k.a KPRB driver
*/
static public void test(String url) throws SQLException {
Connection connection = null;
try {
System.out.println("Connecting to URL " + url);
// Method 1: Using OracleDataSource
OracleDataSource ods = new OracleDataSource();
ods.setURL(url);
connection = ods.getConnection();
System.out.println("Method 1: Getting Default Connection "
+ "using OracleDataSource");
// Perform database operation
printEmployees(connection);

// Method 2: Using defaultConnection() method
OracleDriver ora = new OracleDriver();
connection = ora.defaultConnection();
System.out.println("Method 2: Getting Default Connection "
+ "using OracleDriver");
// Perform database operation
printEmployees(connection);
}
finally {
if (connection != null) connection.close();
}
}

/*
* Displays employee_id and first_name from the employees table.
*/
static public void printEmployees(Connection connection) throws SQLException {
ResultSet resultSet = null;
Statement statement = null;
try {
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT employee_id, first_name FROM "
+ "employees order by employee_id");
while (resultSet.next()) {
System.out.println("Emp no: " + resultSet.getInt(1) + " Emp name: "
+ resultSet.getString(2));
}
}
catch (SQLException ea) {
System.out.println("Error during execution: " + ea);
ea.printStackTrace();
}
finally {
if (resultSet != null) resultSet.close();
if (statement != null) statement.close();
}
}
}


37 changes: 37 additions & 0 deletions java/jdbc/ConnectionManagementSamples/InternalT2Driver.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Rem InternalT2Driver.sql
Rem
Rem Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
Rem
Rem NAME
Rem InternalT2Driver.sql
Rem
Rem DESCRIPTION
Rem SQL for invoking the method which gets a server side connection to
Rem internal T2 Driver
Rem
Rem MODIFIED (MM/DD/YY)
Rem nbsundar 03/23/15 - Created
Rem kmensah 03/23/15 - Contributor

rem Reads the content of the Java source from InternalT2Driver.java
rem then compiles it
connect hr/hr
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED InternalT2Driver_src AS
@ InternalT2Driver.java
/

show error

rem A wrapper (a.k.a. Call Spec), to invoke Java
rem function in the database from SQL, PL/SQL, and client applications
CREATE OR REPLACE PROCEDURE InternalT2Driver_proc AS
LANGUAGE JAVA NAME 'InternalT2Driver.jrun ()';
/

rem Running the sample
connect hr/hr
SET SERVEROUTPUT ON SIZE 10000
CALL dbms_java.set_output (10000);

execute InternalT2Driver_proc;

Loading