java中使用应用服务器配置的数据库连接

适用于基本所有的应用服务器,如jboss、tomcat


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;


public class ConnectionPoolManager
{
	private static Log _log = LogFactory.getLog(ConnectionPoolManager.class);
	private static ConnectionPoolManager instance;	
	private DataSource ds = null;
    
  /** 
	* 构造函数私有以防止其它对象创建本类实例 
	*/ 
	private ConnectionPoolManager()
	{   		
        try {
            Context initCtx=new InitialContext();
            //ChinaWiservDS portal-ds.xml 配置
            ds = (DataSource) _lookup( initCtx ,"test");
        } catch (NamingException e) {
            e.printStackTrace();
        }
	} 
	private ConnectionPoolManager(String jndiName)
    {           
        try {
            Context initCtx=new InitialContext();
            //ChinaWiservDS portal-ds.xml 配置
            ds = (DataSource) _lookup( initCtx ,jndiName);
        } catch (NamingException e) {
            e.printStackTrace();
        }
    } 
	
	private static Object _lookup(Context ctx, String location) throws NamingException{


        Object obj = null;
    
        try {
            obj = ctx.lookup(location);
        }
        catch (NamingException n1) {
    
            // java:comp/env/ObjectName to ObjectName
    
            if (location.indexOf("java:comp/env/") != -1) {
                try {
                    String newLocation = location.replace( "java:comp/env/", "");
    
                    if (_log.isDebugEnabled()) {
                        _log.debug(n1.getMessage());
                        _log.debug("Attempt " + newLocation);
                    }
    
                    obj = ctx.lookup(newLocation);
                }
                catch (NamingException n2) {
                    // java:comp/env/ObjectName to java:ObjectName
                    String newLocation = location.replace( "comp/env/", "");
    
                    if (_log.isDebugEnabled()) {
                        _log.debug(n2.getMessage());
                        _log.debug("Attempt " + newLocation);
                    }
    
                    obj = ctx.lookup(newLocation);
                }
            }
    
            // java:ObjectName to ObjectName
    
            else if (location.indexOf("java:") != -1) {
                try {
                    String newLocation = location.replace("java:", "");
                    if (_log.isDebugEnabled()) {
                        _log.debug(n1.getMessage());
                        _log.debug("Attempt " + newLocation);
                    }
    
                    obj = ctx.lookup(newLocation);
                }
                catch (NamingException n2) {
    
                    // java:ObjectName to java:comp/env/ObjectName
    
                    String newLocation = location.replace( "java:", "java:comp/env/");
    
                    if (_log.isDebugEnabled()) {
                        _log.debug(n2.getMessage());
                        _log.debug("Attempt " + newLocation);
                    }
    
                    obj = ctx.lookup(newLocation);
                }
            }
    
            // ObjectName to java:ObjectName    
            else if (location.indexOf("java:") == -1) {
                try {
                    String newLocation = "java:" + location;
    
                    if (_log.isDebugEnabled()) {
                        _log.debug(n1.getMessage());
                        _log.debug("Attempt " + newLocation);
                    }
    
                    obj = ctx.lookup(newLocation);
                }
                catch (NamingException n2) {
    
                    // ObjectName to java:comp/env/ObjectName    
                    String newLocation = "java:comp/env/" + location;
    
                    if (_log.isDebugEnabled()) {
                        _log.debug(n2.getMessage());
                        _log.debug("Attempt " + newLocation);
                    }
    
                    obj = ctx.lookup(newLocation);
                }
            }
            else {
                throw new NamingException();
            }
        }    
        return obj;
    }
                 
	/** 
      * 返回唯一实例.如果是第一次调用此方法,则创建实例 
	  * 
	  * @return ConnectionPoolManager 唯一实例 
	  */ 
	static synchronized public ConnectionPoolManager getInstance(String jndiName) 
	{ 
		if (instance == null)
		{ 
			instance = new ConnectionPoolManager(jndiName); 
		} 
		return instance; 
	} 
	
	
	

	/**
	  * 获取数据库连接	  
	  * @return - 返回一个可用数据库连接
	  */
	public Connection getConnection()
		throws SQLException
	{
		Connection cnn=null;
		try
		{			
			if(ds == null){
				throw new Exception();
			}
			cnn=ds.getConnection();			
			if (cnn == null){
				throw new Exception();
			}
			cnn.setAutoCommit(false);
			
			return cnn;
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			throw new SQLException("连接池连接获取异常");
		}
	}

	
	/**
	  * 释放数据库连接
	  * @param cnn  欲释放回连接池的连接
	  * @return  
	  */
	public void closeConnection(Connection cnn)
	{
		try
		{
			if (cnn != null)
			{
				cnn.close();
				cnn = null;
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值