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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions java/jdbc-centralized-config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
29 changes: 29 additions & 0 deletions java/jdbc-centralized-config/example/connect.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"example": {
"connect_descriptor": "<connect_descriptor>",
"user": "admin",
"password": {
"type": "ocivault",
"value": "ocid1.vaultsecret.oc1.phx.<ocivaultocid>"
},
"jdbc": {
"oracle.jdbc.ReadTimeout": 1000,
"defaultRowPrefetch": 20,
"autoCommit": "false"
}
},

"test": {
"connect_descriptor": "<connect_descriptor>",
"user": "testuser",
"password": {
"type": "ocivault",
"value": "ocid1.vaultsecret.oc1.phx.<ocivaultocid>"
},
"jdbc": {
"oracle.jdbc.ReadTimeout": 1000,
"defaultRowPrefetch": 20,
"autoCommit": "false"
}
}
}
58 changes: 58 additions & 0 deletions java/jdbc-centralized-config/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.oracle</groupId>
<artifactId>jdbc-centralized-config</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>2.0.13.</slf4j.version>
</properties>

<dependencies>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11</artifactId>
<version>23.4.0.24.05</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ucp11</artifactId>
<version>23.4.0.24.05</version>
</dependency>
<dependency>
<groupId>com.oracle.database.security</groupId>
<artifactId>oraclepki</artifactId>
<version>23.4.0.24.05</version>
</dependency>
<!-- For centralized config with OCI -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc-provider-oci</artifactId>
<version>1.0.1</version>
</dependency>
<!-- For centralized config with Azure -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc-provider-azure</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
</dependency>

</dependencies>
</project>
47 changes: 47 additions & 0 deletions java/jdbc-centralized-config/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import database.DatabaseConfig;
import database.DatabasePoolConfig;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {

static DatabaseConfig dbConfig;

static class Provider {
static final String OCI_OBJECT_STORE_CONFIG = "jdbc:oracle:thin:@config-ociobject://<url-path>";
static final String BUILT_IN_FILE_CONFIG = "jdbc:oracle:thin:@config-file://<path-to-json-file>";
static final String AZURE_APP_CONFIG = "jdbc:oracle:thin:@config-azure://<appconfig-name>";
static final String OCI_VAULT_CONFIG = "jdbc:oracle:thin:@config-ocivault://<vault-secret-ocid>";
static final String NORMAL_CONFIG = "jdbc:oracle:thin:@<connect-string>";
}

public static void main(String[] args) {
System.setProperty("ORACLE_URL", Provider.BUILT_IN_FILE_CONFIG);
dbConfig = DatabaseConfig.get();
runConnectionTest();

}

// Test method
private static void runConnectionTest() {



boolean success = false;
try (Connection c = dbConfig.getConnection();
PreparedStatement stmt = c.prepareStatement("select 'true' from dual");
ResultSet rs = stmt.executeQuery()){

// Boolean Datatype is a 23ai feature
if (rs.next()) success = rs.getBoolean(1);

} catch (SQLException e) {
e.printStackTrace();
}

System.out.println("Success: " + success);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package database;


import oracle.jdbc.pool.OracleDataSource;
import oracle.ucp.jdbc.PoolDataSource;
import oracle.ucp.jdbc.PoolDataSourceFactory;

import java.sql.Connection;
import java.sql.SQLException;


public class DatabaseConfig {

final OracleDataSource ods;

private static DatabaseConfig config;

DatabaseConfig() {

try {
ods = new OracleDataSource();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

public static synchronized DatabaseConfig get() {
if (config == null ) {
String URL = System.getProperty("ORACLE_URL");
config = new DatabaseConfig()
.configure(URL);
}
return config;
}

private DatabaseConfig configure(String URL) {
ods.setURL(URL);
return this;
}

public Connection getConnection() throws SQLException {
return ods.getConnection();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package database;


import oracle.ucp.jdbc.PoolDataSource;
import oracle.ucp.jdbc.PoolDataSourceFactory;

import java.sql.Connection;
import java.sql.SQLException;


public class DatabasePoolConfig {

final String factoryClassName = "oracle.jdbc.pool.OracleDataSource";
final String poolDataSourceName = "JDBC_UCP_POOL";
final PoolDataSource pool;

private static DatabasePoolConfig config;

DatabasePoolConfig() {
pool = PoolDataSourceFactory.getPoolDataSource();
}

public static synchronized DatabasePoolConfig get() {
if (config == null ) {
String URL = System.getProperty("ORACLE_URL");
config = new DatabasePoolConfig()
.configure(URL);
}
return config;
}

private DatabasePoolConfig configure(String URL) {
System.out.println("Configuring with " + URL);
try {
pool.setURL(URL);
pool.setConnectionPoolName(poolDataSourceName);
pool.setConnectionFactoryClassName(factoryClassName);
pool.setInitialPoolSize(100);
} catch (SQLException e) {
System.out.println("Error setting up oracle.jdbc.pool.OracleDataSource");
throw new RuntimeException(e);
}
return this;
}

public Connection getConnection() throws SQLException {
return pool.getConnection();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.slf4j.simpleLogger.defaultLogLevel=error