当前位置: 技术问答>java相关
怎样建立连接池
来源: 互联网 发布时间:2015-11-06
本文导语: 在频繁数据的存取中,一般是采用连接池 基本原理我是知道,但是具体该怎么做呢?不太清楚。 谁能帮帮我啊? | /** * @author Umesh * @version 1.0 * * Development Environment : ...
在频繁数据的存取中,一般是采用连接池
基本原理我是知道,但是具体该怎么做呢?不太清楚。
谁能帮帮我啊?
基本原理我是知道,但是具体该怎么做呢?不太清楚。
谁能帮帮我啊?
|
/**
* @author Umesh
* @version 1.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the Application : ConnCacheBean.java
* Creation/Modification History :
*
* Umesh 25-Nov-2001 Created
*
* Overview of Application : This Bean Class is used by all the JSPs
* to perform database interaction. This class uses JDBC to perform any DML/DDL
* operations. The key concept illustarted here is Connection Caching.
*
* As JSPs execute in middle tier, getting an individual database connection
* everytime for every user is an expensive operation. This is true especially
* when number of users involved are large in numbers.
*
* With the help of Connection Caching, the overhead of instantiating a new physical
* database connection can be easily overcome.
*
* This bean is implemented as a SingleTon Class meaning that there can be only
* one instance of this bean per JVM. In the constructor of the bean, Connection
* Cache is initialized and
*
**/
package oracle.otnsamples.oracle9ijdbc.conncachesample;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import oracle.jdbc.pool.OracleDataSource;
import oracle.jdbc.pool.OracleConnectionPoolDataSource;
import oracle.jdbc.pool.OracleConnectionCacheImpl;
import java.util.Hashtable;
import java.util.Vector;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.NameNotFoundException;
import javax.naming.Context;
public class ConnCacheBean {
// Connection Cache Variable
private OracleConnectionCacheImpl m_ocacheimpl = null;
// Data Source Variable
private OracleConnectionPoolDataSource m_cpds = null;
// Variable pointing to this instance
private static ConnCacheBean m_thisInstance = null;
/**
* Private Constructor : This approach makes it easy to implement this class as
* SingleTon Class.
*
* This method initializes Cache if not already initialized.
**/
private ConnCacheBean() throws Exception {
if (m_ocacheimpl == null)
initializeConnectionCache();
}
/**
* Method which returns a single instance of this bean.
**/
public static ConnCacheBean getInstance() throws Exception {
if ( m_thisInstance == null ) {
m_thisInstance = new ConnCacheBean();
}
return m_thisInstance;
}
* @author Umesh
* @version 1.0
*
* Development Environment : Oracle9i JDeveloper
* Name of the Application : ConnCacheBean.java
* Creation/Modification History :
*
* Umesh 25-Nov-2001 Created
*
* Overview of Application : This Bean Class is used by all the JSPs
* to perform database interaction. This class uses JDBC to perform any DML/DDL
* operations. The key concept illustarted here is Connection Caching.
*
* As JSPs execute in middle tier, getting an individual database connection
* everytime for every user is an expensive operation. This is true especially
* when number of users involved are large in numbers.
*
* With the help of Connection Caching, the overhead of instantiating a new physical
* database connection can be easily overcome.
*
* This bean is implemented as a SingleTon Class meaning that there can be only
* one instance of this bean per JVM. In the constructor of the bean, Connection
* Cache is initialized and
*
**/
package oracle.otnsamples.oracle9ijdbc.conncachesample;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import oracle.jdbc.pool.OracleDataSource;
import oracle.jdbc.pool.OracleConnectionPoolDataSource;
import oracle.jdbc.pool.OracleConnectionCacheImpl;
import java.util.Hashtable;
import java.util.Vector;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.NameNotFoundException;
import javax.naming.Context;
public class ConnCacheBean {
// Connection Cache Variable
private OracleConnectionCacheImpl m_ocacheimpl = null;
// Data Source Variable
private OracleConnectionPoolDataSource m_cpds = null;
// Variable pointing to this instance
private static ConnCacheBean m_thisInstance = null;
/**
* Private Constructor : This approach makes it easy to implement this class as
* SingleTon Class.
*
* This method initializes Cache if not already initialized.
**/
private ConnCacheBean() throws Exception {
if (m_ocacheimpl == null)
initializeConnectionCache();
}
/**
* Method which returns a single instance of this bean.
**/
public static ConnCacheBean getInstance() throws Exception {
if ( m_thisInstance == null ) {
m_thisInstance = new ConnCacheBean();
}
return m_thisInstance;
}
|
servlet连接池的例子
***************************************
import javax.servlet.* ;
import javax.servlet.http.* ;
import java.io.* ;
import java.sql.* ;
import java.util.Vector;
import oracle.jdbc.driver.*;
import java.util.Enumeration;
import java.util.Properties;
import com.unitech.connectionpool.* ;
public class dbTest extends HttpServlet {
//Initialize global variables
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
// 数据库连接:Connetcion conn = null ;
Connection conn = null ;
//数据库查询内容执行:Statement stment = null ;
Statement stment = null ;
// 数据库连接池的初始化
DBConnectionManager connMgr = DBConnectionManager.getInstance();
//初始化数据库连接池,并且获取数据库连接
private void initDatabaseDriver () {
conn = connMgr.getConnection("oracle");
if (conn == null) {
System.out.println("数据库连接失败。");
return;
}
try {
stment = conn.createStatement();
}
catch (SQLException e) {
e.printStackTrace() ;
}
}
//释放数据库连接
private void freeConnectionPool() {
connMgr.freeConnection("oracle", conn) ;
}
//获取记录集,并返回给VERTOR V
public Vector getForumList() {
String[] s = {"","","","",""} ;//与选取的列数相等。
Vector v = new Vector() ;
this.initDatabaseDriver();
try{
String queryStr = null ;
queryStr = "SELECT BBS_ID,BBS_NAME,DESCRIPTION,MANAGER_ID, CREATE_DATE FROM BBS WHERE IS_SYSTEM='0' ORDER BY CREATE_DATE DESC" ;
ResultSet rSet = stment.executeQuery(queryStr) ;
while (rSet.next()) {
s[0] = Integer.toString(rSet.getInt("BBS_ID")) ;
s[1] = rSet.getString("BBS_NAME") ;
s[2] = rSet.getString("DESCRIPTION") ;
s[3] = rSet.getString("MANAGER_ID") ;
Timestamp createdate = rSet.getTimestamp("CREATE_DATE") ;
String tmp = createdate.toString() ;
s[4] = tmp.substring(0,(tmp.length()-2)) ;
v.addElement(s.clone());
}
rSet.close();
stment.close();
this.freeConnectionPool();
}
catch(Exception e) {
try {
stment.close();
this.freeConnectionPool();
}
catch(SQLException ee) {
ee.printStackTrace();
}
e.printStackTrace() ;
}
return v ;
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = new PrintWriter (response.getOutputStream());
response.setContentType("text/html");
out.println("");
out.println("The servlet has received a GET. This is the reply.");
out.println("");
out.println("");
// 将记录集循环输出到页面。
Vector v = new Vector() ;
v = this.getForumList() ;
for (int i=0; i"
+ ""+s[0]+""
+ ""+s[1]+""
+ ""+s[2]+""
+ ""+s[3]+""
+ ""+s[4]+"");
}
out.println("");
out.close();
}
}
***************************************
import javax.servlet.* ;
import javax.servlet.http.* ;
import java.io.* ;
import java.sql.* ;
import java.util.Vector;
import oracle.jdbc.driver.*;
import java.util.Enumeration;
import java.util.Properties;
import com.unitech.connectionpool.* ;
public class dbTest extends HttpServlet {
//Initialize global variables
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
// 数据库连接:Connetcion conn = null ;
Connection conn = null ;
//数据库查询内容执行:Statement stment = null ;
Statement stment = null ;
// 数据库连接池的初始化
DBConnectionManager connMgr = DBConnectionManager.getInstance();
//初始化数据库连接池,并且获取数据库连接
private void initDatabaseDriver () {
conn = connMgr.getConnection("oracle");
if (conn == null) {
System.out.println("数据库连接失败。");
return;
}
try {
stment = conn.createStatement();
}
catch (SQLException e) {
e.printStackTrace() ;
}
}
//释放数据库连接
private void freeConnectionPool() {
connMgr.freeConnection("oracle", conn) ;
}
//获取记录集,并返回给VERTOR V
public Vector getForumList() {
String[] s = {"","","","",""} ;//与选取的列数相等。
Vector v = new Vector() ;
this.initDatabaseDriver();
try{
String queryStr = null ;
queryStr = "SELECT BBS_ID,BBS_NAME,DESCRIPTION,MANAGER_ID, CREATE_DATE FROM BBS WHERE IS_SYSTEM='0' ORDER BY CREATE_DATE DESC" ;
ResultSet rSet = stment.executeQuery(queryStr) ;
while (rSet.next()) {
s[0] = Integer.toString(rSet.getInt("BBS_ID")) ;
s[1] = rSet.getString("BBS_NAME") ;
s[2] = rSet.getString("DESCRIPTION") ;
s[3] = rSet.getString("MANAGER_ID") ;
Timestamp createdate = rSet.getTimestamp("CREATE_DATE") ;
String tmp = createdate.toString() ;
s[4] = tmp.substring(0,(tmp.length()-2)) ;
v.addElement(s.clone());
}
rSet.close();
stment.close();
this.freeConnectionPool();
}
catch(Exception e) {
try {
stment.close();
this.freeConnectionPool();
}
catch(SQLException ee) {
ee.printStackTrace();
}
e.printStackTrace() ;
}
return v ;
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = new PrintWriter (response.getOutputStream());
response.setContentType("text/html");
out.println("");
out.println("The servlet has received a GET. This is the reply.");
out.println("");
out.println("");
// 将记录集循环输出到页面。
Vector v = new Vector() ;
v = this.getForumList() ;
for (int i=0; i"
+ ""+s[0]+""
+ ""+s[1]+""
+ ""+s[2]+""
+ ""+s[3]+""
+ ""+s[4]+"");
}
out.println("");
out.close();
}
}
|
最简单的方法
下载一个连接池的bean
仔细研究两遍
大致是这个意思:
1、建立一个连接bean
2、生成一个唯一的连接池对象
3、调取连接bean
连接池是一个类中类
下载一个连接池的bean
仔细研究两遍
大致是这个意思:
1、建立一个连接bean
2、生成一个唯一的连接池对象
3、调取连接bean
连接池是一个类中类
|
如果使用应用服务器,则一般的在服务器中建立数据源和连接池,方法参看该服务器文档
程序中使用jndi进行查找数据源,然后获取连接
程序中使用jndi进行查找数据源,然后获取连接