| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.dao.config;
|
1 |
package com.spice.profitmandi.dao.config;
|
| 2 |
|
2 |
|
| 3 |
import com.spice.profitmandi.dao.repository.dtr.Mongo;
|
3 |
import com.spice.profitmandi.dao.repository.dtr.Mongo;
|
| 4 |
import com.spice.profitmandi.web.config.AppConfig;
|
4 |
import com.spice.profitmandi.web.config.AppConfig;
|
| - |
|
5 |
import com.zaxxer.hikari.HikariConfig;
|
| - |
|
6 |
import com.zaxxer.hikari.HikariDataSource;
|
| 5 |
import org.hibernate.SessionFactory;
|
7 |
import org.hibernate.SessionFactory;
|
| 6 |
import org.springframework.context.annotation.Bean;
|
8 |
import org.springframework.context.annotation.Bean;
|
| 7 |
import org.springframework.context.annotation.ComponentScan;
|
9 |
import org.springframework.context.annotation.ComponentScan;
|
| 8 |
import org.springframework.context.annotation.Configuration;
|
10 |
import org.springframework.context.annotation.Configuration;
|
| 9 |
import org.springframework.context.annotation.Primary;
|
11 |
import org.springframework.context.annotation.Primary;
|
| 10 |
import org.springframework.core.io.Resource;
|
12 |
import org.springframework.core.io.Resource;
|
| 11 |
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
- |
|
| 12 |
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
13 |
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
| 13 |
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
|
14 |
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
|
| 14 |
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
15 |
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
| 15 |
|
16 |
|
| 16 |
import javax.sql.DataSource;
|
17 |
import javax.sql.DataSource;
|
| Line 18... |
Line 19... |
| 18 |
import java.util.Properties;
|
19 |
import java.util.Properties;
|
| 19 |
|
20 |
|
| 20 |
@Configuration
|
21 |
@Configuration
|
| 21 |
@EnableTransactionManagement
|
22 |
@EnableTransactionManagement
|
| 22 |
@ComponentScan({ "com.spice.profitmandi.dao.*" })
|
23 |
@ComponentScan({ "com.spice.profitmandi.dao.*" })
|
| 23 |
public class WebDBContextConfigure{
|
24 |
public class WebDBContextConfigure {
|
| 24 |
|
25 |
|
| 25 |
private static final String HIBERNATE_DRIVER_CLASS = "hibernate.driver.class";
|
26 |
private static final String HIBERNATE_DRIVER_CLASS = "hibernate.driver.class";
|
| 26 |
private static final String HIBERNATE_URL = "hibernate.url";
|
27 |
private static final String HIBERNATE_URL = "hibernate.url";
|
| 27 |
private static final String HIBERNATE_USER_NAME = "hibernate.user.name";
|
28 |
private static final String HIBERNATE_USER_NAME = "hibernate.user.name";
|
| 28 |
private static final String HIBERNATE_PASSWORD = "hibernate.password";
|
29 |
private static final String HIBERNATE_PASSWORD = "hibernate.password";
|
| 29 |
private static final String HIBERNATE_DIALECT = "hibernate.dialect";
|
30 |
private static final String HIKARI_MAX_POOL_SIZE = "hikari.maximumPoolSize";
|
| 30 |
private static final String HIBERNATE_SHOW_SQL = "hibernate.show_sql";
|
31 |
private static final String HIKARI_MIN_IDLE = "hikari.minimumIdle";
|
| 31 |
private static final String HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
|
32 |
private static final String HIKARI_IDLE_TIMEOUT = "hikari.idleTimeout";
|
| 32 |
private static final String HIBERNATE_JDBC_BATCH_SIZE = "hibernate.jdbc.batch_size";
|
- |
|
| 33 |
private static final String HIBERNATE_C3P0_MIN_SIZE = "hibernate.c3p0.min_size";
|
33 |
private static final String HIKARI_MAX_LIFETIME = "hikari.maxLifetime";
|
| 34 |
private static final String HIBERNATE_C3P0_MAX_SIZE = "hibernate.c3p0.max_size";
|
- |
|
| 35 |
private static final String HIBERNATE_C3P0_TIMEOUT= "hibernate.c3p0.timeout";
|
34 |
private static final String HIKARI_CONNECTION_TIMEOUT = "hikari.connectionTimeout";
|
| 36 |
private static final String HIBERNATE_C3P0_MAX_STATEMENTS = "hibernate.c3p0.max_statements";
|
- |
|
| 37 |
private static final String HIBERNATE_C3P0_IDLE_TEST_PERIOD = "hibernate.c3p0.idle_test_period";
|
- |
|
| - |
|
35 |
|
| 38 |
private Resource resource = AppConfig.getResource();
|
36 |
private Resource resource = AppConfig.getResource();
|
| 39 |
|
37 |
|
| 40 |
@Bean(name = "dataSource")
|
38 |
@Bean(name = "dataSource")
|
| 41 |
public DataSource dataSource() {
|
39 |
public DataSource dataSource() {
|
| 42 |
Properties properties = new Properties();
|
40 |
Properties properties = new Properties();
|
| 43 |
try {
|
41 |
try {
|
| 44 |
properties.load(resource.getInputStream());
|
42 |
properties.load(resource.getInputStream());
|
| 45 |
} catch (IOException e) {
|
43 |
} catch (IOException e) {
|
| 46 |
e.printStackTrace();
|
44 |
e.printStackTrace();
|
| 47 |
}
|
45 |
}
|
| - |
|
46 |
|
| 48 |
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
47 |
HikariConfig config = new HikariConfig();
|
| 49 |
dataSource.setDriverClassName(properties.getProperty(HIBERNATE_DRIVER_CLASS));
|
48 |
config.setDriverClassName(properties.getProperty(HIBERNATE_DRIVER_CLASS));
|
| 50 |
dataSource.setUrl(properties.getProperty(HIBERNATE_URL));
|
49 |
config.setJdbcUrl(properties.getProperty(HIBERNATE_URL));
|
| 51 |
dataSource.setUsername(properties.getProperty(HIBERNATE_USER_NAME));
|
50 |
config.setUsername(properties.getProperty(HIBERNATE_USER_NAME));
|
| 52 |
dataSource.setPassword(properties.getProperty(HIBERNATE_PASSWORD));
|
51 |
config.setPassword(properties.getProperty(HIBERNATE_PASSWORD));
|
| - |
|
52 |
config.setMaximumPoolSize(Integer.parseInt(properties.getProperty(HIKARI_MAX_POOL_SIZE, "20")));
|
| - |
|
53 |
config.setMinimumIdle(Integer.parseInt(properties.getProperty(HIKARI_MIN_IDLE, "2")));
|
| - |
|
54 |
config.setIdleTimeout(Long.parseLong(properties.getProperty(HIKARI_IDLE_TIMEOUT, "30000")));
|
| - |
|
55 |
config.setMaxLifetime(Long.parseLong(properties.getProperty(HIKARI_MAX_LIFETIME, "1800000")));
|
| - |
|
56 |
config.setConnectionTimeout(Long.parseLong(properties.getProperty(HIKARI_CONNECTION_TIMEOUT, "30000")));
|
| - |
|
57 |
|
| 53 |
return dataSource;
|
58 |
return new HikariDataSource(config);
|
| 54 |
}
|
59 |
}
|
| 55 |
|
60 |
|
| 56 |
@Bean
|
61 |
@Bean
|
| 57 |
public Properties getHibernateProperties() {
|
62 |
public Properties getHibernateProperties() {
|
| 58 |
Properties dbProperties = new Properties();
|
63 |
Properties dbProperties = new Properties();
|
| Line 60... |
Line 65... |
| 60 |
try {
|
65 |
try {
|
| 61 |
properties.load(resource.getInputStream());
|
66 |
properties.load(resource.getInputStream());
|
| 62 |
} catch (IOException e) {
|
67 |
} catch (IOException e) {
|
| 63 |
e.printStackTrace();
|
68 |
e.printStackTrace();
|
| 64 |
}
|
69 |
}
|
| 65 |
dbProperties.put(HIBERNATE_DIALECT, properties.getProperty(HIBERNATE_DIALECT));
|
70 |
dbProperties.put("hibernate.dialect", properties.getProperty("hibernate.dialect"));
|
| 66 |
dbProperties.put(HIBERNATE_SHOW_SQL, properties.getProperty(HIBERNATE_SHOW_SQL));
|
71 |
dbProperties.put("hibernate.show_sql", properties.getProperty("hibernate.show_sql"));
|
| 67 |
dbProperties.put(HIBERNATE_FORMAT_SQL, properties.getProperty(HIBERNATE_FORMAT_SQL));
|
72 |
dbProperties.put("hibernate.format_sql", properties.getProperty("hibernate.format_sql"));
|
| 68 |
dbProperties.put(HIBERNATE_JDBC_BATCH_SIZE, properties.getProperty(HIBERNATE_JDBC_BATCH_SIZE));
|
73 |
dbProperties.put("hibernate.jdbc.batch_size", properties.getProperty("hibernate.jdbc.batch_size"));
|
| 69 |
dbProperties.put(HIBERNATE_C3P0_MIN_SIZE, properties.getProperty(HIBERNATE_C3P0_MIN_SIZE));
|
- |
|
| 70 |
dbProperties.put(HIBERNATE_C3P0_MAX_SIZE, properties.getProperty(HIBERNATE_C3P0_MAX_SIZE));
|
- |
|
| 71 |
dbProperties.put(HIBERNATE_C3P0_TIMEOUT, properties.getProperty(HIBERNATE_C3P0_TIMEOUT));
|
- |
|
| 72 |
dbProperties.put(HIBERNATE_C3P0_MAX_STATEMENTS, properties.getProperty(HIBERNATE_C3P0_MAX_STATEMENTS));
|
- |
|
| 73 |
dbProperties.put(HIBERNATE_C3P0_IDLE_TEST_PERIOD, properties.getProperty(HIBERNATE_C3P0_IDLE_TEST_PERIOD));
|
- |
|
| 74 |
return dbProperties;
|
74 |
return dbProperties;
|
| 75 |
}
|
75 |
}
|
| 76 |
|
76 |
|
| 77 |
@Primary
|
77 |
@Primary
|
| 78 |
@Bean(name = "sessionFactory")
|
78 |
@Bean(name = "sessionFactory")
|
| Line 84... |
Line 84... |
| 84 |
}
|
84 |
}
|
| 85 |
|
85 |
|
| 86 |
@Primary
|
86 |
@Primary
|
| 87 |
@Bean(name = "transactionManager")
|
87 |
@Bean(name = "transactionManager")
|
| 88 |
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
|
88 |
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
|
| 89 |
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
|
89 |
return new HibernateTransactionManager(sessionFactory);
|
| 90 |
return transactionManager;
|
- |
|
| 91 |
}
|
90 |
}
|
| 92 |
|
- |
|
| 93 |
|
91 |
|
| 94 |
@Bean
|
92 |
@Bean
|
| 95 |
public Mongo mongoClient(SessionFactory sessionFactory) {
|
93 |
public Mongo mongoClient(SessionFactory sessionFactory) {
|
| 96 |
Properties properties = new Properties();
|
94 |
Properties properties = new Properties();
|
| 97 |
try {
|
95 |
try {
|
| Line 100... |
Line 98... |
| 100 |
e.printStackTrace();
|
98 |
e.printStackTrace();
|
| 101 |
}
|
99 |
}
|
| 102 |
return new Mongo(properties.getProperty("content.mongo.host"),
|
100 |
return new Mongo(properties.getProperty("content.mongo.host"),
|
| 103 |
properties.getProperty("content.mongo.host"));
|
101 |
properties.getProperty("content.mongo.host"));
|
| 104 |
}
|
102 |
}
|
| 105 |
}
|
103 |
}
|
| 106 |
|
104 |
|