Subversion Repositories SmartDukaan

Rev

Rev 23738 | Rev 23898 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
23723 amit.gupta 1
package com.smartdukaan.cron.config;
2
 
3
import java.util.Properties;
4
 
5
import javax.sql.DataSource;
6
 
23755 amit.gupta 7
import org.apache.logging.log4j.LogManager;
8
import org.apache.logging.log4j.Logger;
23723 amit.gupta 9
import org.hibernate.SessionFactory;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.beans.factory.annotation.Value;
12
import org.springframework.context.annotation.Bean;
23755 amit.gupta 13
import org.springframework.context.annotation.ComponentScan;
23723 amit.gupta 14
import org.springframework.context.annotation.Configuration;
15
import org.springframework.context.annotation.PropertySource;
16
import org.springframework.jdbc.datasource.DriverManagerDataSource;
17
import org.springframework.orm.hibernate5.HibernateTransactionManager;
18
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
19
import org.springframework.transaction.annotation.EnableTransactionManagement;
20
 
23738 amit.gupta 21
import com.spice.profitmandi.dao.repository.dtr.Mongo;
22
 
23723 amit.gupta 23
@Configuration
24
@EnableTransactionManagement
23755 amit.gupta 25
@ComponentScan("com.spice.profitmandi.*")
23723 amit.gupta 26
@PropertySource("classpath:META-INF/env.properties")
27
public class DBConfig {
28
 
23755 amit.gupta 29
	private static final Logger LOGGER=LogManager.getLogger(DBConfig.class);
23723 amit.gupta 30
 
23755 amit.gupta 31
 
32
	private static final String HIBERNATE_DIALECT = "hibernate.dialect";
23723 amit.gupta 33
 
23755 amit.gupta 34
	private static final String HIBERNATE_SHOW_SQL = "hibernate.show_sql";
23723 amit.gupta 35
 
23755 amit.gupta 36
	private static final String HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
23723 amit.gupta 37
 
23755 amit.gupta 38
	private static final String HIBERNATE_JDBC_BATCH_SIZE = "hibernate.jdbc.batch_size";
23723 amit.gupta 39
 
23755 amit.gupta 40
	private static final String HIBERNATE_C3P0_MIN_SIZE = "hibernate.c3p0.min_size";
23723 amit.gupta 41
 
23755 amit.gupta 42
	private static final String HIBERNATE_C3P0_MAX_SIZE = "hibernate.c3p0.max_size";
23723 amit.gupta 43
 
23755 amit.gupta 44
	private static final String HIBERNATE_C3P0_TIMEOUT = "hibernate.c3p0.timeout";
23723 amit.gupta 45
 
23755 amit.gupta 46
	private static final String HIBERNATE_C3P0_MAX_STATEMENTS = "hibernate.c3p0.max_statements";
23723 amit.gupta 47
 
23755 amit.gupta 48
	private static final String HIBERNATE_C3P0_IDLE_TEST_PERIOD = "hibernate.c3p0.idle_test_period";
49
 
23723 amit.gupta 50
	@Value("${hibernate.driver.class}")
51
	private String hibernateDriverClass;
52
 
53
	@Value("${hibernate.url}")
54
	private String hibernateUrl;
55
 
56
	@Value("${hibernate.user.name}")
57
	private String hibernateUserName;
58
 
59
	@Value("${hibernate.password}")
60
	private String hibernatePassword;
61
 
62
	@Value("${hibernate.dialect}")
63
	private String hibernateDialect;
64
 
65
	@Value("${hibernate.show_sql}")
66
	private String hibernateShowSql;
67
 
68
	@Value("${hibernate.format_sql}")
69
	private String hibernateFormatSql;
70
 
71
	@Value("${hibernate.jdbc.batch_size}")
72
	private String hibernateBatchSize;
73
 
74
	@Value("${hibernate.c3p0.min_size}")
75
	private String hibernateMinSize;
76
 
77
	@Value("${hibernate.c3p0.max_size}")
78
	private String hibernateMaxSize;
79
 
80
	@Value("${hibernate.c3p0.timeout}")
81
	private String hibernateTimeout;
82
 
83
	@Value("${hibernate.c3p0.max_statements}")
84
	private String hibernateMaxStatements;
85
 
86
	@Value("${hibernate.c3p0.idle_test_period}")
87
	private String hibernateIdleTestPeriod;
23738 amit.gupta 88
 
89
	@Value("${mongo.host}")
90
	private String mongoHost;
23723 amit.gupta 91
 
23738 amit.gupta 92
	@Value("${content.mongo.host}")
93
	private String contentMongoHost;
94
 
23723 amit.gupta 95
	@Bean(name = "dataSource")
96
	public DataSource dataSource() {
97
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
98
		dataSource.setDriverClassName(hibernateDriverClass);
99
		dataSource.setUrl(hibernateUrl);
100
		dataSource.setUsername(hibernateUserName);
101
		dataSource.setPassword(hibernatePassword);
23755 amit.gupta 102
		LOGGER.info("DataSource returned {}", dataSource);
23723 amit.gupta 103
		return dataSource;
104
	}
105
 
106
	@Bean
107
	public Properties getHibernateProperties() {
108
		Properties dbProperties = new Properties();
109
		dbProperties.put(HIBERNATE_DIALECT, hibernateDialect);
110
		dbProperties.put(HIBERNATE_SHOW_SQL, hibernateShowSql);
111
		dbProperties.put(HIBERNATE_FORMAT_SQL, hibernateFormatSql);
112
		dbProperties.put(HIBERNATE_JDBC_BATCH_SIZE, hibernateBatchSize);
113
		dbProperties.put(HIBERNATE_C3P0_MIN_SIZE, hibernateMinSize);
114
		dbProperties.put(HIBERNATE_C3P0_MAX_SIZE, hibernateMaxSize);
115
		dbProperties.put(HIBERNATE_C3P0_TIMEOUT, hibernateTimeout);
116
		dbProperties.put(HIBERNATE_C3P0_MAX_STATEMENTS, hibernateMaxStatements);
117
		dbProperties.put(HIBERNATE_C3P0_IDLE_TEST_PERIOD, hibernateIdleTestPeriod);
118
		return dbProperties;
119
	}
120
 
121
	@Autowired
122
	@Bean(name = "sessionFactory")
123
	public SessionFactory getSessionFactory(DataSource dataSource) {
124
		LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
125
		sessionBuilder.addProperties(getHibernateProperties());
126
		sessionBuilder.scanPackages("com.spice.profitmandi.dao.*");
127
		return sessionBuilder.buildSessionFactory();
128
	}
129
	@Autowired
130
	@Bean(name = "transactionManager")
131
	public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
132
		HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
133
		return transactionManager;
134
	}
23738 amit.gupta 135
 
136
	@Bean
137
	public Mongo mongoClient(SessionFactory sessionFactory) {
138
		return new Mongo(mongoHost, contentMongoHost);
139
	}
23723 amit.gupta 140
}