1
|
package eu.dnetlib.openaire.user.store;
|
2
|
|
3
|
import org.apache.commons.dbcp.BasicDataSource;
|
4
|
|
5
|
import javax.sql.DataSource;
|
6
|
import java.io.IOException;
|
7
|
import java.io.InputStream;
|
8
|
import java.sql.Connection;
|
9
|
import java.sql.DriverManager;
|
10
|
import java.sql.SQLException;
|
11
|
import java.util.Properties;
|
12
|
|
13
|
/**
|
14
|
* Created by sofia on 31/10/2016.
|
15
|
*/
|
16
|
public class DataSourceConnector
|
17
|
{
|
18
|
private final static DataSourceConnector INSTANCE = new DataSourceConnector();
|
19
|
|
20
|
public static synchronized DataSourceConnector v() { return INSTANCE; }
|
21
|
|
22
|
private final String username;
|
23
|
private final String password;
|
24
|
private final String dbUrl;
|
25
|
private final String driver;
|
26
|
private static boolean driverLoad = false;
|
27
|
|
28
|
private final DataSource datasource;
|
29
|
|
30
|
private DataSourceConnector()
|
31
|
{
|
32
|
try {
|
33
|
// Read properties file for database connection
|
34
|
InputStream streamSQL = getClass()
|
35
|
.getResourceAsStream("/config/migration2.postgresql.db.properties");
|
36
|
|
37
|
Properties propSQL = new Properties();
|
38
|
propSQL.load(streamSQL);
|
39
|
|
40
|
driver = propSQL.getProperty("openaire.db.driverClassName");
|
41
|
dbUrl = propSQL.getProperty("openaire.db.url");
|
42
|
username = propSQL.getProperty("openaire.db.username");
|
43
|
password = propSQL.getProperty("openaire.db.password");
|
44
|
|
45
|
// System.out.println("Username: " + username);
|
46
|
// System.out.println("password: " + password);
|
47
|
// System.out.println("driver: " + driver);
|
48
|
// System.out.println("Url: " + dbUrl);
|
49
|
|
50
|
// Test for Driver's existence
|
51
|
Class.forName(driver);
|
52
|
driverLoad=true;
|
53
|
|
54
|
// Test Connection
|
55
|
getConnection().close();
|
56
|
|
57
|
BasicDataSource bds = new BasicDataSource();
|
58
|
|
59
|
bds.setDriverClassName(driver);
|
60
|
bds.setUrl(dbUrl);
|
61
|
bds.setUsername(username);
|
62
|
bds.setPassword(password);
|
63
|
bds.setMaxIdle(10);
|
64
|
bds.setMaxActive(100);
|
65
|
bds.setMaxWait(10000);
|
66
|
bds.setValidationQuery("select 1");
|
67
|
bds.setTestOnBorrow(true);
|
68
|
bds.setTestWhileIdle(true);
|
69
|
bds.setTimeBetweenEvictionRunsMillis(1200000);
|
70
|
bds.setMinEvictableIdleTimeMillis(1800000);
|
71
|
bds.setNumTestsPerEvictionRun(5);
|
72
|
bds.setDefaultAutoCommit(true);
|
73
|
|
74
|
this.datasource = bds;
|
75
|
|
76
|
} catch (ClassNotFoundException | IOException | SQLException exc) {
|
77
|
throw new RuntimeException(exc);
|
78
|
}
|
79
|
}
|
80
|
|
81
|
public Connection getConnection()
|
82
|
throws SQLException
|
83
|
{
|
84
|
return DriverManager.getConnection(dbUrl, username, password);
|
85
|
//return DriverManager.getConnection("jdbc:postgresql://localhost:5433/MigrationUser?user=postgres&password=mypassword");
|
86
|
}
|
87
|
|
88
|
public DataSource getDatasource() {
|
89
|
return datasource;
|
90
|
}
|
91
|
|
92
|
public static void main(String[] args) throws SQLException {
|
93
|
v().getConnection().close();
|
94
|
}
|
95
|
}
|