| 21649 |
amit.gupta |
1 |
package com.spice.profitmandi.dao.config;
|
| 35383 |
amit |
2 |
|
| 21649 |
amit.gupta |
3 |
import java.io.IOException;
|
| 35383 |
amit |
4 |
import java.sql.Driver;
|
|
|
5 |
import java.sql.DriverManager;
|
|
|
6 |
import java.sql.SQLException;
|
|
|
7 |
import java.util.Enumeration;
|
| 21649 |
amit.gupta |
8 |
import java.util.Properties;
|
|
|
9 |
|
| 35383 |
amit |
10 |
import javax.annotation.PreDestroy;
|
| 21649 |
amit.gupta |
11 |
import javax.sql.DataSource;
|
|
|
12 |
|
| 35383 |
amit |
13 |
import com.mysql.cj.jdbc.AbandonedConnectionCleanupThread;
|
| 35596 |
amit |
14 |
import com.zaxxer.hikari.HikariConfig;
|
|
|
15 |
import com.zaxxer.hikari.HikariDataSource;
|
| 21649 |
amit.gupta |
16 |
import org.hibernate.SessionFactory;
|
| 23568 |
govind |
17 |
import org.apache.logging.log4j.Logger;
|
|
|
18 |
import org.apache.logging.log4j.LogManager;
|
| 21649 |
amit.gupta |
19 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
20 |
import org.springframework.context.annotation.Bean;
|
|
|
21 |
import org.springframework.context.annotation.ComponentScan;
|
|
|
22 |
import org.springframework.context.annotation.Configuration;
|
| 35596 |
amit |
23 |
import org.springframework.context.annotation.Primary;
|
| 21649 |
amit.gupta |
24 |
import org.springframework.core.io.Resource;
|
|
|
25 |
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
|
|
26 |
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
|
|
|
27 |
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
| 22173 |
amit.gupta |
28 |
import com.spice.profitmandi.dao.repository.dtr.Mongo;
|
| 21649 |
amit.gupta |
29 |
|
|
|
30 |
import com.spice.profitmandi.web.config.AppConfig;
|
|
|
31 |
|
| 23568 |
govind |
32 |
|
| 21649 |
amit.gupta |
33 |
@Configuration
|
|
|
34 |
@EnableTransactionManagement
|
|
|
35 |
@ComponentScan({ "com.spice.profitmandi.*" })
|
| 35383 |
amit |
36 |
public class WebDBContextConfigure {
|
|
|
37 |
|
|
|
38 |
private static final Logger LOGGER = LogManager.getLogger(WebDBContextConfigure.class);
|
| 35596 |
amit |
39 |
|
| 21649 |
amit.gupta |
40 |
private static final String HIBERNATE_DRIVER_CLASS = "hibernate.driver.class";
|
|
|
41 |
private static final String HIBERNATE_URL = "hibernate.url";
|
|
|
42 |
private static final String HIBERNATE_USER_NAME = "hibernate.user.name";
|
|
|
43 |
private static final String HIBERNATE_PASSWORD = "hibernate.password";
|
|
|
44 |
private static final String HIBERNATE_DIALECT = "hibernate.dialect";
|
|
|
45 |
private static final String HIBERNATE_SHOW_SQL = "hibernate.show_sql";
|
|
|
46 |
private static final String HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
|
|
|
47 |
private static final String HIBERNATE_JDBC_BATCH_SIZE = "hibernate.jdbc.batch_size";
|
| 35828 |
amit |
48 |
private static final String HIBERNATE_ORDER_INSERTS = "hibernate.order_inserts";
|
|
|
49 |
private static final String HIBERNATE_ORDER_UPDATES = "hibernate.order_updates";
|
| 35596 |
amit |
50 |
private static final String HIKARI_MAX_POOL_SIZE = "hikari.maximumPoolSize";
|
|
|
51 |
private static final String HIKARI_MIN_IDLE = "hikari.minimumIdle";
|
|
|
52 |
private static final String HIKARI_IDLE_TIMEOUT = "hikari.idleTimeout";
|
|
|
53 |
private static final String HIKARI_MAX_LIFETIME = "hikari.maxLifetime";
|
|
|
54 |
private static final String HIKARI_CONNECTION_TIMEOUT = "hikari.connectionTimeout";
|
|
|
55 |
|
| 21649 |
amit.gupta |
56 |
private Resource resource = AppConfig.getResource();
|
|
|
57 |
|
| 35832 |
amit |
58 |
private Properties loadAllProperties() {
|
| 21649 |
amit.gupta |
59 |
Properties properties = new Properties();
|
|
|
60 |
try {
|
| 35832 |
amit |
61 |
Resource sharedRes = AppConfig.getSharedResource();
|
|
|
62 |
if (sharedRes != null && sharedRes.exists()) {
|
|
|
63 |
properties.load(sharedRes.getInputStream());
|
|
|
64 |
}
|
| 21649 |
amit.gupta |
65 |
properties.load(resource.getInputStream());
|
|
|
66 |
} catch (IOException e) {
|
| 35596 |
amit |
67 |
LOGGER.error("Failed to load properties", e);
|
| 21649 |
amit.gupta |
68 |
}
|
| 35832 |
amit |
69 |
return properties;
|
|
|
70 |
}
|
| 35596 |
amit |
71 |
|
| 35832 |
amit |
72 |
@Bean(name = "dataSource", destroyMethod = "close")
|
|
|
73 |
public DataSource dataSource() {
|
|
|
74 |
Properties properties = loadAllProperties();
|
|
|
75 |
|
| 35596 |
amit |
76 |
HikariConfig config = new HikariConfig();
|
|
|
77 |
config.setDriverClassName(properties.getProperty(HIBERNATE_DRIVER_CLASS));
|
|
|
78 |
config.setJdbcUrl(properties.getProperty(HIBERNATE_URL));
|
|
|
79 |
config.setUsername(properties.getProperty(HIBERNATE_USER_NAME));
|
|
|
80 |
config.setPassword(properties.getProperty(HIBERNATE_PASSWORD));
|
|
|
81 |
config.setMaximumPoolSize(Integer.parseInt(properties.getProperty(HIKARI_MAX_POOL_SIZE, "20")));
|
|
|
82 |
config.setMinimumIdle(Integer.parseInt(properties.getProperty(HIKARI_MIN_IDLE, "2")));
|
|
|
83 |
config.setIdleTimeout(Long.parseLong(properties.getProperty(HIKARI_IDLE_TIMEOUT, "30000")));
|
|
|
84 |
config.setMaxLifetime(Long.parseLong(properties.getProperty(HIKARI_MAX_LIFETIME, "1800000")));
|
|
|
85 |
config.setConnectionTimeout(Long.parseLong(properties.getProperty(HIKARI_CONNECTION_TIMEOUT, "30000")));
|
|
|
86 |
config.setPoolName("HikariPool-Web");
|
|
|
87 |
config.setLeakDetectionThreshold(30000);
|
|
|
88 |
|
|
|
89 |
LOGGER.info("Initializing HikariDataSource with URL: {}", properties.getProperty(HIBERNATE_URL));
|
|
|
90 |
return new HikariDataSource(config);
|
| 21649 |
amit.gupta |
91 |
}
|
|
|
92 |
|
|
|
93 |
@Bean
|
|
|
94 |
public Properties getHibernateProperties() {
|
|
|
95 |
Properties dbProperties = new Properties();
|
| 35832 |
amit |
96 |
Properties properties = loadAllProperties();
|
| 21649 |
amit.gupta |
97 |
dbProperties.put(HIBERNATE_DIALECT, properties.getProperty(HIBERNATE_DIALECT));
|
|
|
98 |
dbProperties.put(HIBERNATE_SHOW_SQL, properties.getProperty(HIBERNATE_SHOW_SQL));
|
|
|
99 |
dbProperties.put(HIBERNATE_FORMAT_SQL, properties.getProperty(HIBERNATE_FORMAT_SQL));
|
|
|
100 |
dbProperties.put(HIBERNATE_JDBC_BATCH_SIZE, properties.getProperty(HIBERNATE_JDBC_BATCH_SIZE));
|
| 35828 |
amit |
101 |
dbProperties.put(HIBERNATE_ORDER_INSERTS, properties.getProperty(HIBERNATE_ORDER_INSERTS, "true"));
|
|
|
102 |
dbProperties.put(HIBERNATE_ORDER_UPDATES, properties.getProperty(HIBERNATE_ORDER_UPDATES, "true"));
|
| 21649 |
amit.gupta |
103 |
return dbProperties;
|
|
|
104 |
}
|
|
|
105 |
|
| 35596 |
amit |
106 |
@Primary
|
| 21649 |
amit.gupta |
107 |
@Bean(name = "sessionFactory")
|
|
|
108 |
public SessionFactory getSessionFactory(DataSource dataSource) {
|
|
|
109 |
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
|
|
|
110 |
sessionBuilder.addProperties(getHibernateProperties());
|
|
|
111 |
sessionBuilder.scanPackages("com.spice.profitmandi.dao.*");
|
| 35596 |
amit |
112 |
LOGGER.info("SessionFactory created");
|
| 21649 |
amit.gupta |
113 |
return sessionBuilder.buildSessionFactory();
|
|
|
114 |
}
|
|
|
115 |
|
| 35596 |
amit |
116 |
@Primary
|
| 21649 |
amit.gupta |
117 |
@Bean(name = "transactionManager")
|
|
|
118 |
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
|
| 35596 |
amit |
119 |
return new HibernateTransactionManager(sessionFactory);
|
| 21649 |
amit.gupta |
120 |
}
|
| 22173 |
amit.gupta |
121 |
|
| 35383 |
amit |
122 |
@Bean(destroyMethod = "close")
|
| 22355 |
ashik.ali |
123 |
public Mongo mongoClient() {
|
| 35832 |
amit |
124 |
Properties properties = loadAllProperties();
|
| 22173 |
amit.gupta |
125 |
return new Mongo(properties.getProperty("mongo.host"), properties.getProperty("content.mongo.host"));
|
|
|
126 |
}
|
| 35383 |
amit |
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Cleanup method to prevent memory leaks on shutdown.
|
|
|
130 |
* Deregisters JDBC drivers and stops MySQL cleanup thread.
|
|
|
131 |
*/
|
|
|
132 |
@PreDestroy
|
|
|
133 |
public void cleanup() {
|
|
|
134 |
LOGGER.info("WebDBContextConfigure cleanup started...");
|
|
|
135 |
|
|
|
136 |
// Stop MySQL AbandonedConnectionCleanupThread
|
|
|
137 |
try {
|
|
|
138 |
AbandonedConnectionCleanupThread.checkedShutdown();
|
|
|
139 |
LOGGER.info("MySQL AbandonedConnectionCleanupThread stopped");
|
|
|
140 |
} catch (Exception e) {
|
|
|
141 |
LOGGER.error("Error stopping MySQL cleanup thread", e);
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
// Deregister JDBC drivers loaded by this webapp's classloader
|
|
|
145 |
ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
|
|
146 |
Enumeration<Driver> drivers = DriverManager.getDrivers();
|
|
|
147 |
while (drivers.hasMoreElements()) {
|
|
|
148 |
Driver driver = drivers.nextElement();
|
|
|
149 |
if (driver.getClass().getClassLoader() == cl) {
|
|
|
150 |
try {
|
|
|
151 |
DriverManager.deregisterDriver(driver);
|
|
|
152 |
LOGGER.info("Deregistered JDBC driver: {}", driver);
|
|
|
153 |
} catch (SQLException e) {
|
|
|
154 |
LOGGER.error("Error deregistering JDBC driver {}", driver, e);
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
LOGGER.info("WebDBContextConfigure cleanup completed");
|
|
|
160 |
}
|
| 21649 |
amit.gupta |
161 |
}
|