Guest User

Untitled

a guest
Nov 12th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. @Configuration
  2. @EnableJpaRepositories("com.app.dao.repository")
  3. @EnableTransactionManagement
  4. public class DataAccessConfig {
  5. private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
  6. private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
  7. private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
  8. private static final String PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO = "hibernate.hbm2ddl.auto";
  9. private static final String PROPERTY_NAME_H_CONNECTION_PROVIDER = "hibernate.connection.provider_class";
  10.  
  11. @Autowired
  12. private Environment env;
  13.  
  14. @Bean(destroyMethod = "close")
  15. public HikariDataSource dataSource() {
  16. HikariDataSource ds = new HikariDataSource();
  17. ds.setMaximumPoolSize(100);
  18. ds.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
  19. ds.addDataSourceProperty("url", "jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=UTF-8&transformedBitIsBoolean=true");
  20. ds.addDataSourceProperty("user", "usr");
  21. ds.addDataSourceProperty("password", "pwd");
  22. ds.addDataSourceProperty("cachePrepStmts", true);
  23. ds.addDataSourceProperty("prepStmtCacheSize", 250);
  24. ds.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
  25. ds.addDataSourceProperty("useServerPrepStmts", true);
  26. return ds;
  27. }
  28.  
  29. @Bean
  30. @Autowired
  31. public PlatformTransactionManager transactionManager() throws ClassNotFoundException {
  32. return new JpaTransactionManager(entityManagerFactory().getObject());
  33. }
  34.  
  35. @Bean
  36. public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws ClassNotFoundException {
  37. LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
  38.  
  39. entityManagerFactoryBean.setDataSource(dataSource());
  40. entityManagerFactoryBean.setPackagesToScan("com.app.dao.entity");
  41. entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
  42. entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter());
  43. entityManagerFactoryBean.setJpaDialect(new FlushModeCommitHibernateJpaDialect(FlushMode.COMMIT));
  44. Properties jpaProperties = new Properties();
  45. jpaProperties.put(PROPERTY_NAME_HIBERNATE_DIALECT,
  46. env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
  47. jpaProperties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL,
  48. env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
  49. jpaProperties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL,
  50. env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
  51. jpaProperties.put(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO,
  52. env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO));
  53. jpaProperties.put(PROPERTY_NAME_H_CONNECTION_PROVIDER,
  54. env.getRequiredProperty(PROPERTY_NAME_H_CONNECTION_PROVIDER));
  55. entityManagerFactoryBean.setJpaProperties(jpaProperties);
  56. entityManagerFactoryBean.afterPropertiesSet();
  57. return entityManagerFactoryBean;
  58. }
  59.  
  60. @Bean
  61. public SharedEntityManagerBean sharedEntityManager() throws ClassNotFoundException {
  62. SharedEntityManagerBean sharedEntityManagerBean = new SharedEntityManagerBean();
  63. sharedEntityManagerBean.setEntityManagerFactory(entityManagerFactory().getObject());
  64. return new SharedEntityManagerBean();
  65. }
  66. @Bean
  67. public JpaVendorAdapter jpaVendorAdapter() {
  68. AbstractJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
  69.  
  70. jpaVendorAdapter.setDatabasePlatform(env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
  71. jpaVendorAdapter.setShowSql(env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL, Boolean.class));
  72.  
  73. return jpaVendorAdapter;
  74. }
  75.  
  76. Caused by: java.lang.IllegalArgumentException: one of either dataSource or dataSourceClassName must be specified
  77. at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:683)
  78. at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:75)
  79. at com.zaxxer.hikari.hibernate.HikariConnectionProvider.configure(HikariConnectionProvider.java:80)
  80. ... 86 more
  81.  
  82. com.zaxxer.hikari.hibernate.HikariConnectionProvider.configure(HikariConnectionProvider.java:80)
  83.  
  84. //hikariConfig.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
  85.  
  86. hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");
  87.  
  88. # Spring data source needed for Spring boot to behave
  89. # Pre Spring Boot v2.0.0.M6 without below Spring Boot defaults to tomcat-jdbc connection pool included
  90. # in spring-boot-starter-jdbc and as compiled dependency under spring-boot-starter-data-jpa
  91. spring.datasource.type=com.zaxxer.hikari.HikariDataSource
  92. spring.datasource.url=jdbc:postgresql://localhost:5432/somedb
  93. spring.datasource.username=dbuser
  94. spring.datasource.password=dbpassword
  95.  
  96. # Hikari will use the above plus the following to setup connection pooling
  97. spring.datasource.hikari.minimumIdle=5
  98. spring.datasource.hikari.maximumPoolSize=20
  99. spring.datasource.hikari.idleTimeout=30000
  100. spring.datasource.hikari.poolName=SpringBootJPAHikariCP
  101. spring.datasource.hikari.maxLifetime=2000000
  102. spring.datasource.hikari.connectionTimeout=30000
  103.  
  104. # Without below HikariCP uses deprecated com.zaxxer.hikari.hibernate.HikariConnectionProvider
  105. # Surprisingly enough below ConnectionProvider is in hibernate-hikaricp dependency and not hibernate-core
  106. # So you need to pull that dependency but, make sure to exclude it's transitive dependencies or you will end up
  107. # with different versions of hibernate-core
  108. spring.jpa.hibernate.connection.provider_class=org.hibernate.hikaricp.internal.HikariCPConnectionProvider
  109.  
  110. # JPA specific configs
  111. spring.jpa.properties.hibernate.show_sql=true
  112. spring.jpa.properties.hibernate.format_sql=true
  113. spring.jpa.properties.hibernate.use_sql=true
  114. spring.jpa.properties.hibernate.id.new_generator_mappings=false
  115. spring.jpa.properties.hibernate.default_schema=dbschema
  116. spring.jpa.properties.hibernate.search.autoregister_listeners=false
  117. spring.jpa.properties.hibernate.bytecode.use_reflection_optimizer=false
Add Comment
Please, Sign In to add comment