📄 conpool.html
字号:
InstantiationException,
IllegalAccessException,
SQLException {
DriverManager.registerDriver(this);
Class.forName(driver).newInstance();
pool = new JDCConnectionPool(url, user, password);
}
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
When the calling program needs a database connection,
it calls the <CODE>JDCConnectionDriver.connect</CODE> method,
which in turn, calls the <CODE>JDCConnectionPool.getConnection</CODE>
method.
<A NAME="pool"></A>
<H3>Connection Pool</H3>
The <A HREF="./Code/JDCConnectionPool.java">JDCConnectionPool.java</A>
class makes connections available to calling program in its
<CODE>getConnection</CODE> method. This method searches for an
available connection in the connection pool. If no connection
is available from the pool, a new connection is created. If a connection
is available from the pool, the <CODE>getConnection</CODE> method leases
the connection and returns it to the calling program.
</FONT>
<PRE>
public synchronized Connection getConnection()
throws SQLException {
JDCConnection c;
for(int i = 0; i < connections.size(); i++) {
c = (JDCConnection)connections.elementAt(i);
if (c.lease()) {
return c;
}
}
Connection conn = DriverManager.getConnection(
url, user, password);
c = new JDCConnection(conn, this);
c.lease();
connections.addElement(c);
return c;
}
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
The <A HREF="./Code/JDCConnection.java">JDCConnection.java</A> class
represents a JDBC connection in the connection pool, and is
essentially a wrapper around a real JDBC connection. The
<CODE>JDCConnection</CODE> object maintains a state flag to
indicate if the connection is in use and the time
the connection was taken from the pool. This time is used
by the <CODE>ConnectionReaper.java</CODE> class to
identify hanging connections.
<A NAME="dead"></A>
<H3>Deadlocks and Hangs</H3>
While many client and server databases have graceful ways
to handle deadlocks and hangs so you do not have to
write code to handle these situations, many of the newer,
lightweight distributed databases are not so well equipped.
The connection pool class provides a dead connection reaper
to handle such situations.
<P>
The <A HREF="./Code/JDCConnectionPool.java">ConnectionReaper</A>
class decides a connection is dead if the following conditions are
met.
<UL>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The connection is flagged as being in use.</FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The connection is older than a preset connection time out.</FONT>
<LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">The connection fails a validation check. </FONT>
</UL>
The validation check runs a simple SQL query over the connection to see
if it throws an exception. In this example, the validation method requests
the high-level description of the database tables.
If a connection fails the validation test, it is closed,
a new connection is initiated to the database, and added to the
connection pool.
</FONT>
<PRE>
public boolean validate() {
try {
conn.getMetaData();
}catch (Exception e) {
return false;
}
return true;
}
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A NAME="connect"></A>
<H3>Closing Connections</H3>
The connection is returned to the connection pool when
the calling program calls the
<CODE>JDCConnection.close</CODE> method in its <CODE>finally</CODE>
clause.
</FONT>
<PRE>
public void close() throws SQLException {
pool.returnConnection(this);
}
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<A NAME="example"></A>
<H3>Example Application</H3>
You use a connection pool in an application in a similar way to
how you would use any other JDBC driver. Here is the code for
a Bean-managed
<A HREF="./Code/conpool/RegistrationBean.java">RegistrationBean</A>.
This
<CODE>RegistrationBean</CODE> is adapted from
the auction house Enterprise JavaBeans<FONT SIZE="-2"><SUP>TM</SUP></FONT> example described
in Chapters 1 - 3.
<P>
When the first <CODE>RegistrationBean</CODE> object is created, it
creates one static instance of the <CODE>JDCConnectionDriver</CODE>
class. This static driver object registers itself with the
<CODE>DriverManager</CODE> in the <CODE>JDCConnectionDriver</CODE>
constructor making it available for connection requests
to all <CODE>RegistrationBean</CODE> objects created by the
client application.
<P>
Passing the URL as <CODE>jdbc:jdc:jdcpool</CODE> in the
<CODE>getConnection</CODE> method, lets the <CODE>DriverManager</CODE>
match the <CODE>getConnection</CODE> request to the registered driver.
The <CODE>DriverManager</CODE> uses simple <CODE>String</CODE> matching to
find an available driver that can handle URLs in that format.
</FONT>
<PRE>
public class RegistrationBean implements EntityBean{
private transient EntityContext ctx;
public String theuser, password;
public String creditcard, emailaddress;
public double balance;
//Static class instantiation
static {
try{
new pool.JDCConnectionDriver(
"COM.cloudscape.core.JDBCDriver",
"jdbc:cloudscape:ejbdemo",
"none", "none");
}catch(Exception e){}
}
public Connection getConnection()
throws SQLException{
return DriverManager.getConnection(
"jdbc:jdc:jdcpool");
}
}
</PRE>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif">
<P ALIGN="RIGHT">
<FONT SIZE="-1">[<A HREF="#top">TOP</A>]</FONT>
</FONT>
</TD>
</TR>
</TABLE>
<!-- ================ -->
<!-- End Main Content -->
<!-- ================ -->
</TD>
</TR>
</TABLE>
<!-- Copyright Insert -->
<BR CLEAR="ALL">
<FORM ACTION="/cgi-bin/search.cgi" METHOD="POST">
<TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5">
<TR>
<TD VALIGN="TOP">
<P ALIGN=CENTER>
<FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif">
[ This page was updated: <!-- new date --> 13-Oct-99 ]</font></P>
</TD>
</TR>
<TR>
<TD BGCOLOR="#CCCCCC">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD>
<CENTER>
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="http://java.sun.com/products/">Products & APIs</A> |
<A HREF="/developer/index.html">Developer Connection</A> |
<A HREF="/developer/infodocs/index.shtml">Docs & Training</A> |
<A HREF="/developer/support/index.html">Online Support</A><BR>
<A HREF="/developer/community/index.html">Community Discussion</A> |
<A HREF="http://java.sun.com/industry/">Industry News</A> |
<A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> |
<A HREF="http://java.sun.com/casestudies">Case Studies</A>
</FONT>
</CENTER>
</TD>
</TR>
<TR>
<TD BGCOLOR="#CCCCCC">
<IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
</TR>
<TR>
<TD ALIGN="CENTER">
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
<A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> -
<A HREF="http://java.sun.com/applets/">Applets</A> -
<A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> -
<A HREF="http://java.sun.com/jobs/">Employment</A> -
<A HREF="http://java.sun.com/nav/business/">Business & Licensing</A> -
<A HREF="http://java.sun.com/javastore/">Java Store</A> -
<A HREF="http://java.sun.com/casestudies/">Java in the Real World</A>
</FONT>
</TD>
</TR>
<TR>
<TD>
<CENTER>
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
<a href="/siteinfo/faq.html">FAQ</a> |
<a href="/feedback/index.html">Feedback</a> |
<a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> |
<A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A>
</FONT>
</CENTER>
</TD>
</TR>
<TR>
<TD>
<TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0">
<TR>
<TD WIDTH="50%">
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
For more information on Java technology<BR>
and other software from Sun Microsystems, call:<BR>
</FONT>
<FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif">
(800) 786-7638<BR></FONT>
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
Outside the U.S. and Canada, dial your country's
<A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&T Direct Access Number</A> first.<BR>
</FONT>
</TD>
<TD ALIGN="RIGHT" WIDTH="50%">
<A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR>
<FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
Copyright © 1995-99
<A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR>
All Rights Reserved.
<a href="http://www.sun.com/share/text/SMICopyright.html">Legal Terms</a>.
<A HREF="http://www.sun.com/privacy/">Privacy Policy</A>.
</FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
</FORM>
<!-- End Copyright Insert -->
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -