DEV Community

iron_man
iron_man

Posted on

OracleRuntimeConnection

package com.hardening;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.springframework.stereotype.Component;

@Component
public class OracleRuntimeConnection {

private final TnsResolver tnsResolver;

public OracleRuntimeConnection(TnsResolver tnsResolver) {
    this.tnsResolver = tnsResolver;
}

public Connection open(String tnsAlias, String targetSchema)
        throws SQLException {

    if (tnsAlias == null || tnsAlias.isBlank()) {
        throw new IllegalArgumentException("Select a TNS alias.");
    }

    if (targetSchema == null || targetSchema.isBlank()) {
        throw new IllegalArgumentException("Target schema is required.");
    }

    String schema = targetSchema.trim().toUpperCase();
    String descriptor = tnsResolver.resolve(tnsAlias);

    Properties properties = new Properties();
    properties.put("user", schema);
    properties.put("password", schema);
    properties.put("oracle.net.CONNECT_TIMEOUT", "10000");
    properties.put("oracle.jdbc.ReadTimeout", "30000");

    return DriverManager.getConnection(
            "jdbc:oracle:thin:@" + descriptor,
            properties
    );
}
Enter fullscreen mode Exit fullscreen mode

}

Top comments (0)