Implementing connection pooling in JSP

Source: Internet
Author: User
Tags connection pooling getmessage int size resource stmt
JS in the JSP has two ways to implement, one is using Jndi (Java naming Directory Interface), which may be related to the application server, if it is resin, first in resin.conf definition
<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= "/>"
<init-param max-idle-time= "/>"
</resource-ref>
If Tomcat is defined in Server.xml, the relevant information can be found in the document and then used in the 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 ());}
With this code, you get a connection conn from the connection pool. If you want to use a common connection pool, that can only use JavaBean, first write a ConnectionPool Java class, and then directly from the connection pool to obtain the connection, the following is my one connection pool JavaBean
Connectionpool.java is as follows:

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; + +)
{
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;
Todo
{
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 ();
{
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 ();
{
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 contents are as follows:
<%@ page language= "java" contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.sql.*"%>

<HTML>
<HEAD>
<TITLE> System Data Information </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> System Database Information </TD></TR>
<TR><TD>
<%while (Rs.next ()) {
%>
<%=rs.getstring (1)%>
<%}
Rs.close ();%>
</TR></TD>
<TABLE><TR><TD> system Field Information </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>
 





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.