cz.muni.fi.editor.database.helpers.DataSourceConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.editor.database.helpers.DataSourceConfiguration.java

Source

/*
*Copyright  2016 Dominik Szalai (emptulik@gmail.com)
*
*Licensed 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.
*/
/*
 * Copyright  2016 Dominik Szalai (emptulik@gmail.com)
 *
 * Licensed 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 cz.muni.fi.editor.database.helpers;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import net.ttddyy.dsproxy.listener.CommonsLogLevel;
import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.SQLException;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 8.8.2016.
 */
@Configuration
public class DataSourceConfiguration {
    @Autowired
    protected Environment env;

    @Bean(name = "dataSource")
    public DataSource c3p0DataSource() throws SQLException {
        ComboPooledDataSource ds = new ComboPooledDataSource();
        try {
            ds.setDriverClass(env.getProperty("database.jdbc.driver_class"));
        } catch (PropertyVetoException e) {
            throw new SQLException(e);
        }
        ds.setJdbcUrl(env.getProperty("database.jdbc.url"));
        ds.setUser(env.getProperty("database.jdbc.username"));
        ds.setPassword(env.getProperty("database.jdbc.password"));
        ds.setPreferredTestQuery(env.getProperty("database.jdbc.test_query"));
        ds.setInitialPoolSize(env.getProperty("database.jdbc.poolsize.init", Integer.class));
        ds.setMinPoolSize(env.getProperty("database.jdbc.poolsize.min", Integer.class));
        ds.setMaxPoolSize(env.getProperty("database.jdbc.poolsize.max", Integer.class));
        ds.setCheckoutTimeout(env.getProperty("database.jdbc.timeout", Integer.class));
        ds.setMaxStatements(env.getProperty("database.jdbc.max_statements", Integer.class));
        ds.setIdleConnectionTestPeriod(env.getProperty("database.jdbc.idle_test_period", Integer.class));

        return ds;
    }

    //        @Bean(name = "dataSource")
    public DataSource dataSource() throws SQLException {
        return ProxyDataSourceBuilder.create(c3p0DataSource()).logQueryByCommons(CommonsLogLevel.INFO).countQuery()
                .build();

    }
}