brettwooldridge / HikariCP

光 HikariCP・A solid, high-performance, JDBC connection pool at last.
Apache License 2.0
19.98k stars 2.93k forks source link

Reg Tomcat HikariCP Datasource(Oracle connectionProperties) help. #1244

Open sekarmg opened 6 years ago

sekarmg commented 6 years ago

Environment HikariCP version: 3.2.0 JDK version : 1.8.0_111 Database : Oracle 12g Driver version : ojdbc7 Type4 dataSourceClassName="oracle.jdbc.pool.OracleDataSource"

To start with, i love hikariCP. I love the idealogy of simple is better.

Currently i have configured the connection pool in tomcat container as below. I am not able to pass in connectionProperties to the OracleDataSource. The other solution that was described tells us to create the OracleDataSource object and set it in the HikariConfig.SetDataSource. Are there any other solution available to pass in as property? <Resource name="jdbc/db" auth="Container" factory="com.zaxxer.hikari.HikariJNDIFactory" type="javax.sql.DataSource" poolName="reports" minimumIdle="10" maximumPoolSize="50" connectionTimeout="30000" dataSourceClassName="oracle.jdbc.pool.OracleDataSource" connectionProperties="defaultRowPrefetch=1000" dataSource.user="${ds.user}" dataSource.password="${ds.password}" dataSource.url="${ds.url}"/> `` OracleDataSource oracleDs = new OracleDataSource(); Properties connectionProperties = new Properties(); connectionProperties.put("defaultRowPrefetch", "10000"); oracleDs.setConnectionProperties(connectionProperties); oracleDs.setUser(user); oracleDs.setPassword(password); oracleDs.setUser(connectionString);

HikariConfig conf = new HikariConfig("reports.properties"); conf.setDataSource(oracleDs); HikariDataSource ds = new HikariDataSource(conf); return ds; ``

ljluestc commented 1 week ago

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import oracle.jdbc.pool.OracleDataSource;

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

public class HikariCPExample {

    public static HikariDataSource getHikariDataSource() throws SQLException {
        // Create OracleDataSource and set connection properties
        OracleDataSource oracleDs = new OracleDataSource();

        // Set connection properties
        Properties connectionProperties = new Properties();
        connectionProperties.put("defaultRowPrefetch", "1000"); // Adjust the value as needed
        oracleDs.setConnectionProperties(connectionProperties);

        // Set other connection parameters
        oracleDs.setUser("your_username");
        oracleDs.setPassword("your_password");
        oracleDs.setURL("jdbc:oracle:thin:@//localhost:1521/ORCL");

        // Create HikariConfig and set the OracleDataSource
        HikariConfig config = new HikariConfig();
        config.setDataSource(oracleDs); // Set the OracleDataSource

        // Configure HikariCP settings
        config.setMinimumIdle(10);
        config.setMaximumPoolSize(50);
        config.setConnectionTimeout(30000);

        // Create HikariDataSource
        HikariDataSource hikariDataSource = new HikariDataSource(config);

        return hikariDataSource;
    }

    public static void main(String[] args) throws SQLException {
        HikariDataSource dataSource = getHikariDataSource();

        // Test the connection
        try (Connection connection = dataSource.getConnection()) {
            System.out.println("Connected to the database!");
        }
    }
}