Java tutorial
/* * Copyright (C) 2016 Dominion Global * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.dominion.salud.mpr.configuration; import com.dominion.salud.mpr.negocio.configuration.MPRConstantes; import com.mchange.v2.c3p0.ComboPooledDataSource; import java.beans.PropertyVetoException; import javax.naming.NamingException; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySources; import org.springframework.core.env.Environment; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver; import org.springframework.jndi.JndiTemplate; import org.springframework.orm.jpa.JpaDialect; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaDialect; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * Inicializa la conexion JPA con el origen de datos * <br> * * @author jcgonzalez */ @Configuration @EnableTransactionManagement @EnableJpaRepositories(basePackages = "com.dominion.salud.mpr.negocio.repositories") @PropertySources({ @PropertySource(value = "classpath:db.properties") }) public class MPRJpaConfiguration { private static final Logger logger = LoggerFactory.getLogger(MPRJpaConfiguration.class); @Autowired private Environment environment; @Bean @Autowired public JpaTransactionManager transactionManager() { JpaTransactionManager txManager = new JpaTransactionManager(); JpaDialect jpaDialect = new HibernateJpaDialect(); txManager.setEntityManagerFactory(entityManagerFactory()); txManager.setJpaDialect(jpaDialect); return txManager; } @Bean @Autowired public EntityManagerFactory entityManagerFactory() { HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); adapter.setGenerateDdl(Boolean.getBoolean(environment.getRequiredProperty("hibernate.generate_ddl"))); adapter.setShowSql(Boolean.getBoolean(environment.getRequiredProperty("hibernate.show_sql"))); adapter.setDatabasePlatform(environment.getRequiredProperty("hibernate.dialect")); LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setDataSource(dataSource()); factory.setJpaVendorAdapter(adapter); factory.setPackagesToScan("com.dominion.salud.mpr.negocio.entities"); factory.afterPropertiesSet(); factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver()); return factory.getObject(); } @Bean public DataSource dataSource() { if (StringUtils.isNotBlank(environment.getProperty("jdbc.jndi"))) { return JNDIDataSource(); } else { return JDBCDataSource(); } } public DataSource JNDIDataSource() { DataSource dataSource = null; JndiTemplate jndi = new JndiTemplate(); try { dataSource = (DataSource) jndi.lookup(environment.getRequiredProperty("jdbc.jndi")); } catch (NamingException e) { } return dataSource; } public DataSource JDBCDataSource() { ComboPooledDataSource dataSource = new ComboPooledDataSource(); try { dataSource.setDriverClass(environment.getRequiredProperty("jdbc.driverClassName")); dataSource.setJdbcUrl(environment.getRequiredProperty("jdbc.url")); dataSource.setUser(environment.getRequiredProperty("jdbc.username")); dataSource.setPassword(environment.getRequiredProperty("jdbc.password")); //Configuracion Opcional dataSource.setMinPoolSize( StringUtils.isNotBlank(environment.getRequiredProperty("jdbc.minPoolSize")) ? NumberUtils .toInt(environment.getRequiredProperty("jdbc.minPoolSize"), MPRConstantes.MIN_POOL_SIZE) : MPRConstantes.MIN_POOL_SIZE); dataSource.setMaxPoolSize( StringUtils.isNotBlank(environment.getRequiredProperty("jdbc.maxPoolSize")) ? NumberUtils .toInt(environment.getRequiredProperty("jdbc.maxPoolSize"), MPRConstantes.MAX_POOL_SIZE) : MPRConstantes.MAX_POOL_SIZE); dataSource.setCheckoutTimeout( StringUtils.isNotBlank(environment.getRequiredProperty("jdbc.checkoutTimeout")) ? NumberUtils.toInt(environment.getRequiredProperty("jdbc.checkoutTimeout"), MPRConstantes.CHECKOUT_TIMEOUT) : MPRConstantes.CHECKOUT_TIMEOUT); dataSource.setIdleConnectionTestPeriod( StringUtils.isNotBlank(environment.getRequiredProperty("jdbc.idleConnectionTestPeriod")) ? NumberUtils.toInt(environment.getRequiredProperty("jdbc.idleConnectionTestPeriod"), MPRConstantes.IDLE_CONNECTION_TEST_PERIOD) : MPRConstantes.IDLE_CONNECTION_TEST_PERIOD); dataSource.setPreferredTestQuery( StringUtils.isNotBlank(environment.getRequiredProperty("jdbc.preferredTestQuery")) ? environment.getRequiredProperty("jdbc.preferredTestQuery") : MPRConstantes.PREFERRED_TEST_QUERY); //Autoconfiguracion dataSource.setAcquireIncrement(1); dataSource.setInitialPoolSize(3); dataSource.setIdleConnectionTestPeriod(900); dataSource.setMaxIdleTimeExcessConnections(1800); dataSource.setTestConnectionOnCheckin(true); } catch (IllegalStateException | PropertyVetoException e) { } return dataSource; } }