programing

연결 콘솔에 정보 풀링 인쇄

oldcodes 2023. 9. 26. 22:32
반응형

연결 콘솔에 정보 풀링 인쇄

com.mchange.v2.c3p0을 사용하고 있습니다.독립 실행형 자바 데이터베이스 로더 응용 프로그램에서 "ComboPooledDataSource".데이터 연결을 초기화하면 콘솔 창에 연결 정보를 일식으로 출력합니다.데이터베이스 사용자 이름 및 비밀번호 관련 정보도 포함되어 있습니다.

사용하는 db는 MariaDB입니다.

"Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@b1a58a3            [connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@7a4f0f29"

이 메시지를 콘솔에 표시하려면 어떻게 해야 합니까?이 코드 줄 다음에 내용이 콘솔로 인쇄됩니다.

myConn = DataSource.getInstance().getConnection();

구현:

public class DataSource {

    private static DataSource datasource;
    private ComboPooledDataSource cpds;
    Properties prop = new Properties();
    InputStream input = null;

    private DataSource() throws IOException, SQLException, PropertyVetoException {

            input = new FileInputStream("config.properties");
            prop.load(input);

            cpds = new ComboPooledDataSource();
            cpds.setDriverClass(prop.getProperty("driverclass")); //loads the jdbc driver
            cpds.setJdbcUrl(prop.getProperty("database"));
            cpds.setUser(prop.getProperty("dbuser"));
            cpds.setPassword(prop.getProperty("dbpassword"));
            cpds.setMinPoolSize(5);
            cpds.setAcquireIncrement(5);
            cpds.setMaxPoolSize(20);
            cpds.setMaxStatements(180);

    }

    public static DataSource getInstance() throws IOException, SQLException, PropertyVetoException {
        if (datasource == null) {
            datasource = new DataSource();
            return datasource;
        } else {
            return datasource;
        }
    }

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

}

데이터베이스 이름과 crdentials 세부 정보를 저장하는 config.properties 파일을 읽었습니다.

메인 클래스의 접속 풀을 이렇게 불러봅니다.

myConn = DataSource.getInstance().getConnection();
myConn.setAutoCommit(false);

이 행이 실행된 후 곧 콘솔에 데이터베이스 이름, 사용자 이름 및 암호 세부 정보를 포함한 연결 풀에 대한 세부 정보를 언급하는 항목이 나타납니다.저는 log4j를 연결 풀링에 사용하는 것이 아니라 애플리케이션 레벨 로깅에만 사용하고 있습니다.

언급URL : https://stackoverflow.com/questions/33360589/connection-pooling-printing-information-to-console

반응형