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