jsp中实现连接池(Connection Pool) 发布日期:2002-7-9 发 布 者:51jsp.net |
在JSP里有两种实现的办法,一种是用JNDI(JavaNamingDirectoryInterface),这可能和应用服务器有关,如果是Resin,先在resin.conf里定义 <resource-ref> <res-ref-name>jdbc/oracle</res-ref-name> <res-type>javax.sql.DataSource</res-type> <init-paramdriver-name="oracle.jdbc.driver.OracleDriver"/> <init-paramurl="jdbc:oracle:thin:@192.168.1.1:1521:oracle"/> <init-paramuser="system"/> <init-parampassword="manager"/> <init-parammax-connections="20"/> <init-parammax-idle-time="30"/> </resource-ref> 如果为Tomcat,在Server.xml里面定义,有关的资料可以查文档,然后在jsp里这样用 try{ javax.naming.Contextenv=(Context)newInitialContext().lookup("java:comp/env"); javax.sql.DataSourcepool=(javax.sql.DataSource)env.lookup("jdbc/oracle"); }catch(Exceptione){System.err.println("Exceptionerror:"+e.getMessage());} try{ Connectionconn=pool.getConnection(); }catch(Exceptione){System.out.println("Exceptionerror:"+e.getMessage());} 通过这段代码,你就获得从连接池里获得了一个连接conn。如果想用普通的连接池,那只能用JavaBean了,先写一个ConnectionPool的java的类,然后直接从连接池中获得连接,下面是我一个连接池的JavaBean ConnectionPool.java如下: importjava.io.PrintStream; importjava.sql.Connection; importjava.util.Vector; //Referencedclassesofpackagecom.ilovejsp.sql: //DataSource,PooledConnection publicclassConnectionPool { privateVectorpool; privateintsize; DataSourcedb; publicConnectionPool() { pool=null; size=0; db=newDataSource(); } publicvoidsetSize(intvalue) { if(value>1) size=value; } publicintgetSize() { returnsize; } publicsynchronizedvoidinitPool() throwsException { try { for(intx=0;x<size;x++) { Connectionconn=db.getConnection(); if(conn!=null) { PooledConnectionpcon=newPooledConnection(conn); addConnection(pcon); } } } catch(Exceptione) { System.err.println(e.getMessage()); } } privatevoidaddConnection(PooledConnectionpcon) { if(pool==null) pool=newVector(size); pool.addElement(pcon); } publicsynchronizedvoidreleaseConnection(Connectionconn) { intx=0; do { if(x>=pool.size()) break; PooledConnectionpcon=(PooledConnection)pool.elementAt(x); if(pcon.getConnection()==conn) { System.err.println("ReleaseConnection".concat(String.valueOf(String.valueOf(x)))); pcon.setInUse(false); break; } x++; } while(true); } publicsynchronizedConnectiongetConnection() throwsException { PooledConnectionpcon=null; for(intx=0;x<pool.size();x++) { pcon=(PooledConnection)pool.elementAt(x); if(!pcon.inUse()) { pcon.setInUse(true); returnpcon.getConnection(); } } try { Connectionconn=db.getConnection(); pcon=newPooledConnection(conn); pcon.setInUse(true); pool.addElement(pcon); } catch(Exceptione) { System.err.println("Exceptionerror:".concat(String.valueOf(String.valueOf(e.getMessage())))); } returnpcon.getConnection(); } publicsynchronizedvoidemptyPool() { for(intx=0;x<pool.size();x++) { System.err.println("ClosingJdbcConnection".concat(String.valueOf(String.valueOf(x)))); PooledConnectionpcon=(PooledConnection)pool.elementAt(x); if(!pcon.inUse()) { pcon.close(); continue; } try { Thread.sleep(3000L); pcon.close(); } catch(Exceptione) { System.out.println("Exception:".concat(String.valueOf(String.valueOf(e.getMessage())))); } } db.close(); } } testpool.jsp内容如下: <%@pagelanguage="java"contentType="text/html;charset=gb2312"%> <%@pageimport="java.sql.*"%> <HTML> <HEAD> <TITLE>系统数据信息</TITLE> </HEAD> <BODY> <%ConnectionPooldb=newConnectionPool(); Connectionconn=db.getConnection(); Statementstmt=conn.createStatement(); Stringsql1="select*frompg_database"; ResultSetrs=stmt.executeQuery(sql1); %> <TABLE><TR><TD>系统数据库信息</TD></TR> <TR><TD> <%while(rs.next()){ %> <%=rs.getString(1)%> <%} rs.close();%> </TR></TD> <TABLE><TR><TD>系统字段信息</TD></TR> <TR><TD> <%Stringsql2="select*frompg_type"; rs=stmt.executeQuery(sql2); while(rs.next()){ %> (<%=rs.getString(1)%>) <%} rs.close(); db.close();%> </TR></TD> </BODY> </HTML> |
jsp中实现连接池(Connection Pool)
最新推荐文章于 2021-02-16 02:44:12 发布