-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathL2DatabaseFactory.java
More file actions
56 lines (46 loc) · 1.51 KB
/
L2DatabaseFactory.java
File metadata and controls
56 lines (46 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package l2mv.authserver.database;
import java.sql.Connection;
import java.util.logging.Logger;
import org.mariadb.jdbc.MariaDbPoolDataSource;
import l2mv.authserver.Config;
public class L2DatabaseFactory{
private static final Logger LOGGER = Logger.getLogger(L2DatabaseFactory.class.getName());
private static final MariaDbPoolDataSource DATABASE_POOL = new MariaDbPoolDataSource(Config.DATABASE_URL + "&user=" + Config.DATABASE_LOGIN + "&password=" + Config.DATABASE_PASSWORD + "&maxPoolSize=" + Config.DATABASE_MAX_CONN);
public static void init() {
// Test if connection is valid.
try {
DATABASE_POOL.getConnection().close();
LOGGER.info("Database: Initialized.");
} catch (final Exception e) {
LOGGER.info("Database: Problem on initialize. " + e);
}
}
// TODO make it static and drop getInstance()
public Connection getConnection() {
Connection con = null;
while (con == null) {
try {
con = DATABASE_POOL.getConnection();
} catch (final Exception e) {
LOGGER.severe("DatabaseFactory: Cound not get a connection. " + e);
}
}
return con;
}
public void close() {
try {
DATABASE_POOL.close();
} catch (final Exception e) {
LOGGER.severe("DatabaseFactory: There was a problem closing the data source. " + e);
}
}
/**
* @return instance of DatabaseFactory
*/
public static L2DatabaseFactory getInstance() {
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder{
protected static final L2DatabaseFactory INSTANCE = new L2DatabaseFactory();
}
}