/*
* This file is part of the OdinMS Maple Story Server
* Copyright (C) 2008 Patrick Huy <[email protected]>
* Matthias Butz <[email protected]>
* Jan Christian Meyer <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License version 3
* as published by the Free Software Foundation. You may not use, modify
* or distribute this program under any other version of the
* GNU Affero General Public License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package tools;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* All servers maintain a Database Connection. This class therefore "singletonices" the connection per process.
*
*
* @author Frz
*/
public class DatabaseConnection {
private static ThreadLocal<Connection> con = new ThreadLocalConnection();
private final static Logger LOG = LoggerFactory.getLogger(DatabaseConnection.class);
private static Properties props = null;
public static Connection getConnection() {
if (props == null) {
throw new RuntimeException("DatabaseConnection not initialized");
}
return con.get();
}
public static boolean isInitialized() {
return props != null;
}
public static void setProps(Properties aProps) {
props = aProps;
}
public static void closeAll() throws SQLException {
for (Connection chon : ThreadLocalConnection.allConnections) {
chon.close();
}
}
private static class ThreadLocalConnection extends ThreadLocal<Connection> {
public static Collection<Connection> allConnections = new LinkedList<Connection>();
@Override
protected Connection initialValue() {
String driver = props.getProperty("driver");
String url = props.getProperty("url");
String user = props.getProperty("user");
String password = props.getProperty("password");
try {
Class.forName(driver); // touch the mysql driver
} catch (ClassNotFoundException e) {
LOG.error("ERROR", e);
}
try {
Connection con = DriverManager.getConnection(url, user, password);
allConnections.add(con);
return con;
} catch (SQLException e) {
LOG.error("ERROR", e);
return null;
}
}
}
}