Skip to content

Commit

Permalink
[#noissue] Add newDriver to JDBCDriverClass
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Nov 21, 2024
1 parent dcb999b commit 692bbf5
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void setup(
this.jdbcApi = jdbcApi;

try {
Driver driver = jdbcDriverClass.getDriver().getConstructor().newInstance();
Driver driver = jdbcDriverClass.newDriver();
DriverManager.registerDriver(driver);
} catch (Exception e) {
throw new RuntimeException("driver register error", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static void beforeAll() {
public void registerDriver() throws Exception {
driverProperties = DatabaseContainers.readSystemProperties();
JDBCDriverClass driverClass = getJDBCDriverClass();
Driver driver = driverClass.getDriver().newInstance();
Driver driver = driverClass.newDriver();
DriverManager.registerDriver(driver);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@
*/
@PluginTest
@PinpointAgent(AgentPath.PATH)
@Dependency({"org.apache.ibatis:ibatis-sqlmap:[2.3.4.726]","com.ibm.informix:jdbc:[4.10.10.0]", PluginITConstants.VERSION, JDBCTestConstants.VERSION})
@SharedDependency({"com.ibm.informix:jdbc:4.10.10.0", TestcontainersOption.TEST_CONTAINER, PluginITConstants.VERSION, JDBCTestConstants.VERSION})
@Dependency({"org.apache.ibatis:ibatis-sqlmap:[2.3.4.726]", "com.ibm.informix:jdbc:[4.10.10.0]",
PluginITConstants.VERSION, JDBCTestConstants.VERSION})
@SharedDependency({"com.ibm.informix:jdbc:4.10.10.0", TestcontainersOption.TEST_CONTAINER,
PluginITConstants.VERSION, JDBCTestConstants.VERSION})
@SharedTestLifeCycleClass(InformixServer.class)
@PinpointConfig("pinpoint-informix.config")
public class InformixConnectionIT {
Expand All @@ -70,7 +72,7 @@ public static void beforeAll() throws Exception {

@BeforeEach
public void before() throws Exception {
Driver driver = driverClass.getDriver().newInstance();
Driver driver = driverClass.newDriver();
DriverManager.registerDriver(driver);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.navercorp.pinpoint.it.plugin.jdbc.jtds;

import org.junit.jupiter.api.Test;

import java.sql.Driver;
import java.sql.SQLException;

class JtdsJDBCDriverClassTest {
@Test
void name() throws SQLException {
JtdsJDBCDriverClass driverClass = new JtdsJDBCDriverClass();
Driver driver = driverClass.newDriver();
System.out.println(driver);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static void afterClass() throws Exception {

@BeforeEach
public void registerDriver() throws Exception {
Driver driver = driverClass.getDriver().newInstance();
Driver driver = driverClass.newDriver();
DriverManager.registerDriver(driver);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class MySqlItHelper {

void testStatements(JDBCApi jdbcApi) throws Exception {

Class<Driver> driverClass = jdbcApi.getJDBCDriverClass().getDriver();
Driver driverClass = jdbcApi.getJDBCDriverClass().newDriver();
final Connection conn = connect(driverClass);

conn.setAutoCommit(false);
Expand Down Expand Up @@ -138,7 +138,7 @@ void testStatements(JDBCApi jdbcApi) throws Exception {
verifier.verifyTrace(event(MYSQL, commit, null, databaseAddress, databaseName));
}

private Connection connect(Class<Driver> driverClass) throws Exception {
private Connection connect(Driver driverClass) throws Exception {
return DriverManager.getConnection(jdbcUrl, databaseId, databasePassword);
}

Expand All @@ -151,7 +151,7 @@ void testStoredProcedure_with_IN_OUT_parameters(JDBCApi jdbcApi) throws Exceptio
final String param2 = "b";
final String storedProcedureQuery = "{ call concatCharacters(?, ?, ?) }";

final Class<Driver> driverClass = jdbcApi.getJDBCDriverClass().getDriver();
final Driver driverClass = jdbcApi.getJDBCDriverClass().newDriver();
final Connection conn = connect(driverClass);

CallableStatement cs = conn.prepareCall(storedProcedureQuery);
Expand Down Expand Up @@ -202,7 +202,7 @@ void testStoredProcedure_with_INOUT_parameters(JDBCApi jdbcApi) throws Exception
final int param2 = 2;
final String storedProcedureQuery = "{ call swapAndGetSum(?, ?) }";

final Class<Driver> driverClass = jdbcApi.getJDBCDriverClass().getDriver();
final Driver driverClass = jdbcApi.getJDBCDriverClass().newDriver();
final Connection conn = connect(driverClass);

CallableStatement cs = conn.prepareCall(storedProcedureQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Connection getConnection(DriverProperties driverProperties) throws SQLExc
@BeforeEach
public void before() throws Exception {
JDBCDriverClass driverClass = getJDBCDriverClass();
Driver driver = driverClass.getDriver().newInstance();
Driver driver = driverClass.newDriver();
DriverManager.registerDriver(driver);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ public class OracleItHelper {
DB_PASSWORD = driverProperties.getPassword();
}

private Connection connect(Class<Driver> driverClass) throws SQLException {
private Connection connect(Driver driverClass) throws SQLException {
logger.info("Connecting to Oracle Database url: {}, id: {}, pw: {}", JDBC_URL, DB_ID, DB_PASSWORD);
return DriverManager.getConnection(JDBC_URL, DB_ID, DB_PASSWORD);
}

public void create(JDBCApi jdbcApi) throws Exception {
Class<Driver> driverClass = jdbcApi.getJDBCDriverClass().getDriver();
Driver driverClass = jdbcApi.getJDBCDriverClass().newDriver();
final Connection conn = connect(driverClass);

conn.setAutoCommit(false);
Expand All @@ -90,7 +90,7 @@ public void create(JDBCApi jdbcApi) throws Exception {
}

public void testStatement(JDBCApi jdbcApi, String insertQuery, String selectQuery, String deleteQuery) throws Exception {
Class<Driver> driverClass = jdbcApi.getJDBCDriverClass().getDriver();
Driver driverClass = jdbcApi.getJDBCDriverClass().newDriver();
final Connection conn = connect(driverClass);

conn.setAutoCommit(false);
Expand Down Expand Up @@ -189,7 +189,7 @@ CREATE OR REPLACE PROCEDURE concatCharacters(a IN VARCHAR2, b IN VARCHAR2, c OUT
END concatCharacters;
*/
public void testStoredProcedure_with_IN_OUT_parameters(JDBCApi jdbcApi, String param1, String param2, String storedProcedureQuery) throws Exception {
Class<Driver> driverClass = jdbcApi.getJDBCDriverClass().getDriver();
Driver driverClass = jdbcApi.getJDBCDriverClass().newDriver();
final Connection conn = connect(driverClass);

CallableStatement cs = conn.prepareCall(storedProcedureQuery);
Expand Down Expand Up @@ -271,7 +271,7 @@ CREATE OR REPLACE PROCEDURE swapAndGetSum(a IN OUT NUMBER, b IN OUT NUMBER, c OU
END swapAndGetSum;
*/
public void testStoredProcedure_with_INOUT_parameters(JDBCApi jdbcApi, int param1, int param2, String storedProcedureQuery) throws Exception {
Class<Driver> driverClass = jdbcApi.getJDBCDriverClass().getDriver();
Driver driverClass = jdbcApi.getJDBCDriverClass().newDriver();
final Connection conn = connect(driverClass);

CallableStatement cs = conn.prepareCall(storedProcedureQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected static DriverProperties createDriverProperties() {

@BeforeEach
public void registerDriver() throws Exception {
Driver driver = driverClass.getDriver().newInstance();
Driver driver = driverClass.newDriver();
DriverManager.registerDriver(driver);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ public class OracleItHelper {
DB_PASSWORD = driverProperties.getPassword();
}

private Connection connect(Class<Driver> driverClass) throws SQLException {
private Connection connect(Driver driverClass) throws SQLException {
logger.info("Connecting to Oracle Database url: {}, id: {}, pw: {}", JDBC_URL, DB_ID, DB_PASSWORD);
return DriverManager.getConnection(JDBC_URL, DB_ID, DB_PASSWORD);
}

public void testStatement(JDBCApi jdbcApi, String insertQuery, String selectQuery, String deleteQuery) throws Exception {
Class<Driver> driverClass = jdbcApi.getJDBCDriverClass().getDriver();
Driver driverClass = jdbcApi.getJDBCDriverClass().newDriver();
final Connection conn = connect(driverClass);

conn.setAutoCommit(false);
Expand Down Expand Up @@ -172,7 +172,7 @@ CREATE OR REPLACE PROCEDURE concatCharacters(a IN VARCHAR2, b IN VARCHAR2, c OUT
END concatCharacters;
*/
public void testStoredProcedure_with_IN_OUT_parameters(JDBCApi jdbcApi, String param1, String param2, String storedProcedureQuery) throws Exception {
Class<Driver> driverClass = jdbcApi.getJDBCDriverClass().getDriver();
Driver driverClass = jdbcApi.getJDBCDriverClass().newDriver();
final Connection conn = connect(driverClass);

CallableStatement cs = conn.prepareCall(storedProcedureQuery);
Expand Down Expand Up @@ -254,7 +254,7 @@ CREATE OR REPLACE PROCEDURE swapAndGetSum(a IN OUT NUMBER, b IN OUT NUMBER, c OU
END swapAndGetSum;
*/
public void testStoredProcedure_with_INOUT_parameters(JDBCApi jdbcApi, int param1, int param2, String storedProcedureQuery) throws Exception {
Class<Driver> driverClass = jdbcApi.getJDBCDriverClass().getDriver();
Driver driverClass = jdbcApi.getJDBCDriverClass().newDriver();
final Connection conn = connect(driverClass);

CallableStatement cs = conn.prepareCall(storedProcedureQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected static DriverProperties createDriverProperties() {

@BeforeEach
public void registerDriver() throws Exception {
Driver driver = driverClass.getDriver().getConstructor().newInstance();
Driver driver = driverClass.newDriver();
DriverManager.registerDriver(driver);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static void afterClass() {

@BeforeEach
public void before() throws Exception {
Driver driver = driverClass.getDriver().newInstance();
Driver driver = driverClass.newDriver();
DriverManager.registerDriver(driver);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static DriverProperties getDriverProperties() {

@BeforeEach
public void registerDriver() throws Exception {
Driver driver = getJDBCDriverClass().getDriver().newInstance();
Driver driver = getJDBCDriverClass().newDriver();
DriverManager.registerDriver(driver);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void setup(String dbType, String executeQuery, DriverProperties driverPro

try {
JDBCDriverClass jdbcDriverClass = getJDBCDriverClass();
Driver driver = jdbcDriverClass.getDriver().getConstructor().newInstance();
Driver driver = jdbcDriverClass.newDriver();
DriverManager.registerDriver(driver);
} catch (Exception e) {
throw new RuntimeException("driver register error", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.sql.Connection;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

/**
Expand All @@ -28,6 +29,15 @@
public interface JDBCDriverClass {
Class<Driver> getDriver();

default Driver newDriver() throws SQLException {
final Class<Driver> driver = getDriver();
try {
return driver.getDeclaredConstructor().newInstance();
} catch (Throwable th) {
throw new SQLException(driver.getName() + " Driver create failed", th);
}
}

Class<Connection> getConnection();

Class<Statement> getStatement();
Expand Down

0 comments on commit 692bbf5

Please sign in to comment.