Skip to content

Commit 3e0a59e

Browse files
authored
Added JDBC Centralized config code sample (#368)
1 parent 356b78a commit 3e0a59e

File tree

7 files changed

+268
-0
lines changed

7 files changed

+268
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"example": {
3+
"connect_descriptor": "<connect_descriptor>",
4+
"user": "admin",
5+
"password": {
6+
"type": "ocivault",
7+
"value": "ocid1.vaultsecret.oc1.phx.<ocivaultocid>"
8+
},
9+
"jdbc": {
10+
"oracle.jdbc.ReadTimeout": 1000,
11+
"defaultRowPrefetch": 20,
12+
"autoCommit": "false"
13+
}
14+
},
15+
16+
"test": {
17+
"connect_descriptor": "<connect_descriptor>",
18+
"user": "testuser",
19+
"password": {
20+
"type": "ocivault",
21+
"value": "ocid1.vaultsecret.oc1.phx.<ocivaultocid>"
22+
},
23+
"jdbc": {
24+
"oracle.jdbc.ReadTimeout": 1000,
25+
"defaultRowPrefetch": 20,
26+
"autoCommit": "false"
27+
}
28+
}
29+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.oracle</groupId>
8+
<artifactId>jdbc-centralized-config</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>17</maven.compiler.source>
13+
<maven.compiler.target>17</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<slf4j.version>2.0.13.</slf4j.version>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>com.oracle.database.jdbc</groupId>
21+
<artifactId>ojdbc11</artifactId>
22+
<version>23.4.0.24.05</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.oracle.database.jdbc</groupId>
26+
<artifactId>ucp11</artifactId>
27+
<version>23.4.0.24.05</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.oracle.database.security</groupId>
31+
<artifactId>oraclepki</artifactId>
32+
<version>23.4.0.24.05</version>
33+
</dependency>
34+
<!-- For centralized config with OCI -->
35+
<dependency>
36+
<groupId>com.oracle.database.jdbc</groupId>
37+
<artifactId>ojdbc-provider-oci</artifactId>
38+
<version>1.0.1</version>
39+
</dependency>
40+
<!-- For centralized config with Azure -->
41+
<dependency>
42+
<groupId>com.oracle.database.jdbc</groupId>
43+
<artifactId>ojdbc-provider-azure</artifactId>
44+
<version>1.0.1</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.slf4j</groupId>
48+
<artifactId>slf4j-api</artifactId>
49+
<version>1.7.36</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.slf4j</groupId>
53+
<artifactId>slf4j-simple</artifactId>
54+
<version>1.7.36</version>
55+
</dependency>
56+
57+
</dependencies>
58+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import database.DatabaseConfig;
2+
import database.DatabasePoolConfig;
3+
4+
import java.sql.Connection;
5+
import java.sql.PreparedStatement;
6+
import java.sql.ResultSet;
7+
import java.sql.SQLException;
8+
9+
public class Main {
10+
11+
static DatabaseConfig dbConfig;
12+
13+
static class Provider {
14+
static final String OCI_OBJECT_STORE_CONFIG = "jdbc:oracle:thin:@config-ociobject://<url-path>";
15+
static final String BUILT_IN_FILE_CONFIG = "jdbc:oracle:thin:@config-file://<path-to-json-file>";
16+
static final String AZURE_APP_CONFIG = "jdbc:oracle:thin:@config-azure://<appconfig-name>";
17+
static final String OCI_VAULT_CONFIG = "jdbc:oracle:thin:@config-ocivault://<vault-secret-ocid>";
18+
static final String NORMAL_CONFIG = "jdbc:oracle:thin:@<connect-string>";
19+
}
20+
21+
public static void main(String[] args) {
22+
System.setProperty("ORACLE_URL", Provider.BUILT_IN_FILE_CONFIG);
23+
dbConfig = DatabaseConfig.get();
24+
runConnectionTest();
25+
26+
}
27+
28+
// Test method
29+
private static void runConnectionTest() {
30+
31+
32+
33+
boolean success = false;
34+
try (Connection c = dbConfig.getConnection();
35+
PreparedStatement stmt = c.prepareStatement("select 'true' from dual");
36+
ResultSet rs = stmt.executeQuery()){
37+
38+
// Boolean Datatype is a 23ai feature
39+
if (rs.next()) success = rs.getBoolean(1);
40+
41+
} catch (SQLException e) {
42+
e.printStackTrace();
43+
}
44+
45+
System.out.println("Success: " + success);
46+
}
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package database;
2+
3+
4+
import oracle.jdbc.pool.OracleDataSource;
5+
import oracle.ucp.jdbc.PoolDataSource;
6+
import oracle.ucp.jdbc.PoolDataSourceFactory;
7+
8+
import java.sql.Connection;
9+
import java.sql.SQLException;
10+
11+
12+
public class DatabaseConfig {
13+
14+
final OracleDataSource ods;
15+
16+
private static DatabaseConfig config;
17+
18+
DatabaseConfig() {
19+
20+
try {
21+
ods = new OracleDataSource();
22+
} catch (SQLException e) {
23+
throw new RuntimeException(e);
24+
}
25+
}
26+
27+
public static synchronized DatabaseConfig get() {
28+
if (config == null ) {
29+
String URL = System.getProperty("ORACLE_URL");
30+
config = new DatabaseConfig()
31+
.configure(URL);
32+
}
33+
return config;
34+
}
35+
36+
private DatabaseConfig configure(String URL) {
37+
ods.setURL(URL);
38+
return this;
39+
}
40+
41+
public Connection getConnection() throws SQLException {
42+
return ods.getConnection();
43+
}
44+
45+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package database;
2+
3+
4+
import oracle.ucp.jdbc.PoolDataSource;
5+
import oracle.ucp.jdbc.PoolDataSourceFactory;
6+
7+
import java.sql.Connection;
8+
import java.sql.SQLException;
9+
10+
11+
public class DatabasePoolConfig {
12+
13+
final String factoryClassName = "oracle.jdbc.pool.OracleDataSource";
14+
final String poolDataSourceName = "JDBC_UCP_POOL";
15+
final PoolDataSource pool;
16+
17+
private static DatabasePoolConfig config;
18+
19+
DatabasePoolConfig() {
20+
pool = PoolDataSourceFactory.getPoolDataSource();
21+
}
22+
23+
public static synchronized DatabasePoolConfig get() {
24+
if (config == null ) {
25+
String URL = System.getProperty("ORACLE_URL");
26+
config = new DatabasePoolConfig()
27+
.configure(URL);
28+
}
29+
return config;
30+
}
31+
32+
private DatabasePoolConfig configure(String URL) {
33+
System.out.println("Configuring with " + URL);
34+
try {
35+
pool.setURL(URL);
36+
pool.setConnectionPoolName(poolDataSourceName);
37+
pool.setConnectionFactoryClassName(factoryClassName);
38+
pool.setInitialPoolSize(100);
39+
} catch (SQLException e) {
40+
System.out.println("Error setting up oracle.jdbc.pool.OracleDataSource");
41+
throw new RuntimeException(e);
42+
}
43+
return this;
44+
}
45+
46+
public Connection getConnection() throws SQLException {
47+
return pool.getConnection();
48+
}
49+
50+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.slf4j.simpleLogger.defaultLogLevel=error

0 commit comments

Comments
 (0)