📄 databaseconnection.java
字号:
/*
This file is part of the OdinMS Maple Story Server
Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
Matthias Butz <matze@odinms.de>
Jan Christian Meyer <vimes@odinms.de>
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 net.sf.odinms.database;
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 OdinMS 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 void setProps(Properties aProps) {
props = aProps;
}
public static void closeAll() throws SQLException {
for (Connection con : ThreadLocalConnection.allConnections) {
con.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;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -