Nemo

Nemo 关注TA

路漫漫其修远兮,吾将上下而求索。

Nemo

该文章投稿至Nemo社区   Java  板块 复制链接


JDBC DATASOURCE

发布于 2016/07/18 10:21 4,550浏览 0回复 15,072

###这是在最近的TCP SERVER项目中使用到的数据源,这里也稍稍做下记录。一直觉得jdbc如果使用得当,其效率可以很好~


#######DSconfigBean########

package com.nemo.datasource;


 /** 
  * 配置文件Bean类 
  * @author Nemo 
  * 
  */ 
public class DSConfigBean { 
private String dbType     =""; //数据库类型 
private String poolName     =""; //连接池名字 
private String driver   =""; //数据库驱动 
private String url      =""; //数据库url 
private String username =""; //用户名 
private String password =""; //密码 
private int maxconn  =10; //最大连接数 
/** 
 * 
 */ 
public DSConfigBean() { 
 // TODO Auto-generated constructor stub 

/** 
 * @param args 
 */ 
public static void main(String[] args) { 
 // TODO Auto-generated method stub 

/** 
 * @return the driver 
 */ 
public String getDriver() { 
 return driver; 

/** 
 * @param driver the driver to set 
 */ 
public void setDriver(String driver) { 
 this.driver = driver; 

/** 
 * @return the maxconn 
 */ 
public int getMaxconn() { 
 return maxconn; 

/** 
 * @param maxconn the maxconn to set 
 */ 
public void setMaxconn(int maxconn) { 
 this.maxconn = maxconn; 

/** 
 * @return the poolName 
 */ 
public String getPoolName() { 
 return poolName; 

/** 
 * @param poolName the poolName to set 
 */ 
public void setPoolName(String poolName) { 
 this.poolName = poolName; 

/** 
 * @return the password 
 */ 
public String getPassword() { 
 return password; 

/** 
 * @param password the password to set 
 */ 
public void setPassword(String password) { 
 this.password = password; 

/** 
 * @return the url 
 */ 
public String getUrl() { 
 return url; 

/** 
 * @param url the url to set 
 */ 
public void setUrl(String url) { 
 this.url = url; 

/** 
 * @return the username 
 */ 
public String getUsername() { 
 return username; 

/** 
 * @param username the username to set 
 */ 
public void setUsername(String username) { 
 this.username = username; 
}
public String getDbType() {
return dbType;
}
public void setDbType(String dbType) {
this.dbType = dbType;




########DBConnectionPool #########


package com.nemo.datasource;

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.Timer; 
/** 
 * 数据库连接池
* @author Nemo 
*/ 
public class DBConnectionPool { 
private Connection con=null; 
private int inUsed=0;    //使用的连接数 
private ArrayList<Connection> freeConnections = new ArrayList<Connection>();//容器,空闲连接 
private int minConn;     //最小连接数 
private int maxConn;     //最大连接 
private String name;     //连接池名字 
private String password; //密码 
private String url;      //数据库连接地址 
private String driver;   //驱动 
private String user;     //用户名 
public Timer timer;      //定时 

/** 
 * 默认构造
 */ 
public DBConnectionPool() {} 

/** 
 * 创建连接池 
 * @param driver 
 * @param name 
 * @param URL 
 * @param user 
 * @param password 
 * @param maxConn 
 */ 
public DBConnectionPool(String name, String driver,String URL, String user, String password, int maxConn) 

 this.name=name; 
 this.driver=driver; 
 this.url=URL; 
 this.user=user; 
 this.password=password; 
 this.maxConn=maxConn; 


/** 
 * 用完,释放连接 
 * @param con 
 */ 
public synchronized void freeConnection(Connection con) 

 this.freeConnections.add(con);//添加到空闲连接的末尾 
 this.inUsed--; 


/** 
 * timeout  根据timeout得到连接 
 * @param timeout 
 * @return 
 */ 
public synchronized Connection getConnection(long timeout) 

 Connection con=null; 
 if(this.freeConnections.size()>0) 
 { 
  con=(Connection)this.freeConnections.get(0); 
  if(con==null)con=getConnection(timeout); //继续获得连接 
 } 
 else 
 { 
  con=newConnection(); //新建连接 
 } 
 if(this.maxConn==0||this.maxConn<this.inUsed) 
 { 
  con=null;//达到最大连接数,暂时不能获得连接了。 
 } 
 if(con!=null) 
 { 
  this.inUsed++; 
 } 
 return con; 


/** 
 * 
 * 从连接池里得到连接 
 * @return 
 */ 
public synchronized Connection getConnection() 

 Connection con=null; 
 if(this.freeConnections.size()>0) 
 { 
  con=(Connection)this.freeConnections.get(0); 
  this.freeConnections.remove(0);//如果连接分配出去了,就从空闲连接里删除 
  if(con==null)con=getConnection(); //继续获得连接 
 } 
 else 
 { 
  con=newConnection(); //新建连接 
 } 
 if(this.maxConn==0||this.maxConn<this.inUsed) 
 { 
  con=null;//等待 超过最大连接时 
 } 
 if(con!=null) 
 { 
  this.inUsed++; 
  //System.out.println("得到 "+this.name+" 的连接,现有"+inUsed+"个连接在使用!"); 
 } 
 return con; 


/** 
 *释放全部连接 
 * 
 */ 
public synchronized void release() 

 Iterator<Connection> allConns=this.freeConnections.iterator(); 
 while(allConns.hasNext()) 
 { 
  Connection con=(Connection)allConns.next(); 
  try 
  { 
   con.close(); 
  } 
  catch(SQLException e) 
  { 
   e.printStackTrace(); 
  } 
  
 } 
 this.freeConnections.clear(); 
  


/** 
 * 创建新连接 
 * @return 
 */ 
private Connection newConnection() 

 try { 
  Class.forName(driver); 
  con=DriverManager.getConnection(url, user, password); 
 } catch (ClassNotFoundException e) { 
  // TODO Auto-generated catch block 
  e.printStackTrace(); 
  System.out.println("sorry can't find db driver!"); 
 } catch (SQLException e1) { 
  // TODO Auto-generated catch block 
  e1.printStackTrace(); 
  System.out.println("sorry can't create Connection!"); 
 } 
 return con; 
 


/** 
 * 定时处理函数 
 */ 
public synchronized void TimerEvent() 

    //暂时还没有实现以后会加上的 


/** 
 * @param args 
 */ 
public static void main(String[] args) { 
 // TODO Auto-generated method stub 


/** 
 * @return the driver 
 */ 
public String getDriver() { 
 return driver; 


/** 
 * @param driver the driver to set 
 */ 
public void setDriver(String driver) { 
 this.driver = driver; 


/** 
 * @return the maxConn 
 */ 
public int getMaxConn() { 
 return maxConn; 


/** 
 * @param maxConn the maxConn to set 
 */ 
public void setMaxConn(int maxConn) { 
 this.maxConn = maxConn; 


/** 
 * @return the minConn 
 */ 
public int getMinConn() { 
 return minConn; 


/** 
 * @param minConn the minConn to set 
 */ 
public void setMinConn(int minConn) { 
 this.minConn = minConn; 


/** 
 * @return the name 
 */ 
public String getName() { 
 return name; 


/** 
 * @param name the name to set 
 */ 
public void setName(String name) { 
 this.name = name; 


/** 
 * @return the password 
 */ 
public String getPassword() { 
 return password; 


/** 
 * @param password the password to set 
 */ 
public void setPassword(String password) { 
 this.password = password; 


/** 
 * @return the url 
 */ 
public String getUrl() { 
 return url; 


/** 
 * @param url the url to set 
 */ 
public void setUrl(String url) { 
 this.url = url; 


/** 
 * @return the user 
 */ 
public String getUser() { 
 return user; 


/** 
 * @param user the user to set 
 */ 
public void setUser(String user) { 
 this.user = user; 




#########DBConnectionManager ########


package com.nemo.datasource;

import java.sql.Connection; 
import java.util.Enumeration; 
import java.util.Hashtable; 
import java.util.Map;

import com.nemo.datasource.DSConfigBean; 
import com.nemo.datasource.DBConnectionPool; 
import com.nemo.utils.GlobalUtils;
 /** 
  * 连接池管理者
  * @author Nemo 
  * 
  */ 
public class DBConnectionManager { 
static private DBConnectionManager instance;//唯一数据库连接池管理实例类 
@SuppressWarnings("unused")
static private int clients;                 //客户连接数 
private Hashtable<String, DBConnectionPool> pools=new Hashtable<String, DBConnectionPool>();//连接池 

/** 
 * 实例化管理类 
 */ 
public DBConnectionManager() { 
 this.init(); 

/** 
 * 得到唯一实例管理类 
 * @return 
 */ 
static synchronized public DBConnectionManager getInstance() 

 if(instance==null) 
 { 
  instance=new DBConnectionManager(); 
 } 
 return instance; 
 

/** 
 * 释放连接 
 * @param name 
 * @param con 
 */ 
public void freeConnection(String name, Connection con) 

 DBConnectionPool pool=(DBConnectionPool)pools.get(name);//根据关键名字得到连接池 
 if(pool!=null) 
 pool.freeConnection(con);//释放连接 

/** 
 * 得到一个连接根据连接池的名字name 
 * @param name 
 * @return 
 */ 
public Connection getConnection(String name) 

 DBConnectionPool pool=null; 
 Connection con=null; 
 pool=(DBConnectionPool)pools.get(name);//从名字中获取连接池 
 con=pool.getConnection();//从选定的连接池中获得连接 
 return con; 

/** 
 * 得到一个连接,根据连接池的名字和等待时间 
 * @param name 
 * @param time 
 * @return 
 */ 
public Connection getConnection(String name, long timeout) 

 DBConnectionPool pool=null; 
 Connection con=null; 
 pool=(DBConnectionPool)pools.get(name);//从名字中获取连接池 
 con=pool.getConnection(timeout);//从选定的连接池中获得连接 
 return con; 

/** 
 * 释放所有连接 
 */ 
public synchronized void release() 

 Enumeration<DBConnectionPool> allpools=pools.elements(); 
 while(allpools.hasMoreElements()) 
 { 
  DBConnectionPool pool=(DBConnectionPool)allpools.nextElement(); 
  if(pool!=null)pool.release(); 
 } 
 pools.clear(); 

/** 
 * 创建连接池 
 * @param props 
 */ 
private void createPools(DSConfigBean dsb) 

 DBConnectionPool dbpool=new DBConnectionPool(); 
 dbpool.setName(dsb.getPoolName()); 
 dbpool.setDriver(dsb.getDriver()); 
 dbpool.setUrl(dsb.getUrl()); 
 dbpool.setUser(dsb.getUsername()); 
 dbpool.setPassword(dsb.getPassword()); 
 dbpool.setMaxConn(dsb.getMaxconn()); 
//  System.out.println("ioio:"+dsb.getMaxconn()); 
 pools.put(dsb.getPoolName(), dbpool); 

/** 
 * 初始化连接池的参数 
 */ 
private void init() 

 Map<String,String> configs = GlobalUtils.configs;
 DSConfigBean dsb = new DSConfigBean();
 dsb.setDriver(configs.get("jdbc.driver"));
 dsb.setMaxconn(Integer.parseInt(configs.get("jdbc.getMaxConn")));
 dsb.setPoolName(configs.get("jdbc.poolName"));
 dsb.setPassword(configs.get("jdbc.password"));
 dsb.setDbType(configs.get("jdbc.dbType"));
 dsb.setUrl(configs.get("jdbc.url"));
 dsb.setUsername(configs.get("jdbc.username"));
 createPools(dsb);

/** 
 * @param args 
 */ 
public static void main(String[] args) { 
 // TODO Auto-generated method stub 


###########DBBase #########


package com.nemo.datasource;

import java.sql.Connection;

import com.nemo.utils.GlobalUtils;

/**
 * 数据源操作基本类
 * @author nemo
 *
 */
public class DBBase {

private static final String POOLNAME = GlobalUtils.configs.get("jdbc.poolName");//从上下文得到你要访问的数据库的名字 
private static final DBConnectionManager connectionMan=DBConnectionManager .getInstance();

/**
* 得到数据库连接
* @return
*/
public static Connection getConnection (){
//得到连接 
Connection con=connectionMan.getConnection(POOLNAME); 
return con;
}

/**
* 释放连接
* @param conn
*/
public static void freeConnection(Connection conn){
connectionMan.freeConnection(POOLNAME,conn);//释放,但并未断开连接 
}

}

点赞(0)

上一个文章:致自己

下一个文章:获取CPU占用的工具

点了个评