public class ConnPool {
private static final int defaultMaxConnections=3;//默认最大连接对象数;
private Vector freeConnections;//存放还未被配置出去的连接对象;
private Hashtable boundConnections;//存放目前正被使用的连接对象;
private String driverName;
private String jdbcURL;
private String username;
private String password;
private int maxConnections;//最大连接对象数; public ConnPool(int numConnections) {
maxConnections=numConnections;
boundConnections=null;
freeConnections=null;
driverName="";
jdbcURL="";
username="";
password="";
}
public ConnPool() {
this(defaultMaxConnections);
}
public void openDB(String drvName,String url,String uname,String passwd) throws SQLException {
try {
boundConnections=new Hashtable(maxConnections);
freeConnections=new Vector(maxConnections);
Class.forName(drvName);
for(int i=0;i<maxConnections;i++) {
freeConnections.addElement(DriverManager.getConnection(url,uname,passwd));
}
}catch(Exception ex) {
boundConnections=null;
freeConnections=null;
throw new SQLException(ex.toString());
}
}
public void closeDB() throws SQLException {
if(boundConnections!=null) {
for(Enumeration e=boundConnections.elements();e.hasMoreElements();) {
Connection conn=(Connection)e.nextElement();
conn.close();
}
boundConnections.clear();
boundConnections=null;
}
if(freeConnections!=null) {
for(Enumeration e=freeConnections.elements();e.hasMoreElements();) {
Connection conn=(Connection)e.nextElement();
conn.close();
}
freeConnections.removeAllElements();
freeConnections=null;
}
}
public synchronized Connection getConnection() throws SQLException {
if(freeConnections==null) {
throw new SQLException("The connection pool has not been established yet.");
}
if(boundConnections.get(Thread.currentThread())!=null)
throw new SQLException("Cannot get connections over once for this current running thread.");
try {
if(freeConnections.size()==0)
wait();
}catch(InterruptedException ex) {
throw new SQLException(ex.toString());
}
Connection conn=(Connection)freeConnections.firstElement();
freeConnections.removeElement(conn);
boundConnections.put(Thread.currentThread(),conn);
return conn;
}
public synchronized void returnConnection() throws SQLException {
Connection conn=(Connection)boundConnections.remove(Thread.currentThread());
if(conn==null)
throw new SQLException("The connection which this current running thread got isnot found.");
freeConnections.addElement(conn);
notify();
}
public void setMaxConnections(int numConnections) {
maxConnections=numConnections;
}
public void setDriverName(String drvName) {
driverName=drvName;
}
public void setJdbcURL(String url) {
jdbcURL=url;
}
public void setUserName(String uname) {
username=uname;
}
public void setPassword(String passwd) {
password=passwd;
}
}
private static final int defaultMaxConnections=3;//默认最大连接对象数;
private Vector freeConnections;//存放还未被配置出去的连接对象;
private Hashtable boundConnections;//存放目前正被使用的连接对象;
private String driverName;
private String jdbcURL;
private String username;
private String password;
private int maxConnections;//最大连接对象数; public ConnPool(int numConnections) {
maxConnections=numConnections;
boundConnections=null;
freeConnections=null;
driverName="";
jdbcURL="";
username="";
password="";
}
public ConnPool() {
this(defaultMaxConnections);
}
public void openDB(String drvName,String url,String uname,String passwd) throws SQLException {
try {
boundConnections=new Hashtable(maxConnections);
freeConnections=new Vector(maxConnections);
Class.forName(drvName);
for(int i=0;i<maxConnections;i++) {
freeConnections.addElement(DriverManager.getConnection(url,uname,passwd));
}
}catch(Exception ex) {
boundConnections=null;
freeConnections=null;
throw new SQLException(ex.toString());
}
}
public void closeDB() throws SQLException {
if(boundConnections!=null) {
for(Enumeration e=boundConnections.elements();e.hasMoreElements();) {
Connection conn=(Connection)e.nextElement();
conn.close();
}
boundConnections.clear();
boundConnections=null;
}
if(freeConnections!=null) {
for(Enumeration e=freeConnections.elements();e.hasMoreElements();) {
Connection conn=(Connection)e.nextElement();
conn.close();
}
freeConnections.removeAllElements();
freeConnections=null;
}
}
public synchronized Connection getConnection() throws SQLException {
if(freeConnections==null) {
throw new SQLException("The connection pool has not been established yet.");
}
if(boundConnections.get(Thread.currentThread())!=null)
throw new SQLException("Cannot get connections over once for this current running thread.");
try {
if(freeConnections.size()==0)
wait();
}catch(InterruptedException ex) {
throw new SQLException(ex.toString());
}
Connection conn=(Connection)freeConnections.firstElement();
freeConnections.removeElement(conn);
boundConnections.put(Thread.currentThread(),conn);
return conn;
}
public synchronized void returnConnection() throws SQLException {
Connection conn=(Connection)boundConnections.remove(Thread.currentThread());
if(conn==null)
throw new SQLException("The connection which this current running thread got isnot found.");
freeConnections.addElement(conn);
notify();
}
public void setMaxConnections(int numConnections) {
maxConnections=numConnections;
}
public void setDriverName(String drvName) {
driverName=drvName;
}
public void setJdbcURL(String url) {
jdbcURL=url;
}
public void setUserName(String uname) {
username=uname;
}
public void setPassword(String passwd) {
password=passwd;
}
}
解决方案 »
- 新手 问个jsp 的小问题,希望大家帮我解答下
- <a href="javascript:window.external.AddFavorite('http://www.hao123.com', 'wosay中文网')"> 中external为空,怎么回事,我用是IE7
- sql语句不太行,各位路过的帮下阿
- 企业的网站建设,用JSP语言。。
- 谁有Sturts教程啊,易懂的,20分一个!QQ:359635471
- 请大家帮忙运行一下这个小程序·不知道哪里的问题
- 请高手给我看看,applet向servlet发送对象时,在servlet中老是NULL!(在线等待)
- 网站开发与发布
- 为什么会有这样的错误??? 关于jsp中调用javabean的问题...???help me???
- form表单中enctype="multipart/form-data"的传值问题
- 请问作XML格式的页面用什么工具编写XSL文件呀?
- JSP的简单问题,给分
你可以去下载jive论坛去看看它的源码,jive里面有很好的连接池。
你要买本有JDBC的书籍,配合jive自己去研究吧。
如果你要简单我也可以给一个。你也只能是做为参考。
谢谢!!!!!!!!!!!!!!!!!!!!!!!!
2、Tomcat4中自带连接池功能,也可以用它做,但做成的应用不好移植到其他种类的JSP服务器。
能否给我发一个,最好告诉我怎么使用?
E-mail:[email protected]
谢谢先!!!!!!!!!!!!!!!!!!!!!!!!!!!
谢谢!!!
不好意思,没说清楚.本人正在做一个用jsp修改asp的项目.只有原代码,无任何文本.要求用Tomcat+jdk+sqlserver,尽量控制bean的数量,不能用servlet,本人只能逐页修改.近日发现数据库操作很慢,有时当结果集交叉使用时速度极慢,无连接池有一个OpenDB的Bean每页调用,请问如何解决?
谢谢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
JDBC Data Sources
0. Introduction
Many web applications need to access a database via a JDBC driver, to support the functionality required by that application. The J2EE Platform Specification requires J2EE Application Servers to make available a DataSource implementation (that is, a connection pool for JDBC connections) for this purpose. Tomcat 4 offers exactly the same support, so that database-based applications you develop on Tomcat using this service will run unchanged on any J2EE server.For information about JDBC, you should consult the following:http://java.sun.com/products/jdbc/ - Home page for information about Java Database Connectivity.
http://java.sun.com/j2se/1.3/docs/guide/jdbc/spec2/jdbc2.1.frame.html - The JDBC 2.1 API Specification.
http://java.sun.com/products/jdbc/jdbc20.stdext.pdf - The JDBC 2.0 Standard Extension API (including the javax.sql.DataSource API). This package is now known as the "JDBC Optional Package".
http://java.sun.com/j2ee/download.html - The J2EE Platform Specification (covers the JDBC facilities that all J2EE platforms must provide to applications).
NOTE - The default data source support in Tomcat supports Tyrex. However, it is possible to use any other connection pool that implements javax.sql.DataSource, by writing your own custom resource factory, as described below.1. Install Your JDBC Driver
Use of the JDBC Data Sources JNDI Resource Factory requires that you make an appropriate JDBC driver available to both Tomcat internal classes and to your web application. This is most easily accomplished by installing the driver's JAR file(s) into the $CATALINA_HOME/common/lib directory, which makes the driver available both to the resource factory and to your application.2. Declare Your Resource Requirements
Next, modify the web application deployment descriptor (/WEB-INF/web.xml) to declare the JNDI name under which you will look up preconfigured data source. By convention, all such names should resolve to the jdbc subcontext (relative to the standard java:comp/env naming context that is the root of all provided resource factories. A typical web.xml entry might look like this:
<resource-ref> <description> Resource reference to a factory for java.sql.Connection instances that may be used for talking to a particular database that is configured in the server.xml file. </description> <res-ref-name> jdbc/EmployeDB </res-ref-name> <res-type> javax.sql.DataSource </res-type> <res-auth> Container </res-auth></resource-ref>
WARNING - Be sure you respect the element ordering that is required by the DTD for web application deployment descriptors! See the Servlet Specification for details.3. Code Your Application's Use Of This Resource
A typical use of this resource reference might look like this:
Context initCtx = new InitialContext();Context envCtx = (Context) initCtx.lookup("java:comp/env");DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB");Connection conn = ds.getConnection();... use this connection to access the database ...conn.close();
Note that the application uses the same resource reference name that was declared in the web application deployment descriptor. This is matched up against the resource factory that is configured in $CATALINA_HOME/conf/server.xml, as described below.4. Configure Tomcat's Resource Factory
To configure Tomcat's resource factory, add an elements like this to the $CATALINA_HOME/conf/server.xml file, nested inside the Context element for this web application (or nested inside a DefaultContext element for the surrounding <Host> or <Engine> element.
<Context ...> ... <Resource name="jdbc/EmployeeDB" auth="Container" type="javax.sql.DataSource"/> <ResourceParams name="jdbc/EmployeeDB"> <parameter> <name>user</name> <value>dbusername</value> </parameter> <parameter> <name>password</name> <value>dbpassword</value> </parameter> <parameter> <name>driverClassName</name> <value>org.hsql.jdbcDriver</value> </parameter> <parameter> <name>driverName</name> <value>jdbc:HypersonicSQL:database</value> </parameter> </ResourceParams> ...</Context>
Note that the resource name (here, jdbc/EmployeeDB) must match the value specified in the web application deployment descriptor. Customize the value of the mail.smtp.host parameter to point at the server that provides SMTP service for your network.This example assumes that you are using the HypersonicSQL database JDBC driver. Customize the driverClassName and driverName parameters to match your actual database's JDBC driver and connection URL.
谢谢!!!!!!!!!!!!!!!!!!!!!!!!!