Project

General

Profile

1
package eu.dnetlib.openaire.user.store;
2

    
3
import org.apache.commons.dbcp.BasicDataSource;
4
import org.apache.log4j.Logger;
5

    
6
import javax.sql.DataSource;
7
import java.sql.Connection;
8
import java.sql.DriverManager;
9
import java.sql.SQLException;
10

    
11
/**
12
 * Created by sofia on 31/10/2016.
13
 */
14
public class DataSourceConnector {
15

    
16
    private String username;
17
    private String password;
18
    private String dbUrl;
19
    private String driver;
20

    
21
    private static boolean driverLoad = false;
22
    private DataSource datasource;
23

    
24
    private static final Logger logger = Logger.getLogger(DataSourceConnector.class);
25

    
26
    public void init() {
27
        logger.info("Initializing data source");
28

    
29
        try {
30
            Class.forName(driver);
31
            driverLoad = true;
32

    
33
            // Test Connection
34
            getConnection().close();
35

    
36
            BasicDataSource bds = new BasicDataSource();
37

    
38
            bds.setDriverClassName(driver);
39
            bds.setUrl(dbUrl);
40
            bds.setUsername(username);
41
            bds.setPassword(password);
42
            bds.setMaxIdle(10);
43
            bds.setMaxActive(100);
44
            bds.setMaxWait(10000);
45
            bds.setValidationQuery("select 1");
46
            bds.setTestOnBorrow(true);
47
            bds.setTestWhileIdle(true);
48
            bds.setTimeBetweenEvictionRunsMillis(1200000);
49
            bds.setMinEvictableIdleTimeMillis(1800000);
50
            bds.setNumTestsPerEvictionRun(5);
51
            bds.setDefaultAutoCommit(true);
52

    
53
            this.datasource = bds;
54

    
55
        } catch (ClassNotFoundException | SQLException exc) {
56
            throw new RuntimeException(exc);
57
        }
58
    }
59

    
60
    public Connection getConnection() throws SQLException {
61
        return DriverManager.getConnection(dbUrl, username, password);
62
        //return DriverManager.getConnection("jdbc:postgresql://localhost:5433/MigrationUser?user=postgres&password=mypassword");
63
    }
64

    
65
    public String getUsername() {
66
        return username;
67
    }
68

    
69
    public void setUsername(String username) {
70
        this.username = username;
71
    }
72

    
73
    public String getPassword() {
74
        return password;
75
    }
76

    
77
    public void setPassword(String password) {
78
        this.password = password;
79
    }
80

    
81
    public String getDbUrl() {
82
        return dbUrl;
83
    }
84

    
85
    public void setDbUrl(String dbUrl) {
86
        this.dbUrl = dbUrl;
87
    }
88

    
89
    public String getDriver() {
90
        return driver;
91
    }
92

    
93
    public void setDriver(String driver) {
94
        this.driver = driver;
95
    }
96

    
97
    public DataSource getDatasource() {
98
        return datasource;
99
    }
100
}
(2-2/4)