Java tutorial
/** * Copyright 2016 REPLACE ME OWNER (REPLACE ME YEAR) * * 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 org.homiefund.config; import com.mchange.v2.c3p0.ComboPooledDataSource; import net.ttddyy.dsproxy.listener.CommonsLogLevel; import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Description; 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 22.9.2016. */ @Configuration public class DatasourceConfiguration implements EnvironmentAware { private Environment environment; @Override public void setEnvironment(Environment environment) { this.environment = environment; } // @Bean(name="dataSource") public DataSource dataSource() throws SQLException { return ProxyDataSourceBuilder.create(c3p0dataSource()).logQueryByCommons(CommonsLogLevel.INFO).countQuery() .build(); } @Bean(name = "dataSource") @Description("ComboPooledDataSource responsible for database connection.") public DataSource c3p0dataSource() throws SQLException { ComboPooledDataSource ds = new ComboPooledDataSource(); try { ds.setDriverClass(environment.getProperty("jdbc.driver_class")); } catch (PropertyVetoException e) { throw new SQLException(e); } ds.setJdbcUrl(environment.getProperty("jdbc.url")); ds.setUser(environment.getProperty("jdbc.username")); ds.setPassword(environment.getProperty("jdbc.password")); ds.setPreferredTestQuery(environment.getProperty("jdbc.test_query")); ds.setInitialPoolSize(environment.getProperty("jdbc.poolsize.init", Integer.class)); ds.setMinPoolSize(environment.getProperty("jdbc.poolsize.min", Integer.class)); ds.setMaxPoolSize(environment.getProperty("jdbc.poolsize.max", Integer.class)); ds.setCheckoutTimeout(environment.getProperty("jdbc.timeout", Integer.class)); ds.setMaxStatements(environment.getProperty("jdbc.max_statements", Integer.class)); ds.setIdleConnectionTestPeriod(environment.getProperty("jdbc.idle_test_period", Integer.class)); return ds; } }