|
1 | | -import java.sql.Connection; |
2 | | -import java.sql.ResultSet; |
3 | | -import java.sql.SQLException; |
4 | | -import java.sql.Statement; |
5 | | - |
6 | | -import oracle.ucp.jdbc.PoolDataSource; |
7 | | -import oracle.ucp.jdbc.PoolDataSourceFactory; |
8 | | - |
9 | | -/** |
10 | | - * This sample code demonstrates the functionality of Multi-tenant shared pools. |
11 | | - * With the use of shared pools now it is possible for more than one tenant |
12 | | - * datasources to share a common pool provided they are connecting to the same |
13 | | - * database with a single url. To use shared pool functionality, all the tenant |
14 | | - * datasources accessing shared pools should be defined in UCP XML configuration |
15 | | - * file along with the pool properties. The code example shows how to get a |
16 | | - * connection from a shared pool defined in xml. |
17 | | - * |
18 | | - * The shared pool defined in sample XML config file has only one connection and |
19 | | - * both the tenant datasources -tenat1_ds and tenant2_ds are reusing the same |
20 | | - * connection to get the employee data from respective employee tables |
21 | | - * (tenant1_emp and tenant2_emp) present in tenant1 PDB and tenant2 PDB. |
22 | | - * |
23 | | - * |
24 | | - */ |
25 | | -public class SharedPoolCodeSample { |
26 | | - // UCP XML config file location URI |
27 | | - private static final String xmlFileURI = "file:/test/ucp/config/SharedPoolCodeSample.xml"; |
28 | | - |
29 | | - public static void main(String[] args) throws Exception { |
30 | | - System.out.println("Multi-Tenant shared pool configuration using XML"); |
31 | | - |
32 | | - // Java system property to specify the location of UCP XML configuration |
33 | | - // file which has shared pool, datasource properties defined in it. |
34 | | - System.setProperty("oracle.ucp.jdbc.xmlConfigFile", xmlFileURI); |
35 | | - |
36 | | - // The xml file used in this code example defines a connection pool with |
37 | | - // connection-pool-name -"pool1" and two tenant datasources with |
38 | | - // datasource-name as "tenant1_ds" and "tenant2_ds" which are using this |
39 | | - // shared pool. |
40 | | - |
41 | | - // Get the datasource instance named as "tenant1_ds" in XML config file |
42 | | - PoolDataSource tenant1_DS = PoolDataSourceFactory |
43 | | - .getPoolDataSource("tenant1_ds"); |
44 | | - |
45 | | - // Get a connection using tenant1 datasource |
46 | | - Connection tenant1Conn = tenant1_DS.getConnection(); |
47 | | - |
48 | | - // Run a query on the connection obtained using tenant1 datasource i.e. |
49 | | - // tenant1_ds |
50 | | - runQueryOnTenant1(tenant1Conn); |
51 | | - |
52 | | - // return tenant1 connection to the pool |
53 | | - tenant1Conn.close(); |
54 | | - |
55 | | - // Get the datasource instance named as "tenant2_ds" in XML config file |
56 | | - PoolDataSource tenant2_DS = PoolDataSourceFactory |
57 | | - .getPoolDataSource("tenant2_ds"); |
58 | | - |
59 | | - // Get a connection using tenant2 datasource |
60 | | - Connection tenant2Conn = tenant2_DS.getConnection(); |
61 | | - |
62 | | - // Run a query on the connection obtained using tenant2 datasource i.e. |
63 | | - // tenant2_ds |
64 | | - runQueryOnTenant2(tenant2Conn); |
65 | | - |
66 | | - // return tenant2 connection to the pool |
67 | | - tenant2Conn.close(); |
68 | | - |
69 | | - } |
70 | | - |
71 | | - /** |
72 | | - * Runs a query on the tenant1 table i.e. tenant1_emp to get the employee details |
73 | | - * using the given connection. |
74 | | - */ |
75 | | - private static void runQueryOnTenant1(Connection tenant1Conn) { |
76 | | - try { |
77 | | - String sql = "SELECT empno,ename FROM tenant1_emp"; |
78 | | - Statement st = tenant1Conn.createStatement(); |
79 | | - ResultSet rs = st.executeQuery(sql); |
80 | | - System.out.println("Teant1 Employee Details :"); |
81 | | - while (rs.next()) { |
82 | | - System.out.println("Employee ID = " + rs.getInt("empno") |
83 | | - + " Employee Name = " + rs.getString("ename")); |
84 | | - } |
85 | | - rs.close(); |
86 | | - st.close(); |
87 | | - |
88 | | - } catch (SQLException e) { |
89 | | - e.printStackTrace(); |
90 | | - } |
91 | | - |
92 | | - } |
93 | | - |
94 | | - /** |
95 | | - * Runs a query on the tenant2 table i.e. tenant2_emp to get the employee details |
96 | | - * using the given connection. |
97 | | - */ |
98 | | - private static void runQueryOnTenant2(Connection tenant2Conn) { |
99 | | - try { |
100 | | - String sql = "SELECT empno,ename FROM tenant2_emp"; |
101 | | - Statement st = tenant2Conn.createStatement(); |
102 | | - ResultSet rs = st.executeQuery(sql); |
103 | | - System.out.println("Teant2 Employee Details :"); |
104 | | - while (rs.next()) { |
105 | | - System.out.println("Employee ID = " + rs.getInt("empno") |
106 | | - + " Employee Name = " + rs.getString("ename")); |
107 | | - } |
108 | | - rs.close(); |
109 | | - st.close(); |
110 | | - |
111 | | - } catch (SQLException e) { |
112 | | - e.printStackTrace(); |
113 | | - } |
114 | | - |
115 | | - } |
116 | | - |
117 | | -} |
| 1 | +<?xml version="1.0" encoding="UTF-8"?> |
| 2 | +<ucp-properties> |
| 3 | + <connection-pool |
| 4 | + connection-pool-name="pool1" |
| 5 | +connection-factory-class-name="oracle.jdbc.pool.OracleDataSource" |
| 6 | +url="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=<host_name>)(PORT=<port_number>))(CONNECT_DATA=(SERVICE_NAME=cdb_root_app_service_name)))" |
| 7 | +user="c##common_user" |
| 8 | + password="password" |
| 9 | + initial-pool-size="1" |
| 10 | + min-pool-size="1" |
| 11 | + max-pool-size="1" |
| 12 | + shared="true" > |
| 13 | + |
| 14 | + <connection-property name="oracle.jdbc.ReadTimeout" value="20000"/> |
| 15 | + <connection-property name="oracle.net.OUTBOUND_CONNECT_TIMEOUT" value="20000"/> |
| 16 | + |
| 17 | + <data-source |
| 18 | + data-source-name="tenant1_ds" |
| 19 | + service="pdb1_app_service_name" |
| 20 | + description="tenant1 data source" |
| 21 | + /> |
| 22 | + <data-source data-source-name="tenant2_ds" |
| 23 | + service="pdb2_app_service_name" |
| 24 | + description="tenant2 data source" |
| 25 | + /> |
| 26 | + </connection-pool> |
| 27 | +</ucp-properties> |
0 commit comments