Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sharykin.anton.configs;
- import com.mchange.v2.c3p0.ComboPooledDataSource;
- import org.hibernate.jpa.HibernatePersistenceProvider;
- 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.core.env.Environment;
- import org.springframework.orm.jpa.JpaTransactionManager;
- import org.springframework.orm.jpa.JpaVendorAdapter;
- import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
- import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
- import org.springframework.transaction.PlatformTransactionManager;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- import javax.persistence.EntityManagerFactory;
- import javax.sql.DataSource;
- import javax.transaction.TransactionManager;
- import java.beans.PropertyVetoException;
- import java.io.IOException;
- import java.sql.SQLException;
- import java.util.Properties;
- @Configuration
- @EnableTransactionManagement
- //@PropertySource(value = {"classpath:database.properties"})
- public class DBConfig {
- @Autowired
- private Environment env;
- @Bean
- public ComboPooledDataSource getDataSource() throws IOException,SQLException,PropertyVetoException{
- ComboPooledDataSource dataSource = new ComboPooledDataSource();
- dataSource.setDriverClass("com.mysql.jdbc.Driver");
- dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/bestfriendsdb");
- dataSource.setUser("root");
- dataSource.setPassword("1909java1950");
- dataSource.setMinPoolSize(5);
- dataSource.setMaxPoolSize(30);
- dataSource.setMaxStatements(50);
- //dataSource.setIdleConnectionTestPeriod(60);
- dataSource.setAcquireIncrement(1);
- return dataSource;
- }
- @Bean
- public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws
- IOException,SQLException,PropertyVetoException{
- LocalContainerEntityManagerFactoryBean lemf = new LocalContainerEntityManagerFactoryBean();
- lemf.setDataSource(getDataSource());
- lemf.setPersistenceProviderClass(HibernatePersistenceProvider.class);
- lemf.setPackagesToScan("com.sharykin.anton");
- lemf.setJpaVendorAdapter(jpaVendorAdapter());
- lemf.setJpaProperties(jpaProperties());
- return lemf;
- }
- @Bean
- public JpaVendorAdapter jpaVendorAdapter(){
- HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
- return hibernateJpaVendorAdapter;
- }
- @Bean
- @Autowired
- public JpaTransactionManager transactionManager(EntityManagerFactory emf){
- JpaTransactionManager txManager= new JpaTransactionManager();
- txManager.setEntityManagerFactory(emf);
- return txManager;
- }
- private Properties jpaProperties() {
- Properties properties = new Properties();
- properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
- properties.put("hibernate.show_sql", "true");
- return properties;
- }
- }
Add Comment
Please, Sign In to add comment