|
1
|
|
|
/* |
|
2
|
|
|
* This file is part of Araknemu. |
|
3
|
|
|
* |
|
4
|
|
|
* Araknemu is free software: you can redistribute it and/or modify |
|
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
|
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
7
|
|
|
* (at your option) any later version. |
|
8
|
|
|
* |
|
9
|
|
|
* Araknemu is distributed in the hope that it will be useful, |
|
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12
|
|
|
* GNU Lesser General Public License for more details. |
|
13
|
|
|
* |
|
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
15
|
|
|
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>. |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright (c) 2017-2020 Vincent Quatrevieux |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
package fr.quatrevieux.araknemu.core.dbal; |
|
21
|
|
|
|
|
22
|
|
|
import org.apache.logging.log4j.Logger; |
|
23
|
|
|
|
|
24
|
|
|
import java.sql.Connection; |
|
25
|
|
|
import java.sql.SQLException; |
|
26
|
|
|
import java.sql.SQLRecoverableException; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Add auto-reconnect on connection pool |
|
30
|
|
|
*/ |
|
31
|
|
|
public final class AutoReconnectConnectionPool implements ConnectionPool { |
|
32
|
|
|
private final ConnectionPool pool; |
|
33
|
|
|
private final Logger logger; |
|
34
|
|
|
|
|
35
|
1 |
|
public AutoReconnectConnectionPool(ConnectionPool pool, Logger logger) { |
|
36
|
1 |
|
this.pool = pool; |
|
37
|
1 |
|
this.logger = logger; |
|
38
|
1 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
@Override |
|
41
|
|
|
public void initialize() throws SQLException { |
|
42
|
1 |
|
pool.initialize(); |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
@Override |
|
46
|
|
|
public Connection acquire() throws SQLException { |
|
47
|
1 |
|
return pool.acquire(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
@Override |
|
51
|
|
|
public void release(Connection connection) { |
|
52
|
1 |
|
pool.release(connection); |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
@Override |
|
56
|
|
|
public int size() { |
|
57
|
1 |
|
return pool.size(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
@Override |
|
61
|
|
|
public <T> T execute(Task<T> task) throws SQLException { |
|
62
|
1 |
|
SQLException lastError = null; |
|
63
|
|
|
|
|
64
|
1 |
|
for (int i = pool.size(); i >= 0; --i) { |
|
65
|
|
|
try { |
|
66
|
1 |
|
return pool.execute(task); |
|
67
|
1 |
|
} catch (SQLException e) { |
|
68
|
1 |
|
if (!causedByConnectionClosed(e)) { |
|
69
|
1 |
|
throw e; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
logger.warn("Recoverable SQL error occurs. Retry on a new connection"); |
|
73
|
1 |
|
lastError = e; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
throw new SQLException("Max retry limit reached", lastError); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
@Override |
|
81
|
|
|
public void close() throws Exception { |
|
82
|
1 |
|
pool.close(); |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Check if the exception is caused by a closed connection |
|
87
|
|
|
*/ |
|
88
|
|
|
private boolean causedByConnectionClosed(SQLException exception) { |
|
89
|
1 |
|
return exception instanceof SQLRecoverableException |
|
90
|
1 |
|
|| (exception.getMessage() != null && exception.getMessage().toLowerCase().contains("connection closed")) |
|
91
|
|
|
; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|