All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arakelian.jdbc.utils.DataSourceUtils Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arakelian.jdbc.utils;

import javax.sql.DataSource;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

public class DataSourceUtils {
    /**
     * Create data source
     *
     * @param url
     *            JDBC connect string
     * @param username
     *            database user
     * @param password
     *            database password
     * @param perUserPooling
     *            true if using per-user pooling
     * @return a DataSource
     */
    public static DataSource createPooledDataSource(
            final String url,
            final String username,
            final String password,
            final boolean perUserPooling) {
        final HikariConfig config = new HikariConfig();
        config.setJdbcUrl(url);
        config.setUsername(username);
        config.setPassword(password);
        config.addDataSourceProperty("cachePrepStmts", "true");
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
        config.setInitializationFailTimeout(0);
        final HikariDataSource ds = new HikariDataSource(config);
        return ds;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy