| 21749 |
ashik.ali |
1 |
package com.spice.profitmandi.dao.config;
|
|
|
2 |
|
|
|
3 |
import java.io.IOException;
|
|
|
4 |
import java.util.Properties;
|
|
|
5 |
|
|
|
6 |
import javax.sql.DataSource;
|
|
|
7 |
|
|
|
8 |
import org.hibernate.SessionFactory;
|
|
|
9 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
10 |
import org.springframework.context.annotation.Bean;
|
|
|
11 |
import org.springframework.context.annotation.ComponentScan;
|
|
|
12 |
import org.springframework.context.annotation.Configuration;
|
|
|
13 |
import org.springframework.core.io.Resource;
|
|
|
14 |
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
|
|
15 |
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
|
|
16 |
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
|
|
|
17 |
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
18 |
|
|
|
19 |
import com.spice.profitmandi.web.config.AppConfig;
|
|
|
20 |
|
|
|
21 |
@Configuration
|
|
|
22 |
@EnableTransactionManagement
|
|
|
23 |
@ComponentScan({ "com.spice.profitmandi.*" })
|
|
|
24 |
public class WebDBContextConfigure{
|
|
|
25 |
|
|
|
26 |
private static final String HIBERNATE_DRIVER_CLASS = "hibernate.driver.class";
|
|
|
27 |
private static final String HIBERNATE_URL = "hibernate.url";
|
|
|
28 |
private static final String HIBERNATE_USER_NAME = "hibernate.user.name";
|
|
|
29 |
private static final String HIBERNATE_PASSWORD = "hibernate.password";
|
|
|
30 |
private static final String HIBERNATE_DIALECT = "hibernate.dialect";
|
|
|
31 |
private static final String HIBERNATE_SHOW_SQL = "hibernate.show_sql";
|
|
|
32 |
private static final String HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
|
|
|
33 |
private static final String HIBERNATE_JDBC_BATCH_SIZE = "hibernate.jdbc.batch_size";
|
|
|
34 |
private static final String HIBERNATE_C3P0_MIN_SIZE = "hibernate.c3p0.min_size";
|
|
|
35 |
private static final String HIBERNATE_C3P0_MAX_SIZE = "hibernate.c3p0.max_size";
|
|
|
36 |
private static final String HIBERNATE_C3P0_TIMEOUT= "hibernate.c3p0.timeout";
|
|
|
37 |
private static final String HIBERNATE_C3P0_MAX_STATEMENTS = "hibernate.c3p0.max_statements";
|
|
|
38 |
private static final String HIBERNATE_C3P0_IDLE_TEST_PERIOD = "hibernate.c3p0.idle_test_period";
|
|
|
39 |
private Resource resource = AppConfig.getResource();
|
|
|
40 |
|
|
|
41 |
@Bean(name = "dataSource")
|
|
|
42 |
public DataSource dataSource() {
|
|
|
43 |
Properties properties = new Properties();
|
|
|
44 |
try {
|
|
|
45 |
properties.load(resource.getInputStream());
|
|
|
46 |
} catch (IOException e) {
|
|
|
47 |
e.printStackTrace();
|
|
|
48 |
}
|
|
|
49 |
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
|
|
50 |
dataSource.setDriverClassName(properties.getProperty(HIBERNATE_DRIVER_CLASS));
|
|
|
51 |
dataSource.setUrl(properties.getProperty(HIBERNATE_URL));
|
|
|
52 |
dataSource.setUsername(properties.getProperty(HIBERNATE_USER_NAME));
|
|
|
53 |
dataSource.setPassword(properties.getProperty(HIBERNATE_PASSWORD));
|
|
|
54 |
return dataSource;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
@Bean
|
|
|
58 |
public Properties getHibernateProperties() {
|
|
|
59 |
Properties dbProperties = new Properties();
|
|
|
60 |
Properties properties = new Properties();
|
|
|
61 |
try {
|
|
|
62 |
properties.load(resource.getInputStream());
|
|
|
63 |
} catch (IOException e) {
|
|
|
64 |
e.printStackTrace();
|
|
|
65 |
}
|
|
|
66 |
dbProperties.put(HIBERNATE_DIALECT, properties.getProperty(HIBERNATE_DIALECT));
|
|
|
67 |
dbProperties.put(HIBERNATE_SHOW_SQL, properties.getProperty(HIBERNATE_SHOW_SQL));
|
|
|
68 |
dbProperties.put(HIBERNATE_FORMAT_SQL, properties.getProperty(HIBERNATE_FORMAT_SQL));
|
|
|
69 |
dbProperties.put(HIBERNATE_JDBC_BATCH_SIZE, properties.getProperty(HIBERNATE_JDBC_BATCH_SIZE));
|
|
|
70 |
dbProperties.put(HIBERNATE_C3P0_MIN_SIZE, properties.getProperty(HIBERNATE_C3P0_MIN_SIZE));
|
|
|
71 |
dbProperties.put(HIBERNATE_C3P0_MAX_SIZE, properties.getProperty(HIBERNATE_C3P0_MAX_SIZE));
|
|
|
72 |
dbProperties.put(HIBERNATE_C3P0_TIMEOUT, properties.getProperty(HIBERNATE_C3P0_TIMEOUT));
|
|
|
73 |
dbProperties.put(HIBERNATE_C3P0_MAX_STATEMENTS, properties.getProperty(HIBERNATE_C3P0_MAX_STATEMENTS));
|
|
|
74 |
dbProperties.put(HIBERNATE_C3P0_IDLE_TEST_PERIOD, properties.getProperty(HIBERNATE_C3P0_IDLE_TEST_PERIOD));
|
|
|
75 |
return dbProperties;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
@Autowired
|
|
|
79 |
@Bean(name = "sessionFactory")
|
|
|
80 |
public SessionFactory getSessionFactory(DataSource dataSource) {
|
|
|
81 |
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
|
|
|
82 |
sessionBuilder.addProperties(getHibernateProperties());
|
|
|
83 |
sessionBuilder.scanPackages("com.spice.profitmandi.dao.*");
|
|
|
84 |
return sessionBuilder.buildSessionFactory();
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
@Autowired
|
|
|
88 |
@Bean(name = "transactionManager")
|
|
|
89 |
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
|
|
|
90 |
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
|
|
|
91 |
return transactionManager;
|
|
|
92 |
}
|
|
|
93 |
}
|