jsp中實現串連池

來源:互聯網
上載者:User
js 在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>
 





相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。