Guest User

Untitled

a guest
Dec 6th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. package com.sharykin.anton.configs;
  2.  
  3.  
  4.  
  5. import com.mchange.v2.c3p0.ComboPooledDataSource;
  6. import org.hibernate.jpa.HibernatePersistenceProvider;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.context.annotation.PropertySource;
  11. import org.springframework.core.env.Environment;
  12. import org.springframework.orm.jpa.JpaTransactionManager;
  13. import org.springframework.orm.jpa.JpaVendorAdapter;
  14. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  15. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  16. import org.springframework.transaction.PlatformTransactionManager;
  17. import org.springframework.transaction.annotation.EnableTransactionManagement;
  18.  
  19.  
  20. import javax.persistence.EntityManagerFactory;
  21. import javax.sql.DataSource;
  22. import javax.transaction.TransactionManager;
  23. import java.beans.PropertyVetoException;
  24. import java.io.IOException;
  25. import java.sql.SQLException;
  26. import java.util.Properties;
  27.  
  28. @Configuration
  29. @EnableTransactionManagement
  30. //@PropertySource(value = {"classpath:database.properties"})
  31. public class DBConfig {
  32. @Autowired
  33. private Environment env;
  34.  
  35. @Bean
  36. public ComboPooledDataSource getDataSource() throws IOException,SQLException,PropertyVetoException{
  37. ComboPooledDataSource dataSource = new ComboPooledDataSource();
  38. dataSource.setDriverClass("com.mysql.jdbc.Driver");
  39. dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/bestfriendsdb");
  40. dataSource.setUser("root");
  41. dataSource.setPassword("1909java1950");
  42. dataSource.setMinPoolSize(5);
  43. dataSource.setMaxPoolSize(30);
  44. dataSource.setMaxStatements(50);
  45. //dataSource.setIdleConnectionTestPeriod(60);
  46. dataSource.setAcquireIncrement(1);
  47. return dataSource;
  48. }
  49.  
  50. @Bean
  51. public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws
  52. IOException,SQLException,PropertyVetoException{
  53. LocalContainerEntityManagerFactoryBean lemf = new LocalContainerEntityManagerFactoryBean();
  54. lemf.setDataSource(getDataSource());
  55. lemf.setPersistenceProviderClass(HibernatePersistenceProvider.class);
  56. lemf.setPackagesToScan("com.sharykin.anton");
  57. lemf.setJpaVendorAdapter(jpaVendorAdapter());
  58. lemf.setJpaProperties(jpaProperties());
  59.  
  60. return lemf;
  61. }
  62.  
  63. @Bean
  64. public JpaVendorAdapter jpaVendorAdapter(){
  65. HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
  66. return hibernateJpaVendorAdapter;
  67. }
  68.  
  69. @Bean
  70. @Autowired
  71. public JpaTransactionManager transactionManager(EntityManagerFactory emf){
  72. JpaTransactionManager txManager= new JpaTransactionManager();
  73. txManager.setEntityManagerFactory(emf);
  74. return txManager;
  75. }
  76.  
  77. private Properties jpaProperties() {
  78. Properties properties = new Properties();
  79. properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
  80.  
  81. properties.put("hibernate.show_sql", "true");
  82.  
  83. return properties;
  84. }
  85.  
  86.  
  87. }
Add Comment
Please, Sign In to add comment