Revision 45071
Added by Sofia Baltzi over 7 years ago
modules/uoa-user-management/trunk/src/main/java/eu/dnetlib/openaire/user/store/JDBCConnector.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.store; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.io.InputStream; |
|
5 |
import java.sql.Connection; |
|
6 |
import java.sql.DriverManager; |
|
7 |
import java.sql.SQLException; |
|
8 |
import java.util.Properties; |
|
9 |
import org.apache.commons.dbcp.BasicDataSource; |
|
10 |
|
|
11 |
|
|
12 |
/** |
|
13 |
* Created by sofia on 5/8/2016. |
|
14 |
*/ |
|
15 |
public class JDBCConnector { |
|
16 |
|
|
17 |
private final static JDBCConnector INSTANCE = new JDBCConnector(); |
|
18 |
|
|
19 |
public static synchronized JDBCConnector v() { return INSTANCE; } |
|
20 |
|
|
21 |
private final String username; |
|
22 |
private final String password; |
|
23 |
private final String dbUrl; |
|
24 |
private final String driver; |
|
25 |
|
|
26 |
private JDBCConnector() |
|
27 |
{ |
|
28 |
try { |
|
29 |
// Read properties file |
|
30 |
InputStream stream = getClass().getClassLoader() |
|
31 |
.getResourceAsStream("src/main/resources/config/postgresql.db.properties"); |
|
32 |
|
|
33 |
Properties prop = new Properties(); |
|
34 |
prop.load(stream); |
|
35 |
|
|
36 |
driver = prop.getProperty("driver"); |
|
37 |
dbUrl = prop.getProperty("url"); |
|
38 |
username = prop.getProperty("username"); |
|
39 |
password = prop.getProperty("password"); |
|
40 |
|
|
41 |
// Test for Driver's existence |
|
42 |
Class.forName(driver); |
|
43 |
|
|
44 |
// Test Connection |
|
45 |
newConnection().close(); |
|
46 |
|
|
47 |
BasicDataSource bds = new BasicDataSource(); |
|
48 |
|
|
49 |
bds.setDriverClassName(driver); |
|
50 |
bds.setUrl(dbUrl); |
|
51 |
bds.setUsername(username); |
|
52 |
bds.setPassword(password); |
|
53 |
|
|
54 |
bds.setMaxIdle(10); |
|
55 |
bds.setMaxActive(100); |
|
56 |
bds.setMaxWait(10000); |
|
57 |
bds.setValidationQuery("select 1"); |
|
58 |
bds.setTestOnBorrow(true); |
|
59 |
bds.setTestWhileIdle(true); |
|
60 |
bds.setTimeBetweenEvictionRunsMillis(1200000); |
|
61 |
bds.setMinEvictableIdleTimeMillis(1800000); |
|
62 |
bds.setNumTestsPerEvictionRun(5); |
|
63 |
bds.setDefaultAutoCommit(false); |
|
64 |
|
|
65 |
} catch (ClassNotFoundException | IOException | SQLException exc) { |
|
66 |
throw new RuntimeException(exc); |
|
67 |
} |
|
68 |
} |
|
69 |
|
|
70 |
private Connection newConnection() |
|
71 |
throws SQLException |
|
72 |
{ |
|
73 |
return DriverManager.getConnection(dbUrl, username, password); |
|
74 |
} |
|
75 |
|
|
76 |
|
|
77 |
} |
Also available in: Unified diff
Delete JDBCConnector