欢迎光临
我们一直在努力

jsp中实现连接池-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

在jsp里有两种实现的办法,一种是用jndi(java naming directory interface),这可能和应用服务器有关,如果是resin,先在resin.conf里定义

<resource-ref>

<res-ref-name>jdbc/oracle</res-ref-name>

<res-type>javax.sql.datasource</res-type>

<init-param driver-name="oracle.jdbc.driver.oracledriver"/>

<init-param url="jdbc:oracle:thin:@192.168.1.1:1521:oracle"/>

<init-param user="system"/>

<init-param password="manager"/>

<init-param max-connections="20"/>

<init-param max-idle-time="30"/>

</resource-ref>

如果为tomcat,在server.xml里面定义,有关的资料可以查文档,然后在jsp里这样用

try{

javax.naming.context env = (context)new initialcontext().lookup("java:comp/env");

javax.sql.datasource pool=(javax.sql.datasource) env.lookup("jdbc/oracle");

}catch(exception e){system.err.println("exception error:"+e.getmessage());}

try {

connection conn = pool.getconnection();

}catch(exception e){system.out.println("exception error:"+e.getmessage());}

通过这段代码,你就获得从连接池里获得了一个连接conn。如果想用普通的连接池,那只能用javabean了,先写一个connectionpool的java的类,然后直接从连接池中获得连接,下面是我一个连接池的javabean

connectionpool.java如下:

import java.io.printstream;

import java.sql.connection;

import java.util.vector;

// referenced classes of package com.ilovejsp.sql:

// datasource, pooledconnection

public class connectionpool

{

private vector pool;

private int size;

datasource db;

public connectionpool()

{

pool = null;

size = 0;

db = new datasource();

}

public void setsize(int value)

{

if(value > 1)

size = value;

}

public int getsize()

{

return size;

}

public synchronized void initpool()

throws exception

{

try

{

for(int x = 0; x < size; x++)

{

connection conn = db.getconnection();

if(conn != null)

{

pooledconnection pcon = new pooledconnection(conn);

addconnection(pcon);

}

}

}

catch(exception e)

{

system.err.println(e.getmessage());

}

}

private void addconnection(pooledconnection pcon)

{

if(pool == null)

pool = new vector(size);

pool.addelement(pcon);

}

public synchronized void releaseconnection(connection conn)

{

int x = 0;

do

{

if(x >= pool.size())

break;

pooledconnection pcon = (pooledconnection)pool.elementat(x);

if(pcon.getconnection() == conn)

{

system.err.println("release connection".concat(string.valueof(string.valueof(x))));

pcon.setinuse(false);

break;

}

x++;

}

while(true);

}

public synchronized connection getconnection()

throws exception

{

pooledconnection pcon = null;

for(int x = 0; x < pool.size(); x++)

{

pcon = (pooledconnection)pool.elementat(x);

if(!pcon.inuse())

{

pcon.setinuse(true);

return pcon.getconnection();

}

}

try

{

connection conn = db.getconnection();

pcon = new pooledconnection(conn);

pcon.setinuse(true);

pool.addelement(pcon);

}

catch(exception e)

{

system.err.println("exception error:".concat(string.valueof(string.valueof(e.getmessage()))));

}

return pcon.getconnection();

}

public synchronized void emptypool()

{

for(int x = 0; x < pool.size(); x++)

{

system.err.println("closing jdbc connection".concat(string.valueof(string.valueof(x))));

pooledconnection pcon = (pooledconnection)pool.elementat(x);

if(!pcon.inuse())

{

pcon.close();

continue;

}

try

{

thread.sleep(3000l);

pcon.close();

}

catch(exception e)

{

system.out.println("exception :".concat(string.valueof(string.valueof(e.getmessage()))));

}

}

db.close();

}

}

testpool.jsp内容如下:

<%@ page language="java" contenttype="text/html;charset=gb2312"%>

<%@ page import="java.sql.*"%>

<html>

<head>

<title>系统数据信息</title>

</head>

<body>

<%connectionpool db=new connectionpool();

connection conn=db.getconnection();

statement stmt=conn.createstatement();

string sql1="select * from pg_database ";

resultset rs=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>

<%string sql2="select * from pg_type";

rs=stmt.executequery(sql2);

while(rs.next()) {

%>

(<%=rs.getstring(1)%>)

<%}

rs.close();

db.close();%>

</tr></td>

</body>

</html>

 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » jsp中实现连接池-JSP教程,Jsp/Servlet
分享到: 更多 (0)