您的位置:首页 > 编程语言 > Java开发

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

2012-05-31 15:11 696 查看
适用于基本所有的应用服务器,如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();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: