| 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";
|
|
|
45 |
|
| 21598 |
ashik.ali |
46 |
private Resource resource = AppConfig.getResource();
|
|
|
47 |
|
| 35380 |
amit |
48 |
@Bean(name = "dataSource", destroyMethod = "close")
|
| 21598 |
ashik.ali |
49 |
public DataSource dataSource() {
|
|
|
50 |
Properties properties = new Properties();
|
|
|
51 |
try {
|
|
|
52 |
properties.load(resource.getInputStream());
|
|
|
53 |
} catch (IOException e) {
|
|
|
54 |
e.printStackTrace();
|
|
|
55 |
}
|
| 34686 |
amit.gupta |
56 |
|
|
|
57 |
HikariConfig config = new HikariConfig();
|
|
|
58 |
config.setDriverClassName(properties.getProperty(HIBERNATE_DRIVER_CLASS));
|
|
|
59 |
config.setJdbcUrl(properties.getProperty(HIBERNATE_URL));
|
|
|
60 |
config.setUsername(properties.getProperty(HIBERNATE_USER_NAME));
|
|
|
61 |
config.setPassword(properties.getProperty(HIBERNATE_PASSWORD));
|
|
|
62 |
config.setMaximumPoolSize(Integer.parseInt(properties.getProperty(HIKARI_MAX_POOL_SIZE, "20")));
|
|
|
63 |
config.setMinimumIdle(Integer.parseInt(properties.getProperty(HIKARI_MIN_IDLE, "2")));
|
|
|
64 |
config.setIdleTimeout(Long.parseLong(properties.getProperty(HIKARI_IDLE_TIMEOUT, "30000")));
|
|
|
65 |
config.setMaxLifetime(Long.parseLong(properties.getProperty(HIKARI_MAX_LIFETIME, "1800000")));
|
|
|
66 |
config.setConnectionTimeout(Long.parseLong(properties.getProperty(HIKARI_CONNECTION_TIMEOUT, "30000")));
|
| 35042 |
amit |
67 |
config.setLeakDetectionThreshold(30000);
|
| 34686 |
amit.gupta |
68 |
|
|
|
69 |
return new HikariDataSource(config);
|
| 21598 |
ashik.ali |
70 |
}
|
|
|
71 |
|
|
|
72 |
@Bean
|
|
|
73 |
public Properties getHibernateProperties() {
|
|
|
74 |
Properties dbProperties = new Properties();
|
|
|
75 |
Properties properties = new Properties();
|
|
|
76 |
try {
|
|
|
77 |
properties.load(resource.getInputStream());
|
|
|
78 |
} catch (IOException e) {
|
|
|
79 |
e.printStackTrace();
|
|
|
80 |
}
|
| 34686 |
amit.gupta |
81 |
dbProperties.put("hibernate.dialect", properties.getProperty("hibernate.dialect"));
|
|
|
82 |
dbProperties.put("hibernate.show_sql", properties.getProperty("hibernate.show_sql"));
|
|
|
83 |
dbProperties.put("hibernate.format_sql", properties.getProperty("hibernate.format_sql"));
|
|
|
84 |
dbProperties.put("hibernate.jdbc.batch_size", properties.getProperty("hibernate.jdbc.batch_size"));
|
| 21598 |
ashik.ali |
85 |
return dbProperties;
|
|
|
86 |
}
|
|
|
87 |
|
| 23884 |
amit.gupta |
88 |
@Primary
|
| 21598 |
ashik.ali |
89 |
@Bean(name = "sessionFactory")
|
|
|
90 |
public SessionFactory getSessionFactory(DataSource dataSource) {
|
|
|
91 |
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
|
|
|
92 |
sessionBuilder.addProperties(getHibernateProperties());
|
|
|
93 |
sessionBuilder.scanPackages("com.spice.profitmandi.dao.*");
|
|
|
94 |
return sessionBuilder.buildSessionFactory();
|
|
|
95 |
}
|
|
|
96 |
|
| 23884 |
amit.gupta |
97 |
@Primary
|
| 21598 |
ashik.ali |
98 |
@Bean(name = "transactionManager")
|
|
|
99 |
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
|
| 34686 |
amit.gupta |
100 |
return new HibernateTransactionManager(sessionFactory);
|
| 21598 |
ashik.ali |
101 |
}
|
| 22172 |
amit.gupta |
102 |
|
| 35380 |
amit |
103 |
@Bean(destroyMethod = "close")
|
| 22178 |
amit.gupta |
104 |
public Mongo mongoClient(SessionFactory sessionFactory) {
|
| 22172 |
amit.gupta |
105 |
Properties properties = new Properties();
|
|
|
106 |
try {
|
|
|
107 |
properties.load(resource.getInputStream());
|
|
|
108 |
} catch (IOException e) {
|
|
|
109 |
e.printStackTrace();
|
|
|
110 |
}
|
| 26850 |
amit.gupta |
111 |
return new Mongo(properties.getProperty("content.mongo.host"),
|
| 24383 |
amit.gupta |
112 |
properties.getProperty("content.mongo.host"));
|
| 22178 |
amit.gupta |
113 |
}
|
| 35380 |
amit |
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Cleanup method to prevent memory leaks on shutdown.
|
|
|
117 |
* Deregisters JDBC drivers and stops MySQL cleanup thread.
|
|
|
118 |
*/
|
|
|
119 |
@PreDestroy
|
|
|
120 |
public void cleanup() {
|
|
|
121 |
LOGGER.info("WebDBContextConfigure cleanup started...");
|
|
|
122 |
|
|
|
123 |
// Stop MySQL AbandonedConnectionCleanupThread
|
|
|
124 |
try {
|
|
|
125 |
AbandonedConnectionCleanupThread.checkedShutdown();
|
|
|
126 |
LOGGER.info("MySQL AbandonedConnectionCleanupThread stopped");
|
|
|
127 |
} catch (Exception e) {
|
|
|
128 |
LOGGER.error("Error stopping MySQL cleanup thread", e);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
// Deregister JDBC drivers loaded by this webapp's classloader
|
|
|
132 |
ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
|
|
133 |
Enumeration<Driver> drivers = DriverManager.getDrivers();
|
|
|
134 |
while (drivers.hasMoreElements()) {
|
|
|
135 |
Driver driver = drivers.nextElement();
|
|
|
136 |
if (driver.getClass().getClassLoader() == cl) {
|
|
|
137 |
try {
|
|
|
138 |
DriverManager.deregisterDriver(driver);
|
|
|
139 |
LOGGER.info("Deregistered JDBC driver: {}", driver);
|
|
|
140 |
} catch (SQLException e) {
|
|
|
141 |
LOGGER.error("Error deregistering JDBC driver {}", driver, e);
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
LOGGER.info("WebDBContextConfigure cleanup completed");
|
|
|
147 |
}
|
| 34686 |
amit.gupta |
148 |
}
|