| 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;
|
| 21649 |
amit.gupta |
14 |
import org.hibernate.SessionFactory;
|
| 23568 |
govind |
15 |
import org.apache.logging.log4j.Logger;
|
|
|
16 |
import org.apache.logging.log4j.LogManager;
|
| 21649 |
amit.gupta |
17 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
18 |
import org.springframework.context.annotation.Bean;
|
|
|
19 |
import org.springframework.context.annotation.ComponentScan;
|
|
|
20 |
import org.springframework.context.annotation.Configuration;
|
|
|
21 |
import org.springframework.core.io.Resource;
|
|
|
22 |
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
|
|
23 |
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
|
|
24 |
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
|
|
|
25 |
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
| 22173 |
amit.gupta |
26 |
import com.spice.profitmandi.dao.repository.dtr.Mongo;
|
| 21649 |
amit.gupta |
27 |
|
|
|
28 |
import com.spice.profitmandi.web.config.AppConfig;
|
|
|
29 |
|
| 23568 |
govind |
30 |
|
| 21649 |
amit.gupta |
31 |
@Configuration
|
|
|
32 |
@EnableTransactionManagement
|
|
|
33 |
@ComponentScan({ "com.spice.profitmandi.*" })
|
| 35383 |
amit |
34 |
public class WebDBContextConfigure {
|
|
|
35 |
|
|
|
36 |
private static final Logger LOGGER = LogManager.getLogger(WebDBContextConfigure.class);
|
| 21649 |
amit.gupta |
37 |
|
| 23568 |
govind |
38 |
|
|
|
39 |
|
|
|
40 |
|
| 21649 |
amit.gupta |
41 |
private static final String HIBERNATE_DRIVER_CLASS = "hibernate.driver.class";
|
|
|
42 |
private static final String HIBERNATE_URL = "hibernate.url";
|
|
|
43 |
private static final String HIBERNATE_USER_NAME = "hibernate.user.name";
|
|
|
44 |
private static final String HIBERNATE_PASSWORD = "hibernate.password";
|
|
|
45 |
private static final String HIBERNATE_DIALECT = "hibernate.dialect";
|
|
|
46 |
private static final String HIBERNATE_SHOW_SQL = "hibernate.show_sql";
|
|
|
47 |
private static final String HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
|
|
|
48 |
private static final String HIBERNATE_JDBC_BATCH_SIZE = "hibernate.jdbc.batch_size";
|
|
|
49 |
private static final String HIBERNATE_C3P0_MIN_SIZE = "hibernate.c3p0.min_size";
|
|
|
50 |
private static final String HIBERNATE_C3P0_MAX_SIZE = "hibernate.c3p0.max_size";
|
|
|
51 |
private static final String HIBERNATE_C3P0_TIMEOUT= "hibernate.c3p0.timeout";
|
|
|
52 |
private static final String HIBERNATE_C3P0_MAX_STATEMENTS = "hibernate.c3p0.max_statements";
|
|
|
53 |
private static final String HIBERNATE_C3P0_IDLE_TEST_PERIOD = "hibernate.c3p0.idle_test_period";
|
|
|
54 |
private Resource resource = AppConfig.getResource();
|
|
|
55 |
|
|
|
56 |
@Bean(name = "dataSource")
|
|
|
57 |
public DataSource dataSource() {
|
|
|
58 |
Properties properties = new Properties();
|
|
|
59 |
try {
|
|
|
60 |
properties.load(resource.getInputStream());
|
|
|
61 |
} catch (IOException e) {
|
|
|
62 |
e.printStackTrace();
|
|
|
63 |
}
|
|
|
64 |
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
|
|
65 |
dataSource.setDriverClassName(properties.getProperty(HIBERNATE_DRIVER_CLASS));
|
|
|
66 |
dataSource.setUrl(properties.getProperty(HIBERNATE_URL));
|
|
|
67 |
dataSource.setUsername(properties.getProperty(HIBERNATE_USER_NAME));
|
|
|
68 |
dataSource.setPassword(properties.getProperty(HIBERNATE_PASSWORD));
|
|
|
69 |
return dataSource;
|
|
|
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 |
}
|
|
|
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));
|
|
|
85 |
dbProperties.put(HIBERNATE_C3P0_MIN_SIZE, properties.getProperty(HIBERNATE_C3P0_MIN_SIZE));
|
|
|
86 |
dbProperties.put(HIBERNATE_C3P0_MAX_SIZE, properties.getProperty(HIBERNATE_C3P0_MAX_SIZE));
|
|
|
87 |
dbProperties.put(HIBERNATE_C3P0_TIMEOUT, properties.getProperty(HIBERNATE_C3P0_TIMEOUT));
|
|
|
88 |
dbProperties.put(HIBERNATE_C3P0_MAX_STATEMENTS, properties.getProperty(HIBERNATE_C3P0_MAX_STATEMENTS));
|
|
|
89 |
dbProperties.put(HIBERNATE_C3P0_IDLE_TEST_PERIOD, properties.getProperty(HIBERNATE_C3P0_IDLE_TEST_PERIOD));
|
|
|
90 |
return dbProperties;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
@Autowired
|
|
|
94 |
@Bean(name = "sessionFactory")
|
|
|
95 |
public SessionFactory getSessionFactory(DataSource dataSource) {
|
|
|
96 |
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
|
|
|
97 |
sessionBuilder.addProperties(getHibernateProperties());
|
|
|
98 |
sessionBuilder.scanPackages("com.spice.profitmandi.dao.*");
|
|
|
99 |
return sessionBuilder.buildSessionFactory();
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
@Autowired
|
|
|
103 |
@Bean(name = "transactionManager")
|
|
|
104 |
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
|
|
|
105 |
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
|
|
|
106 |
return transactionManager;
|
|
|
107 |
}
|
| 22173 |
amit.gupta |
108 |
|
| 35383 |
amit |
109 |
@Bean(destroyMethod = "close")
|
| 22355 |
ashik.ali |
110 |
public Mongo mongoClient() {
|
| 22173 |
amit.gupta |
111 |
Properties properties = new Properties();
|
|
|
112 |
try {
|
|
|
113 |
properties.load(resource.getInputStream());
|
|
|
114 |
} catch (IOException e) {
|
|
|
115 |
e.printStackTrace();
|
|
|
116 |
}
|
|
|
117 |
return new Mongo(properties.getProperty("mongo.host"), properties.getProperty("content.mongo.host"));
|
|
|
118 |
}
|
| 35383 |
amit |
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Cleanup method to prevent memory leaks on shutdown.
|
|
|
122 |
* Deregisters JDBC drivers and stops MySQL cleanup thread.
|
|
|
123 |
*/
|
|
|
124 |
@PreDestroy
|
|
|
125 |
public void cleanup() {
|
|
|
126 |
LOGGER.info("WebDBContextConfigure cleanup started...");
|
|
|
127 |
|
|
|
128 |
// Stop MySQL AbandonedConnectionCleanupThread
|
|
|
129 |
try {
|
|
|
130 |
AbandonedConnectionCleanupThread.checkedShutdown();
|
|
|
131 |
LOGGER.info("MySQL AbandonedConnectionCleanupThread stopped");
|
|
|
132 |
} catch (Exception e) {
|
|
|
133 |
LOGGER.error("Error stopping MySQL cleanup thread", e);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
// Deregister JDBC drivers loaded by this webapp's classloader
|
|
|
137 |
ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
|
|
138 |
Enumeration<Driver> drivers = DriverManager.getDrivers();
|
|
|
139 |
while (drivers.hasMoreElements()) {
|
|
|
140 |
Driver driver = drivers.nextElement();
|
|
|
141 |
if (driver.getClass().getClassLoader() == cl) {
|
|
|
142 |
try {
|
|
|
143 |
DriverManager.deregisterDriver(driver);
|
|
|
144 |
LOGGER.info("Deregistered JDBC driver: {}", driver);
|
|
|
145 |
} catch (SQLException e) {
|
|
|
146 |
LOGGER.error("Error deregistering JDBC driver {}", driver, e);
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
LOGGER.info("WebDBContextConfigure cleanup completed");
|
|
|
152 |
}
|
| 21649 |
amit.gupta |
153 |
}
|