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.SQLException; |
||
25 | import java.util.ArrayList; |
||
26 | import java.util.Collection; |
||
27 | import java.util.HashMap; |
||
28 | import java.util.Map; |
||
29 | |||
30 | /** |
||
31 | * Base database handler |
||
32 | */ |
||
33 | public final class DefaultDatabaseHandler implements DatabaseHandler { |
||
34 | private final DatabaseConfiguration configuration; |
||
35 | private final Map<String, Driver.Factory> factories; |
||
36 | private final Logger logger; |
||
37 | |||
38 | 1 | private final Map<String, ConnectionPool> connections = new HashMap<>(); |
|
39 | |||
40 | 1 | public DefaultDatabaseHandler(DatabaseConfiguration configuration, Logger logger, Map<String, Driver.Factory> factories) { |
|
41 | 1 | this.configuration = configuration; |
|
42 | 1 | this.factories = factories; |
|
43 | 1 | this.logger = logger; |
|
44 | 1 | } |
|
45 | |||
46 | public DefaultDatabaseHandler(DatabaseConfiguration configuration, Logger logger) { |
||
47 | 1 | this(configuration, logger, new HashMap<>()); |
|
48 | |||
49 | 1 | factory("sqlite", SQLiteDriver::new); |
|
50 | 1 | factory("mysql", MySQLDriver::new); |
|
51 | 1 | } |
|
52 | |||
53 | @Override |
||
54 | public ConnectionPool get(String name) throws SQLException { |
||
55 | 1 | ConnectionPool pool = connections.get(name); |
|
56 | |||
57 | 1 | if (pool != null) { |
|
58 | 1 | return pool; |
|
59 | } |
||
60 | |||
61 | 1 | final DatabaseConfiguration.Connection config = configuration.connection(name); |
|
62 | |||
63 | 1 | final Driver.Factory factory = factories.get(config.type()); |
|
64 | |||
65 | 1 | if (factory == null) { |
|
66 | 1 | throw new IllegalArgumentException("Invalid database driver " + config.type()); |
|
67 | } |
||
68 | |||
69 | 1 | pool = new SimpleConnectionPool( |
|
0 ignored issues
–
show
|
|||
70 | 1 | factory.create(config), |
|
71 | 1 | config.maxPoolSize(), |
|
72 | logger |
||
73 | ); |
||
74 | |||
75 | 1 | if (config.refreshPoolInterval() > 0) { |
|
76 | 1 | pool = new RefreshConnectionPool(pool, config.refreshPoolInterval(), logger); |
|
0 ignored issues
–
show
Use try-with-resources or close this "RefreshConnectionPool" in a "finally" clause.
You may want to use ![]() |
|||
77 | } |
||
78 | |||
79 | 1 | if (config.autoReconnect()) { |
|
80 | 1 | pool = new AutoReconnectConnectionPool(pool, logger); |
|
0 ignored issues
–
show
Use try-with-resources or close this "AutoReconnectConnectionPool" in a "finally" clause.
You may want to use ![]() |
|||
81 | } |
||
82 | |||
83 | 1 | pool.initialize(); |
|
84 | |||
85 | 1 | connections.put(name, pool); |
|
86 | |||
87 | 1 | return pool; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Register a new factory |
||
92 | * |
||
93 | * @param type The factory type. {@link Driver#type()} |
||
94 | * @param factory The factory for create the driver |
||
95 | */ |
||
96 | public void factory(String type, Driver.Factory factory) { |
||
97 | 1 | factories.put(type, factory); |
|
98 | 1 | } |
|
99 | |||
100 | @Override |
||
101 | public void stop() { |
||
102 | 1 | final Collection<ConnectionPool> pools = new ArrayList<>(connections.values()); |
|
103 | |||
104 | 1 | connections.clear(); |
|
105 | |||
106 | 1 | for (ConnectionPool pool : pools) { |
|
107 | try { |
||
108 | 1 | pool.close(); |
|
109 | } catch (Exception e) { |
||
110 | // Stop is a no fail operation |
||
111 | 1 | } |
|
112 | 1 | } |
|
113 | 1 | } |
|
114 | } |
||
115 |
You may want to use
try {} ... finally {}
to close the resource or use the (relatively) new try-with-resources capability.