1
|
package eu.dnetlib.openaire.user.store;
|
2
|
|
3
|
import org.apache.commons.dbcp.BasicDataSource;
|
4
|
import org.apache.log4j.Logger;
|
5
|
import org.springframework.beans.factory.annotation.Autowired;
|
6
|
import org.springframework.beans.factory.annotation.Value;
|
7
|
import org.springframework.stereotype.Component;
|
8
|
|
9
|
import javax.annotation.PostConstruct;
|
10
|
import javax.sql.DataSource;
|
11
|
import java.sql.Connection;
|
12
|
import java.sql.DriverManager;
|
13
|
import java.sql.SQLException;
|
14
|
|
15
|
/**
|
16
|
* Created by sofia on 31/10/2016.
|
17
|
*/
|
18
|
@Component(value = "dataSourceConnector")
|
19
|
public class DataSourceConnector {
|
20
|
|
21
|
@Value("${openaire.users.db.username}")
|
22
|
private String username;
|
23
|
@Value("${openaire.users.db.password}")
|
24
|
private String password;
|
25
|
@Value("${openaire.users.db.url}")
|
26
|
private String dbUrl;
|
27
|
@Value("${openaire.users.db.driverClassName}")
|
28
|
private String driver;
|
29
|
|
30
|
private static boolean driverLoad = false;
|
31
|
|
32
|
private DataSource datasource;
|
33
|
|
34
|
private static final Logger logger = Logger.getLogger(DataSourceConnector.class);
|
35
|
|
36
|
@PostConstruct
|
37
|
public void init() {
|
38
|
logger.info("Initializing data source");
|
39
|
|
40
|
try {
|
41
|
Class.forName(driver);
|
42
|
driverLoad = true;
|
43
|
|
44
|
BasicDataSource bds = new BasicDataSource();
|
45
|
|
46
|
bds.setDriverClassName(driver);
|
47
|
bds.setUrl(dbUrl);
|
48
|
bds.setUsername(username);
|
49
|
bds.setPassword(password);
|
50
|
bds.setMaxIdle(10);
|
51
|
bds.setMaxActive(100);
|
52
|
bds.setMaxWait(10000);
|
53
|
bds.setValidationQuery("select 1");
|
54
|
bds.setTestOnBorrow(true);
|
55
|
bds.setTestWhileIdle(true);
|
56
|
bds.setTimeBetweenEvictionRunsMillis(1200000);
|
57
|
bds.setMinEvictableIdleTimeMillis(1800000);
|
58
|
bds.setNumTestsPerEvictionRun(5);
|
59
|
bds.setDefaultAutoCommit(true);
|
60
|
|
61
|
this.datasource = bds;
|
62
|
|
63
|
// Test Connection
|
64
|
getConnection().close();
|
65
|
logger.info("Datasource " + datasource);
|
66
|
logger.info(datasource.getConnection());
|
67
|
|
68
|
} catch (ClassNotFoundException | SQLException exc) {
|
69
|
logger.error("Unable to connenct to the DB. ", exc);
|
70
|
throw new RuntimeException(exc);
|
71
|
}
|
72
|
}
|
73
|
|
74
|
public Connection getConnection() throws SQLException {
|
75
|
logger.debug("dbUrl " + dbUrl);
|
76
|
return DriverManager.getConnection(dbUrl, username, password);
|
77
|
//return DriverManager.getConnection("jdbc:postgresql://localhost:5433/MigrationUser?user=postgres&password=mypassword");
|
78
|
}
|
79
|
|
80
|
public String getUsername() {
|
81
|
return username;
|
82
|
}
|
83
|
|
84
|
public void setUsername(String username) {
|
85
|
this.username = username;
|
86
|
}
|
87
|
|
88
|
public String getPassword() {
|
89
|
return password;
|
90
|
}
|
91
|
|
92
|
public void setPassword(String password) {
|
93
|
this.password = password;
|
94
|
}
|
95
|
|
96
|
public String getDbUrl() {
|
97
|
return dbUrl;
|
98
|
}
|
99
|
|
100
|
public void setDbUrl(String dbUrl) {
|
101
|
this.dbUrl = dbUrl;
|
102
|
}
|
103
|
|
104
|
public String getDriver() {
|
105
|
return driver;
|
106
|
}
|
107
|
|
108
|
public void setDriver(String driver) {
|
109
|
this.driver = driver;
|
110
|
}
|
111
|
|
112
|
public DataSource getDatasource() {
|
113
|
return datasource;
|
114
|
}
|
115
|
}
|