/**
 * ÀÌ ¼Ò½º´Â Spring ÇÁ·¹ÀÓ¿öÅ© ¿öÅ©ºÏ¿¡¼­ »ç¿ëÇÑ ¿¹Á¦ ¼Ò½ºÀÔ´Ï´Ù. 
 * ÀÌ ¼Ò½º´Â ¸ðµç °³¹ßÀÚµéÀÌ ÀÚÀ¯·Ó°Ô ¼öÁ¤ ¹× ¹èÆ÷ÇÒ ¼ö ÀÖ½À´Ï´Ù. 
 * ´Ü, ÀÌ ¼Ò½º¸¦ ±â¹ÝÀ¸·Î »õ·Î¿î ¾ÖÇÃ¸®ÄÉÀÌ¼ÇÀ» °³¹ßÇÒ °æ¿ì ÃâÃ³¸¦ ¸í½ÃÇØ ÁÖ½Ã¸é µË´Ï´Ù. 
 */
package net.javajigi.db;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ConnectionManager {
    protected static final Log logger = LogFactory
            .getLog(ConnectionManager.class);

    public static Connection getConnection() {
        try {
            Context ctx = new InitialContext();
            // Tomcat Server¿¡¼­ DataSource¸¦ Lookup
            //DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/SpringDS");
            
            // JBoss Server¿¡¼­ DataSource¸¦ Lookup
            DataSource ds = (DataSource) ctx.lookup("java:/jdbc/SpringDS");            

            return ds.getConnection();
        } catch (NamingException e) {
            if (logger.isErrorEnabled()) {
                logger.error(e.getMessage(), e);
            }

            throw new DBNotConnectedException(e.getMessage(), e);
        } catch (SQLException e) {
            if (logger.isErrorEnabled()) {
                logger.error(e.getMessage(), e);
            }
            
            throw new DBNotConnectedException(e.getMessage(), e);
        }
    }
}