| Line 12... |
Line 12... |
| 12 |
import com.google.gson.Gson;
|
12 |
import com.google.gson.Gson;
|
| 13 |
import com.google.gson.GsonBuilder;
|
13 |
import com.google.gson.GsonBuilder;
|
| 14 |
import com.spice.profitmandi.dao.convertor.LocalDateJsonConverter;
|
14 |
import com.spice.profitmandi.dao.convertor.LocalDateJsonConverter;
|
| 15 |
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;
|
15 |
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;
|
| 16 |
import com.spice.profitmandi.dao.repository.dtr.Mongo;
|
16 |
import com.spice.profitmandi.dao.repository.dtr.Mongo;
|
| - |
|
17 |
|
| - |
|
18 |
import com.zaxxer.hikari.HikariConfig;
|
| - |
|
19 |
import com.zaxxer.hikari.HikariDataSource;
|
| 17 |
import org.apache.logging.log4j.LogManager;
|
20 |
import org.apache.logging.log4j.LogManager;
|
| 18 |
import org.apache.logging.log4j.Logger;
|
21 |
import org.apache.logging.log4j.Logger;
|
| 19 |
import org.hibernate.SessionFactory;
|
22 |
import org.hibernate.SessionFactory;
|
| 20 |
import org.springframework.beans.factory.annotation.Autowired;
|
23 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 21 |
import org.springframework.beans.factory.annotation.Value;
|
24 |
import org.springframework.beans.factory.annotation.Value;
|
| 22 |
import org.springframework.context.annotation.*;
|
25 |
import org.springframework.context.annotation.*;
|
| - |
|
26 |
import org.springframework.core.env.Environment;
|
| 23 |
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
27 |
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
| 24 |
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
28 |
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
| 25 |
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
|
29 |
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
|
| 26 |
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
30 |
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
| 27 |
|
31 |
|
| Line 38... |
Line 42... |
| 38 |
@PropertySource("classpath:META-INF/env.properties")
|
42 |
@PropertySource("classpath:META-INF/env.properties")
|
| 39 |
public class DBConfig {
|
43 |
public class DBConfig {
|
| 40 |
|
44 |
|
| 41 |
private static final Logger LOGGER = LogManager.getLogger(DBConfig.class);
|
45 |
private static final Logger LOGGER = LogManager.getLogger(DBConfig.class);
|
| 42 |
|
46 |
|
| - |
|
47 |
// Hibernate Property Keys
|
| 43 |
private static final String HIBERNATE_DIALECT = "hibernate.dialect";
|
48 |
private static final String HIBERNATE_DIALECT = "hibernate.dialect";
|
| 44 |
|
- |
|
| 45 |
private static final String HIBERNATE_SHOW_SQL = "hibernate.show_sql";
|
49 |
private static final String HIBERNATE_SHOW_SQL = "hibernate.show_sql";
|
| 46 |
|
- |
|
| 47 |
private static final String HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
|
50 |
private static final String HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
|
| 48 |
|
- |
|
| 49 |
private static final String HIBERNATE_JDBC_BATCH_SIZE = "hibernate.jdbc.batch_size";
|
51 |
private static final String HIBERNATE_JDBC_BATCH_SIZE = "hibernate.jdbc.batch_size";
|
| 50 |
|
52 |
|
| - |
|
53 |
// HikariCP Properties
|
| 51 |
private static final String HIBERNATE_C3P0_MIN_SIZE = "hibernate.c3p0.min_size";
|
54 |
private static final String HIKARI_MAX_POOL_SIZE = "hikari.maximumPoolSize";
|
| 52 |
|
- |
|
| 53 |
private static final String HIBERNATE_C3P0_MAX_SIZE = "hibernate.c3p0.max_size";
|
55 |
private static final String HIKARI_MIN_IDLE = "hikari.minimumIdle";
|
| 54 |
|
- |
|
| 55 |
private static final String HIBERNATE_C3P0_TIMEOUT = "hibernate.c3p0.timeout";
|
56 |
private static final String HIKARI_IDLE_TIMEOUT = "hikari.idleTimeout";
|
| - |
|
57 |
private static final String HIKARI_MAX_LIFETIME = "hikari.maxLifetime";
|
| - |
|
58 |
private static final String HIKARI_CONNECTION_TIMEOUT = "hikari.connectionTimeout";
|
| 56 |
|
59 |
|
| 57 |
private static final String HIBERNATE_C3P0_MAX_STATEMENTS = "hibernate.c3p0.max_statements";
|
- |
|
| 58 |
|
- |
|
| 59 |
private static final String HIBERNATE_C3P0_IDLE_TEST_PERIOD = "hibernate.c3p0.idle_test_period";
|
- |
|
| 60 |
|
60 |
|
| - |
|
61 |
// Injected DB Properties
|
| 61 |
@Value("${hibernate.driver.class}")
|
62 |
@Value("${hibernate.driver.class}")
|
| 62 |
private String hibernateDriverClass;
|
63 |
private String hibernateDriverClass;
|
| 63 |
|
64 |
|
| 64 |
@Value("${hibernate.url}")
|
65 |
@Value("${hibernate.url}")
|
| 65 |
private String hibernateUrl;
|
66 |
private String hibernateUrl;
|
| Line 80... |
Line 81... |
| 80 |
private String hibernateFormatSql;
|
81 |
private String hibernateFormatSql;
|
| 81 |
|
82 |
|
| 82 |
@Value("${hibernate.jdbc.batch_size}")
|
83 |
@Value("${hibernate.jdbc.batch_size}")
|
| 83 |
private String hibernateBatchSize;
|
84 |
private String hibernateBatchSize;
|
| 84 |
|
85 |
|
| 85 |
@Value("${hibernate.c3p0.min_size}")
|
- |
|
| 86 |
private String hibernateMinSize;
|
- |
|
| 87 |
|
- |
|
| 88 |
@Value("${hibernate.c3p0.max_size}")
|
- |
|
| 89 |
private String hibernateMaxSize;
|
- |
|
| 90 |
|
- |
|
| 91 |
@Value("${hibernate.c3p0.timeout}")
|
- |
|
| 92 |
private String hibernateTimeout;
|
86 |
// Mongo Config
|
| 93 |
|
- |
|
| 94 |
@Value("${hibernate.c3p0.max_statements}")
|
- |
|
| 95 |
private String hibernateMaxStatements;
|
- |
|
| 96 |
|
- |
|
| 97 |
@Value("${hibernate.c3p0.idle_test_period}")
|
- |
|
| 98 |
private String hibernateIdleTestPeriod;
|
- |
|
| 99 |
|
- |
|
| 100 |
@Value("${mongo.host}")
|
87 |
@Value("${mongo.host}")
|
| 101 |
private String mongoHost;
|
88 |
private String mongoHost;
|
| 102 |
|
89 |
|
| 103 |
@Value("${content.mongo.host}")
|
90 |
@Value("${content.mongo.host}")
|
| 104 |
private String contentMongoHost;
|
91 |
private String contentMongoHost;
|
| 105 |
|
92 |
|
| - |
|
93 |
@Autowired
|
| - |
|
94 |
private Environment env;
|
| - |
|
95 |
|
| - |
|
96 |
/**
|
| - |
|
97 |
* Primary DataSource using DriverManager (can be replaced with HikariDataSource)
|
| - |
|
98 |
*/
|
| 106 |
@Primary
|
99 |
@Primary
|
| 107 |
@Bean(name = "dataSource")
|
100 |
@Bean(name = "dataSource")
|
| 108 |
public DataSource dataSource() {
|
101 |
public DataSource dataSource() {
|
| 109 |
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
102 |
HikariConfig config = new HikariConfig();
|
| 110 |
dataSource.setDriverClassName(hibernateDriverClass);
|
103 |
config.setDriverClassName(hibernateDriverClass);
|
| 111 |
dataSource.setUrl(hibernateUrl);
|
104 |
config.setJdbcUrl(hibernateUrl);
|
| 112 |
dataSource.setUsername(hibernateUserName);
|
105 |
config.setUsername(hibernateUserName);
|
| 113 |
dataSource.setPassword(hibernatePassword);
|
106 |
config.setPassword(hibernatePassword);
|
| 114 |
LOGGER.info("DataSource returned {}", dataSource);
|
- |
|
| 115 |
return dataSource;
|
- |
|
| 116 |
}
|
107 |
|
| - |
|
108 |
config.setMaximumPoolSize(Integer.parseInt(env.getProperty(HIKARI_MAX_POOL_SIZE, "20")));
|
| - |
|
109 |
config.setMinimumIdle(Integer.parseInt(env.getProperty(HIKARI_MIN_IDLE, "2")));
|
| - |
|
110 |
config.setIdleTimeout(Long.parseLong(env.getProperty(HIKARI_IDLE_TIMEOUT, "30000")));
|
| - |
|
111 |
config.setMaxLifetime(Long.parseLong(env.getProperty(HIKARI_MAX_LIFETIME, "1800000")));
|
| - |
|
112 |
config.setConnectionTimeout(Long.parseLong(env.getProperty(HIKARI_CONNECTION_TIMEOUT, "30000")));
|
| 117 |
|
113 |
|
| 118 |
@Bean
|
- |
|
| 119 |
public ObjectMapper objectMapper() {
|
114 |
config.setPoolName("HikariPool");
|
| 120 |
DateTimeFormatter df = new DateTimeFormatterBuilder().parseCaseInsensitive()
|
- |
|
| 121 |
.append(DateTimeFormatter.ISO_LOCAL_DATE).optionalStart().appendLiteral('T').optionalEnd()
|
- |
|
| 122 |
.appendLiteral(' ').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
|
- |
|
| 123 |
DateTimeFormatter sf = new DateTimeFormatterBuilder().parseCaseInsensitive()
|
- |
|
| 124 |
.append(DateTimeFormatter.ISO_LOCAL_DATE).appendLiteral('T').append(DateTimeFormatter.ISO_LOCAL_TIME)
|
- |
|
| 125 |
.toFormatter();
|
115 |
config.setAutoCommit(true);
|
| 126 |
LocalDateTimeSerializer serializer = new LocalDateTimeSerializer(sf);
|
- |
|
| 127 |
LocalDateTimeDeserializer deserializer = new LocalDateTimeDeserializer(df);
|
- |
|
| 128 |
LocalDateSerializer serializer1 = new LocalDateSerializer(DateTimeFormatter.ISO_LOCAL_DATE);
|
- |
|
| 129 |
LocalDateDeserializer deserializer1 = new LocalDateDeserializer(DateTimeFormatter.ISO_LOCAL_DATE);
|
- |
|
| - |
|
116 |
|
| - |
|
117 |
|
| 130 |
JavaTimeModule jtm = new JavaTimeModule();
|
118 |
HikariDataSource dataSource = new HikariDataSource(config);
|
| 131 |
jtm.addSerializer(LocalDateTime.class, serializer);
|
- |
|
| 132 |
jtm.addSerializer(LocalDate.class, serializer1);
|
- |
|
| 133 |
jtm.addDeserializer(LocalDateTime.class, deserializer);
|
- |
|
| 134 |
jtm.addDeserializer(LocalDate.class, deserializer1);
|
- |
|
| 135 |
ObjectMapper mapper = new ObjectMapper()
|
- |
|
| 136 |
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
- |
|
| 137 |
.registerModule(new ParameterNamesModule())
|
- |
|
| 138 |
.registerModule(new Jdk8Module()).registerModule(jtm); // new module, NOT JSR310Module
|
- |
|
| 139 |
LOGGER.info("ObjectMapper returned {}", mapper);
|
119 |
LOGGER.info("HikariDataSource initialized with JDBC URL {}", hibernateUrl);
|
| 140 |
return mapper;
|
120 |
return dataSource;
|
| 141 |
}
|
121 |
}
|
| 142 |
|
122 |
|
| - |
|
123 |
/**
|
| - |
|
124 |
* Configure Hibernate properties
|
| - |
|
125 |
*/
|
| 143 |
@Bean
|
126 |
@Bean
|
| 144 |
public Properties getHibernateProperties() {
|
127 |
public Properties getHibernateProperties() {
|
| 145 |
Properties dbProperties = new Properties();
|
128 |
Properties dbProperties = new Properties();
|
| 146 |
dbProperties.put(HIBERNATE_DIALECT, hibernateDialect);
|
129 |
dbProperties.put(HIBERNATE_DIALECT, hibernateDialect);
|
| 147 |
dbProperties.put(HIBERNATE_SHOW_SQL, hibernateShowSql);
|
130 |
dbProperties.put(HIBERNATE_SHOW_SQL, hibernateShowSql);
|
| 148 |
dbProperties.put(HIBERNATE_FORMAT_SQL, hibernateFormatSql);
|
131 |
dbProperties.put(HIBERNATE_FORMAT_SQL, hibernateFormatSql);
|
| 149 |
dbProperties.put(HIBERNATE_JDBC_BATCH_SIZE, hibernateBatchSize);
|
132 |
dbProperties.put(HIBERNATE_JDBC_BATCH_SIZE, hibernateBatchSize);
|
| 150 |
dbProperties.put(HIBERNATE_C3P0_MIN_SIZE, hibernateMinSize);
|
- |
|
| 151 |
dbProperties.put(HIBERNATE_C3P0_MAX_SIZE, hibernateMaxSize);
|
- |
|
| 152 |
dbProperties.put(HIBERNATE_C3P0_TIMEOUT, hibernateTimeout);
|
133 |
// HikariCP properties (Optional — Spring Boot auto-configures most)
|
| 153 |
dbProperties.put(HIBERNATE_C3P0_MAX_STATEMENTS, hibernateMaxStatements);
|
- |
|
| 154 |
dbProperties.put(HIBERNATE_C3P0_IDLE_TEST_PERIOD, hibernateIdleTestPeriod);
|
134 |
//dbProperties.put("hibernate.connection.provider_class", "org.hibernate.hikaricp.internal.HikariCPConnectionProvider");
|
| 155 |
return dbProperties;
|
135 |
return dbProperties;
|
| 156 |
}
|
136 |
}
|
| 157 |
|
137 |
|
| - |
|
138 |
/**
|
| - |
|
139 |
* Configure Hibernate SessionFactory
|
| - |
|
140 |
*/
|
| 158 |
@Primary
|
141 |
@Primary
|
| 159 |
@Autowired
|
- |
|
| 160 |
@Bean(name = "sessionFactory")
|
142 |
@Bean(name = "sessionFactory")
|
| - |
|
143 |
@Autowired
|
| 161 |
public SessionFactory getSessionFactory(DataSource dataSource) {
|
144 |
public SessionFactory getSessionFactory(DataSource dataSource) {
|
| 162 |
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
|
145 |
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
|
| 163 |
sessionBuilder.addProperties(getHibernateProperties());
|
146 |
sessionBuilder.addProperties(getHibernateProperties());
|
| 164 |
sessionBuilder.scanPackages("com.spice.profitmandi.dao.*");
|
147 |
sessionBuilder.scanPackages("com.spice.profitmandi.dao.*");
|
| 165 |
LOGGER.info("Session Factory returned {}", "sessionFactory");
|
148 |
LOGGER.info("SessionFactory created");
|
| 166 |
return sessionBuilder.buildSessionFactory();
|
149 |
return sessionBuilder.buildSessionFactory();
|
| 167 |
}
|
150 |
}
|
| 168 |
|
151 |
|
| - |
|
152 |
/**
|
| - |
|
153 |
* Transaction Manager
|
| - |
|
154 |
*/
|
| 169 |
@Primary
|
155 |
@Primary
|
| 170 |
@Autowired
|
- |
|
| 171 |
@Bean(name = "transactionManager")
|
156 |
@Bean(name = "transactionManager")
|
| - |
|
157 |
@Autowired
|
| 172 |
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
|
158 |
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
|
| 173 |
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
|
159 |
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
|
| 174 |
LOGGER.info("Transaction Manager returned {}", transactionManager);
|
160 |
LOGGER.info("TransactionManager created");
|
| 175 |
return transactionManager;
|
161 |
return transactionManager;
|
| 176 |
}
|
162 |
}
|
| 177 |
|
163 |
|
| - |
|
164 |
/**
|
| - |
|
165 |
* ObjectMapper Bean with Java 8 Time module config
|
| - |
|
166 |
*/
|
| - |
|
167 |
@Bean
|
| - |
|
168 |
public ObjectMapper objectMapper() {
|
| - |
|
169 |
DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
|
| - |
|
170 |
.parseCaseInsensitive()
|
| - |
|
171 |
.append(DateTimeFormatter.ISO_LOCAL_DATE)
|
| - |
|
172 |
.optionalStart().appendLiteral('T').optionalEnd()
|
| - |
|
173 |
.appendLiteral(' ')
|
| - |
|
174 |
.append(DateTimeFormatter.ISO_LOCAL_TIME)
|
| - |
|
175 |
.toFormatter();
|
| - |
|
176 |
|
| - |
|
177 |
DateTimeFormatter standardFormatter = new DateTimeFormatterBuilder()
|
| - |
|
178 |
.parseCaseInsensitive()
|
| - |
|
179 |
.append(DateTimeFormatter.ISO_LOCAL_DATE)
|
| - |
|
180 |
.appendLiteral('T')
|
| - |
|
181 |
.append(DateTimeFormatter.ISO_LOCAL_TIME)
|
| - |
|
182 |
.toFormatter();
|
| - |
|
183 |
|
| - |
|
184 |
JavaTimeModule javaTimeModule = new JavaTimeModule();
|
| - |
|
185 |
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(standardFormatter));
|
| - |
|
186 |
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ISO_LOCAL_DATE));
|
| - |
|
187 |
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
|
| - |
|
188 |
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ISO_LOCAL_DATE));
|
| - |
|
189 |
|
| - |
|
190 |
ObjectMapper mapper = new ObjectMapper()
|
| - |
|
191 |
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
| - |
|
192 |
.registerModule(new ParameterNamesModule())
|
| - |
|
193 |
.registerModule(new Jdk8Module())
|
| - |
|
194 |
.registerModule(javaTimeModule);
|
| - |
|
195 |
|
| - |
|
196 |
LOGGER.info("ObjectMapper configured");
|
| - |
|
197 |
return mapper;
|
| - |
|
198 |
}
|
| - |
|
199 |
|
| - |
|
200 |
/**
|
| - |
|
201 |
* Mongo Client Bean
|
| - |
|
202 |
*/
|
| 178 |
@Bean
|
203 |
@Bean
|
| 179 |
public Mongo mongoClient(SessionFactory sessionFactory) {
|
204 |
public Mongo mongoClient(SessionFactory sessionFactory) {
|
| 180 |
return new Mongo(mongoHost, contentMongoHost);
|
205 |
return new Mongo(mongoHost, contentMongoHost);
|
| 181 |
}
|
206 |
}
|
| 182 |
|
207 |
|
| - |
|
208 |
/**
|
| - |
|
209 |
* Gson Bean for LocalDate and LocalDateTime
|
| - |
|
210 |
*/
|
| 183 |
@Bean(name = "gson")
|
211 |
@Bean(name = "gson")
|
| 184 |
public Gson gson() {
|
212 |
public Gson gson() {
|
| 185 |
Gson gson = new GsonBuilder().serializeNulls()
|
213 |
Gson gson = new GsonBuilder()
|
| - |
|
214 |
.serializeNulls()
|
| 186 |
.registerTypeAdapter(LocalDate.class, new LocalDateJsonConverter())
|
215 |
.registerTypeAdapter(LocalDate.class, new LocalDateJsonConverter())
|
| 187 |
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
|
216 |
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter())
|
| - |
|
217 |
.create();
|
| 188 |
LOGGER.info("Gson returned {}", gson);
|
218 |
LOGGER.info("Gson configured");
|
| 189 |
return gson;
|
219 |
return gson;
|
| 190 |
|
- |
|
| 191 |
}
|
220 |
}
|
| 192 |
}
|
221 |
}
|