Skip to content

Commit b250f81

Browse files
authored
Merge pull request #2 from oracle/restructure
Restructure
2 parents a28c7bd + 4f50b44 commit b250f81

21 files changed

+2156
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
*.class
3+
*.jar
4+
classes/

java/jdbc-ucp

Submodule jdbc-ucp deleted from 5e5c927
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.*/
2+
/*
3+
DESCRIPTION
4+
The code sample shows how to use the DataSource API to establish a connection
5+
to the Database. You can specify properties with "setConnectionProperties".
6+
This is the recommended way to create connections to the Database.
7+
8+
Note that an instance of oracle.jdbc.pool.OracleDataSource doesn't provide
9+
any connection pooling. It's just a connection factory. A connection pool,
10+
such as Universal Connection Pool (UCP), can be configured to use an
11+
instance of oracle.jdbc.pool.OracleDataSource to create connections and
12+
then cache them.
13+
14+
Step 1: Enter the Database details in this file.
15+
DB_USER, DB_PASSWORD and DB_URL are required
16+
Step 2: Run the sample with "ant DataSourceSample"
17+
18+
NOTES
19+
Use JDK 1.7 and above
20+
21+
MODIFIED (MM/DD/YY)
22+
nbsundar 02/17/15 - Creation
23+
*/
24+
25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.sql.Connection;
28+
import java.sql.ResultSet;
29+
import java.sql.SQLException;
30+
import java.sql.Statement;
31+
import java.util.Properties;
32+
33+
import oracle.jdbc.pool.OracleDataSource;
34+
import oracle.jdbc.OracleConnection;
35+
import java.sql.DatabaseMetaData;
36+
37+
public class DataSourceSample {
38+
// The recommended format of a connection URL is the long format with the
39+
// connection descriptor.
40+
final static String DB_URL= "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=myhost)(PORT=1521)(PROTOCOL=tcp))(CONNECT_DATA=(SERVICE_NAME=myorcldbservicename)))";
41+
final static String DB_USER = "hr";
42+
final static String DB_PASSWORD = "hr";
43+
44+
/*
45+
* The method gets a database connection using
46+
* oracle.jdbc.pool.OracleDataSource. It also sets some connection
47+
* level properties, such as,
48+
* OracleConnection.CONNECTION_PROPERTY_DEFAULT_ROW_PREFETCH,
49+
* OracleConnection.CONNECTION_PROPERTY_THIN_NET_CHECKSUM_TYPES, etc.,
50+
* There are many other connection related properties. Refer to
51+
* the OracleConnection interface to find more.
52+
*/
53+
public static void main(String args[]) throws SQLException {
54+
Properties info = new Properties();
55+
info.put(OracleConnection.CONNECTION_PROPERTY_USER_NAME, DB_USER);
56+
info.put(OracleConnection.CONNECTION_PROPERTY_PASSWORD, DB_PASSWORD);
57+
info.put(OracleConnection.CONNECTION_PROPERTY_DEFAULT_ROW_PREFETCH, "20");
58+
info.put(OracleConnection.CONNECTION_PROPERTY_THIN_NET_CHECKSUM_TYPES,
59+
"(MD5,SHA1,SHA256,SHA384,SHA512)");
60+
info.put(OracleConnection.CONNECTION_PROPERTY_THIN_NET_CHECKSUM_LEVEL,
61+
"REQUIRED");
62+
63+
OracleDataSource ods = new OracleDataSource();
64+
ods.setURL(DB_URL);
65+
ods.setConnectionProperties(info);
66+
67+
// With AutoCloseable, the connection is closed automatically.
68+
try (OracleConnection connection = (OracleConnection) ods.getConnection()) {
69+
// Get the JDBC driver name and version
70+
DatabaseMetaData dbmd = connection.getMetaData();
71+
System.out.println("Driver Name: " + dbmd.getDriverName());
72+
System.out.println("Driver Version: " + dbmd.getDriverVersion());
73+
// Print some connection properties
74+
System.out.println("Default Row Prefetch Value is: " +
75+
connection.getDefaultRowPrefetch());
76+
System.out.println("Database Username is: " + connection.getUserName());
77+
System.out.println();
78+
// Perform a database operation
79+
printEmployees(connection);
80+
}
81+
}
82+
/*
83+
* Displays first_name and last_name from the employees table.
84+
*/
85+
public static void printEmployees(Connection connection) throws SQLException {
86+
// Statement and ResultSet are AutoCloseable and closed automatically.
87+
try (Statement statement = connection.createStatement()) {
88+
try (ResultSet resultSet = statement
89+
.executeQuery("select first_name, last_name from employees")) {
90+
System.out.println("FIRST_NAME" + " " + "LAST_NAME");
91+
System.out.println("---------------------");
92+
while (resultSet.next())
93+
System.out.println(resultSet.getString(1) + " "
94+
+ resultSet.getString(2) + " ");
95+
}
96+
}
97+
}
98+
}
99+
100+
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.*/
2+
/*
3+
DESCRIPTION
4+
The server-side Type 2 (T2S) driver (aka KPRB driver) is for Java in the
5+
database. Java running in the database session uses the KPRB driver or
6+
T2S driver to access data, locally.
7+
We furnish the server-side thin JDBC (aka Type 4 server driver) for
8+
accessing data in other session in the same database or a remote Oracle
9+
database.
10+
11+
Step 1: Connect to SQLPLUS using the database USER/PASSWORD.
12+
Make sure to have InternalT2Driver.sql accessible on the
13+
client side to execute.
14+
Step 2: Run the SQL file after connecting to DB "@InternalT2Driver.sql"
15+
16+
NOTES
17+
Use JDK 1.6 and above
18+
19+
MODIFIED (MM/DD/YY)
20+
nbsundar 03/23/15 - Creation (kmensah - Contributor)
21+
*/
22+
import java.sql.Connection;
23+
import java.sql.ResultSet;
24+
import java.sql.SQLException;
25+
import java.sql.Statement;
26+
27+
import oracle.jdbc.driver.OracleDriver;
28+
import oracle.jdbc.pool.OracleDataSource;
29+
30+
31+
public class InternalT2Driver {
32+
33+
static public void jrun() throws SQLException {
34+
// For testing InternalT2Driver
35+
// test("jdbc:oracle:kprb:@");
36+
test("jdbc:default:connection");
37+
}
38+
/*
39+
* Shows using the server side Type 2 driver a.k.a KPRB driver
40+
*/
41+
static public void test(String url) throws SQLException {
42+
Connection connection = null;
43+
try {
44+
System.out.println("Connecting to URL " + url);
45+
// Method 1: Using OracleDataSource
46+
OracleDataSource ods = new OracleDataSource();
47+
ods.setURL(url);
48+
connection = ods.getConnection();
49+
System.out.println("Method 1: Getting Default Connection "
50+
+ "using OracleDataSource");
51+
// Perform database operation
52+
printEmployees(connection);
53+
54+
// Method 2: Using defaultConnection() method
55+
OracleDriver ora = new OracleDriver();
56+
connection = ora.defaultConnection();
57+
System.out.println("Method 2: Getting Default Connection "
58+
+ "using OracleDriver");
59+
// Perform database operation
60+
printEmployees(connection);
61+
}
62+
finally {
63+
if (connection != null) connection.close();
64+
}
65+
}
66+
67+
/*
68+
* Displays employee_id and first_name from the employees table.
69+
*/
70+
static public void printEmployees(Connection connection) throws SQLException {
71+
ResultSet resultSet = null;
72+
Statement statement = null;
73+
try {
74+
statement = connection.createStatement();
75+
resultSet = statement.executeQuery("SELECT employee_id, first_name FROM "
76+
+ "employees order by employee_id");
77+
while (resultSet.next()) {
78+
System.out.println("Emp no: " + resultSet.getInt(1) + " Emp name: "
79+
+ resultSet.getString(2));
80+
}
81+
}
82+
catch (SQLException ea) {
83+
System.out.println("Error during execution: " + ea);
84+
ea.printStackTrace();
85+
}
86+
finally {
87+
if (resultSet != null) resultSet.close();
88+
if (statement != null) statement.close();
89+
}
90+
}
91+
}
92+
93+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Rem InternalT2Driver.sql
2+
Rem
3+
Rem Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
4+
Rem
5+
Rem NAME
6+
Rem InternalT2Driver.sql
7+
Rem
8+
Rem DESCRIPTION
9+
Rem SQL for invoking the method which gets a server side connection to
10+
Rem internal T2 Driver
11+
Rem
12+
Rem MODIFIED (MM/DD/YY)
13+
Rem nbsundar 03/23/15 - Created
14+
Rem kmensah 03/23/15 - Contributor
15+
16+
rem Reads the content of the Java source from InternalT2Driver.java
17+
rem then compiles it
18+
connect hr/hr
19+
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED InternalT2Driver_src AS
20+
@ InternalT2Driver.java
21+
/
22+
23+
show error
24+
25+
rem A wrapper (a.k.a. Call Spec), to invoke Java
26+
rem function in the database from SQL, PL/SQL, and client applications
27+
CREATE OR REPLACE PROCEDURE InternalT2Driver_proc AS
28+
LANGUAGE JAVA NAME 'InternalT2Driver.jrun ()';
29+
/
30+
31+
rem Running the sample
32+
connect hr/hr
33+
SET SERVEROUTPUT ON SIZE 10000
34+
CALL dbms_java.set_output (10000);
35+
36+
execute InternalT2Driver_proc;
37+

0 commit comments

Comments
 (0)