public class MYSQL {
private static final ThreadLocal<Connection> con = new ThreadLocalConnection();
public static final int CLOSE_CURRENT_RESULT = 1;
public static final int KEEP_CURRENT_RESULT = 2;
public static final int CLOSE_ALL_RESULTS = 3;
public static final int SUCCESS_NO_INFO = -2;
public static final int EXECUTE_FAILED = -3;
public static final int RETURN_GENERATED_KEYS = 1;
public static final int NO_GENERATED_KEYS = 2;
public static Connection getConnection() {
return con.get();
}
public static void closeAll() throws SQLException {
for (final Connection conn : ThreadLocalConnection.allConnections) {
conn.close();
}
}
private static final class ThreadLocalConnection extends ThreadLocal<Connection> {
public static final Collection<Connection> allConnections = new LinkedList<Connection>();
@Override
protected final Connection initialValue() {
try {
Class.forName("com.mysql.jdbc.Driver"); // touch the mysql driver
} catch (final ClassNotFoundException e) {
System.err.println("[오류] MYSQL 클래스를 발견할 수 없습니다.");
if (!ServerConstants.realese) {
e.printStackTrace();
}
}
try {
final Connection con = DriverManager.getConnection(
ServerConstants.dbHost,
ServerConstants.dbUser,
ServerConstants.dbPassword);
allConnections.add(con);
return con;
} catch (SQLException e) {
System.err.println("[오류] 데이터베이스 연결에 오류가 발생했습니다.");
if (!ServerConstants.realese) e.printStackTrace();
return null;
}
}
}
}
여기에서 접속 될때마다 커넥션을 클로즈하지않고 하나의 커넥션을 유지하면서 사용하고 싶은데 방법 아시는분 없나요??
[출처] 쿠워리 (@USFM - 메이플스토리 공식 카페) |작성자 블라
커넥션 풀 쓰세요 근데 가급적이면 사용 후 가능한 빨리 풀에 반환하는게 좋음
was 커넥션풀 잡는 방법 알아봐
java applucation이라면 커넥션 직접 만들어야 할꺼인데 그건 mysql이나 java service net 초기 게시물에 지금은 제니퍼 사장인 이원영씨가 직접 쓴 게시물이 있을꺼야 2000년도 자료라 찾기 어려울지도 몰겠다