1
|
|
|
/* |
2
|
|
|
* Copyright 2014, Armenak Grigoryan, and individual contributors as indicated |
3
|
|
|
* by the @authors tag. See the copyright.txt in the distribution for a |
4
|
|
|
* full listing of individual contributors. |
5
|
|
|
* |
6
|
|
|
* This is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation; either version 2.1 of |
9
|
|
|
* the License, or (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This software is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14
|
|
|
* Lesser General Public License for more details. |
15
|
|
|
*/ |
16
|
|
|
package com.strider.datadefender.database; |
17
|
|
|
|
18
|
|
|
import com.strider.datadefender.DbConfig; |
19
|
|
|
import com.strider.datadefender.utils.ISupplierWithException; |
20
|
|
|
|
21
|
|
|
import java.sql.Connection; |
22
|
|
|
import java.sql.SQLException; |
23
|
|
|
import java.sql.DriverManager; |
24
|
|
|
|
25
|
|
|
import org.apache.commons.lang3.StringUtils; |
26
|
|
|
|
27
|
|
|
import lombok.extern.log4j.Log4j2; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Handles standard database connections with username/password provided. |
31
|
|
|
*/ |
32
|
|
|
@Log4j2 |
33
|
|
|
public class DbConnection implements IDbConnection { |
34
|
|
|
|
35
|
|
|
protected final DbConfig config; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Default constructor, initializes config. |
39
|
|
|
* @param config |
40
|
|
|
*/ |
41
|
3 |
|
public DbConnection(final DbConfig config) { |
42
|
3 |
|
this.config = config; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Handles the actual creating of the connection, by running the supplier |
47
|
|
|
* method provided by subclasses. |
48
|
|
|
* |
49
|
|
|
* @param supplier |
50
|
|
|
* @return |
51
|
|
|
* @throws DatabaseException |
52
|
|
|
*/ |
53
|
3 |
|
protected Connection doConnect( |
54
|
|
|
final ISupplierWithException<Connection, SQLException> supplier |
55
|
|
|
) throws DatabaseException { |
56
|
|
|
|
57
|
3 |
|
Connection conn = null; |
58
|
3 |
|
try { |
59
|
3 |
|
log.info("Establishing database connection"); |
60
|
3 |
|
conn = supplier.get(); |
61
|
2 |
|
conn.setAutoCommit(false); |
62
|
|
|
} catch (SQLException e) { |
63
|
2 |
|
if (conn != null) { |
64
|
|
|
try { |
65
|
|
|
conn.close(); |
66
|
|
|
} catch (SQLException e2) { |
67
|
|
|
// ignore |
68
|
|
|
} |
69
|
|
|
} |
70
|
1 |
|
throw new DatabaseException(e.getMessage(), e); |
71
|
|
|
} |
72
|
2 |
|
return conn; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Default implementation calls DriverManager.getConnection with the |
77
|
|
|
* provided jdbc url, username and password. |
78
|
|
|
* |
79
|
|
|
* @return Connection |
80
|
|
|
* @throws DatabaseAnonymizerException |
81
|
|
|
*/ |
82
|
2 |
|
@Override |
83
|
|
|
public Connection connect() throws DatabaseException { |
84
|
2 |
|
return doConnect(() -> DriverManager.getConnection( |
85
|
|
|
config.getUrl(), |
86
|
|
|
StringUtils.trimToNull(config.getUsername()), |
87
|
|
|
StringUtils.trimToNull(config.getPassword()) |
88
|
|
|
)); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|