| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.dao.config;
|
1 |
package com.spice.profitmandi.dao.config;
|
| 2 |
|
2 |
|
| - |
|
3 |
import com.mysql.cj.jdbc.AbandonedConnectionCleanupThread;
|
| 3 |
import com.spice.profitmandi.dao.repository.dtr.Mongo;
|
4 |
import com.spice.profitmandi.dao.repository.dtr.Mongo;
|
| 4 |
import com.spice.profitmandi.web.config.AppConfig;
|
5 |
import com.spice.profitmandi.web.config.AppConfig;
|
| 5 |
import com.zaxxer.hikari.HikariConfig;
|
6 |
import com.zaxxer.hikari.HikariConfig;
|
| 6 |
import com.zaxxer.hikari.HikariDataSource;
|
7 |
import com.zaxxer.hikari.HikariDataSource;
|
| - |
|
8 |
import org.apache.logging.log4j.LogManager;
|
| - |
|
9 |
import org.apache.logging.log4j.Logger;
|
| 7 |
import org.hibernate.SessionFactory;
|
10 |
import org.hibernate.SessionFactory;
|
| 8 |
import org.springframework.context.annotation.Bean;
|
11 |
import org.springframework.context.annotation.Bean;
|
| 9 |
import org.springframework.context.annotation.ComponentScan;
|
12 |
import org.springframework.context.annotation.ComponentScan;
|
| 10 |
import org.springframework.context.annotation.Configuration;
|
13 |
import org.springframework.context.annotation.Configuration;
|
| 11 |
import org.springframework.context.annotation.Primary;
|
14 |
import org.springframework.context.annotation.Primary;
|
| 12 |
import org.springframework.core.io.Resource;
|
15 |
import org.springframework.core.io.Resource;
|
| 13 |
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
16 |
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
| 14 |
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
|
17 |
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
|
| 15 |
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
18 |
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
| 16 |
|
19 |
|
| - |
|
20 |
import javax.annotation.PreDestroy;
|
| 17 |
import javax.sql.DataSource;
|
21 |
import javax.sql.DataSource;
|
| 18 |
import java.io.IOException;
|
22 |
import java.io.IOException;
|
| - |
|
23 |
import java.sql.Driver;
|
| - |
|
24 |
import java.sql.DriverManager;
|
| - |
|
25 |
import java.sql.SQLException;
|
| - |
|
26 |
import java.util.Enumeration;
|
| 19 |
import java.util.Properties;
|
27 |
import java.util.Properties;
|
| 20 |
|
28 |
|
| 21 |
@Configuration
|
29 |
@Configuration
|
| 22 |
@EnableTransactionManagement
|
30 |
@EnableTransactionManagement
|
| 23 |
@ComponentScan({ "com.spice.profitmandi.dao.*" })
|
31 |
@ComponentScan({ "com.spice.profitmandi.dao.*" })
|
| 24 |
public class WebDBContextConfigure {
|
32 |
public class WebDBContextConfigure {
|
| 25 |
|
33 |
|
| - |
|
34 |
private static final Logger LOGGER = LogManager.getLogger(WebDBContextConfigure.class);
|
| - |
|
35 |
|
| 26 |
private static final String HIBERNATE_DRIVER_CLASS = "hibernate.driver.class";
|
36 |
private static final String HIBERNATE_DRIVER_CLASS = "hibernate.driver.class";
|
| 27 |
private static final String HIBERNATE_URL = "hibernate.url";
|
37 |
private static final String HIBERNATE_URL = "hibernate.url";
|
| 28 |
private static final String HIBERNATE_USER_NAME = "hibernate.user.name";
|
38 |
private static final String HIBERNATE_USER_NAME = "hibernate.user.name";
|
| 29 |
private static final String HIBERNATE_PASSWORD = "hibernate.password";
|
39 |
private static final String HIBERNATE_PASSWORD = "hibernate.password";
|
| 30 |
private static final String HIKARI_MAX_POOL_SIZE = "hikari.maximumPoolSize";
|
40 |
private static final String HIKARI_MAX_POOL_SIZE = "hikari.maximumPoolSize";
|
| Line 33... |
Line 43... |
| 33 |
private static final String HIKARI_MAX_LIFETIME = "hikari.maxLifetime";
|
43 |
private static final String HIKARI_MAX_LIFETIME = "hikari.maxLifetime";
|
| 34 |
private static final String HIKARI_CONNECTION_TIMEOUT = "hikari.connectionTimeout";
|
44 |
private static final String HIKARI_CONNECTION_TIMEOUT = "hikari.connectionTimeout";
|
| 35 |
|
45 |
|
| 36 |
private Resource resource = AppConfig.getResource();
|
46 |
private Resource resource = AppConfig.getResource();
|
| 37 |
|
47 |
|
| 38 |
@Bean(name = "dataSource")
|
48 |
@Bean(name = "dataSource", destroyMethod = "close")
|
| 39 |
public DataSource dataSource() {
|
49 |
public DataSource dataSource() {
|
| 40 |
Properties properties = new Properties();
|
50 |
Properties properties = new Properties();
|
| 41 |
try {
|
51 |
try {
|
| 42 |
properties.load(resource.getInputStream());
|
52 |
properties.load(resource.getInputStream());
|
| 43 |
} catch (IOException e) {
|
53 |
} catch (IOException e) {
|
| Line 88... |
Line 98... |
| 88 |
@Bean(name = "transactionManager")
|
98 |
@Bean(name = "transactionManager")
|
| 89 |
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
|
99 |
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
|
| 90 |
return new HibernateTransactionManager(sessionFactory);
|
100 |
return new HibernateTransactionManager(sessionFactory);
|
| 91 |
}
|
101 |
}
|
| 92 |
|
102 |
|
| 93 |
@Bean
|
103 |
@Bean(destroyMethod = "close")
|
| 94 |
public Mongo mongoClient(SessionFactory sessionFactory) {
|
104 |
public Mongo mongoClient(SessionFactory sessionFactory) {
|
| 95 |
Properties properties = new Properties();
|
105 |
Properties properties = new Properties();
|
| 96 |
try {
|
106 |
try {
|
| 97 |
properties.load(resource.getInputStream());
|
107 |
properties.load(resource.getInputStream());
|
| 98 |
} catch (IOException e) {
|
108 |
} catch (IOException e) {
|
| 99 |
e.printStackTrace();
|
109 |
e.printStackTrace();
|
| 100 |
}
|
110 |
}
|
| 101 |
return new Mongo(properties.getProperty("content.mongo.host"),
|
111 |
return new Mongo(properties.getProperty("content.mongo.host"),
|
| 102 |
properties.getProperty("content.mongo.host"));
|
112 |
properties.getProperty("content.mongo.host"));
|
| 103 |
}
|
113 |
}
|
| - |
|
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 |
}
|
| 104 |
}
|
148 |
}
|
| 105 |
|
149 |
|