📄 datasourcemanager.java
字号:
package com.easyjf.dbo;
import com.easyjf.dbo.config.*;
//apache的连接沲
import org.apache.commons.dbcp.*;
import org.apache.log4j.*;
import java.sql.*;
import java.util.*;
import javax.naming.*;
import javax.sql.*;
/**
*
* <p>
* Title: 数据源管理类
* </p>
* <p>
* Description:主要用户测试,实际应用中一般直接通过JNDI访问服务器的数据源
* </p>
*
* <p>
* Copyright: Copyright (c) 2006
* </p>
*
* <p>
* Company: EasyJF开源团队-EasyDBO项目组
* </p>
*
* @author 蔡世友
* @version 1.0
*/
public class DataSourceManager {
private final static Logger logger = Logger
.getLogger(DataSourceManager.class);
private static DataSource dataSource = null;
private static final Map container = new HashMap();
/**
* 驱动
*/
private String driver;
private String jndi;
private String jndiFactory;
private String password;
private String poolFactory;
private String url;
private String userName;
// ~--- constructors -------------------------------------------------------
public DataSourceManager() {
}
/**
* 通过外部数据源直接配置,也即支持IOC方式从外部植入数据源
*
* @param source
* 用户自定义的数据源
*/
public DataSourceManager(DataSource source) {
DataSourceManager.dataSource = source;
// container.put("default",source);
}
/**
* 通过jndi取得数据源
*
* @param jndiFactory
* String
* @param url
* String
* @param jndi
* String
*/
public DataSourceManager(String jndiFactory, String url, String jndi) {
this.url = url;
this.jndiFactory = jndiFactory;
this.jndi = jndi;
}
/**
* 获取DataSourceManager
*
* @param driver
* 驱动名称
* @param url
* 驱动URL
* @param userName
* 数据库用户名
* @param password
* 数据库帐号
*/
public DataSourceManager(String driver, String url, String userName,
String password, String poolFactory) {
this.driver = driver;
this.url = url;
this.userName = userName;
this.password = password;
this.poolFactory = poolFactory;
}
/**
* 创建驱动数据源
*
* @return 如果数据源可用,则返回该数据源,不可用则返回空
*/
public DataSource createDriverSource() {
BasicDataSource bdds = new BasicDataSource();
bdds.setDriverClassName(driver);
bdds.setUrl(url);
bdds.setUsername(userName);
bdds.setPassword(password);
if (!testConnection(bdds)) {
System.out.println("无法打开数据源!");
bdds = null; // 如果数据源不可用,则返回空
}
return bdds;
}
/**
* 创建JNDI数据源
*
* @return DataSource数据源可用则返该数据源,不可用则返回空
*/
public DataSource createJNDISource() {
DataSource source = null;
try {
Hashtable env = new Hashtable();
if ((jndiFactory != null) && (!jndiFactory.equals(""))) {
env.put(Context.INITIAL_CONTEXT_FACTORY, jndiFactory);
}
if ((url != null) && (!url.equals(""))) {
env.put(Context.PROVIDER_URL, url);
}
InitialContext ctx = new InitialContext(env);
if ((jndi != null) && (!jndi.equals(""))) {
source = (DataSource) ctx.lookup(jndi);
} else {
logger.error("无法查找JNDI数据源,请确认配置文件是否正确!");
}
} catch (Exception e) {
logger.error("JNDI数据源创建错误!");
}
if (!testConnection(source)) {
source = null;
}
return source;
}
/**
* 测试数据源连接是否可用
*
* @param source
* @return
*/
public boolean testConnection(DataSource source) {
boolean ret = false;
if (source == null) {
return false;
}
Connection con = null;
try {
con = source.getConnection();
if (con != null) {
ret = true;
}
} catch (Exception e) {
logger.error("无法从数据源创建数据连接,请确认是否配置正确!" + e);
e.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
} catch (Exception e) {
logger.error("无法从数据源创建数据连接,请确认是否配置正确!" + e);
}
}
return ret;
}
// ~--- get methods --------------------------------------------------------
/**
* 获取数据源
*
* @return DataSource
* @throws EasyDBOException
*/
public static DataSource getDataSource() throws EasyDBOException {
if (dataSource == null) {
// try{
dataSource = DBOConfig.getInstance().getDataSource();
// }
// catch(EasyDBOException e)
// {
// logger.error(e);
// }
}
return dataSource;
}
/**
* 根据名字获取数据源
*
* @param name
* String
* @return DataSource
* @throws EasyDBOException
*/
public static DataSource getDataSource(String name) throws EasyDBOException {
if (container.isEmpty()) {
DBOConfig.getInstance().init();
}
Object data = container.get(name);
if ((data == null) || (!(data instanceof DataSource))) {
throw new EasyDBOException("找不到ID为" + name + "的数据库源,请确认配置文件是否正确!");
}
return dataSource;
}
/**
* 获取所有数据源
*
* @return Map
* @throws EasyDBOException
*/
public static Map getDataSources() throws EasyDBOException {
if (container.isEmpty()) {
DBOConfig.getInstance().init();
}
return container;
}
/**
* 增加数据源
*
* @param name
* String
* @param dataSource
* DataSource
*/
public static void addDataSource(String name, DataSource dataSource) {
container.put(name, dataSource);
}
// ~--- set methods --------------------------------------------------------
/**
* 设置数据源
*
* @param source
* DataSource
*/
public void setDataSource(DataSource source) {
DataSourceManager.dataSource = source;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -