?? dbpool.html
字號:
<html>
<head>
<title>Database Connection Pooling</title>
<meta name="Author" content="Giles Winstanley">
<meta name="keywords" content="DBPool, dbpool, database, jdbc, connection, pool, Pool, pooling, Pooling, connection pool, java, oracle, mysql">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="#D0D0D0">
<div align="center">
<table border="0" width="85%"><tr><td>
<!-- Start main page here -->
<div align="center">
<h1>DBPool - Java Database Connection Pooling</h1>
<p>Copyright © Giles Winstanley<br>
Last updated: 21st July 2006<br>
Latest version: 4.8.3</p>
</div>
<hr size="1">
<h2>What is DBPool?</h2>
<p>A Java-based database connection pooling utility, which supports time-based expiry and statement caching, connection validation, and easy configuration using a pool manager. Also included is a generic object pool which can be extended to create your own pools of custom types.</p>
<p><b>Licence Agreement</b><br>
Currently you may freely use DBPool in all applications, both commercial and non-commercial, provided it is used for the purpose intended as stated in this document. The source code is provided for reference, but you are not permitted in any way to use the code for purposes other than that which is intended. You are permitted to redistribute the original code on the condition that it is also distributed with the original documentation. If the code is changed in any way and then redistributed an acknowledgment to the source of the material must be made along with a link to the website (<a href="http://www.snaq.net">www.snaq.net</a>), <em>and it must be made plainly clear that the code has been modified from the original</em>. It is not permitted to distribute a modified version of DBPool for commercial purposes without explicit permission from the author.</p>
<p><b>Disclaimer</b><br>
Whilst the product has been developed with the utmost care, the author takes no responsibility for the failure of DBPool in either commercial or non-commercial operation.</p>
<h2>Why would I use it?</h2>
<p>Applications that make use of databases often need to frequently obtain connections to the database. For example, a popular website that is serving out information from a back-end database may need to obtain a database connection for each client who is requesting a page with their browser. To ensure the application is capable of responding to each client fast enough we need to profile the time spent performing each of it's tasks. One of the most expensive tasks involving accessing databases is the initial creation of the connection. Once the connection has been made the transaction often takes place very quickly. This is where the connection pool comes in, by retaining a pool of already-opened connections so the application can simply grab one when it needs to, use it, and then hand it back, without the long wait for the initial creation of the connection.</p>
<h2>Where can I get it?</h2>
<p>Select one of the links below to download the version you require.</p>
<ul>
<li><a href="DBPool_v4.8.3.zip">Download DBPool as ZIP</a> - contains JAR file & all documentation
<li><a href="DBPool_v4.8.3.jar">Download DBPool as JAR</a> and <a href="DBPool_v4.8.3_javadoc.zip">DBPool_javadoc.zip</a>
<li><a href="DBPool_v4.8.3_src.zip">Download DBPool source</a>
</ul>
<a name="Requirements"></a><h2>What do I need to use it?</h2>
<p>The latest version requires Java 1.4 or greater, supporting the JDBC 3.0 specification. Versions 4.x have been updated to support the JDBC 3.0 specification and are not JDBC 2.0 compatible. Version 5.0 is in progress which aims to provide support for both specifications, and will hopefully be released soon once final changes are finalised and the testing is completed.</p>
<a name="Support"></a><h2>What about support?</h2>
<p><b>Please make sure you have read this documentation before sending a support email.</b> I frequently receive support emails from people who seem to have simply been too lazy to read the documentation and find which bits are relevant to them. Unsurprisingly I am not overly helpful to those people, and point them back to the documentation. Please remember to check here first, as you may find an updated version of the library. If you still need to ask for help, <b>please send all support emails concerning DBPool to <a href="mailto:support@snaq.net">this address</a></b>, and include as much information as possible to help diagnose the problem, including log file, stack traces, and source code, and properties file where appropriate. I will endeavour to reply as soon as possible, but due to the nature of my work (a lot of travel) it is possible that I won't be able to reply quickly.</p>
<p>If you would like to be added to the DBPool mailing list list to receive notification of new versions when they are released, send an email to the <a href="mailto:support@snaq.net?subject=Mailing List Subscription">support address</a> with your email contact details, asking to be added to the list.</p>
<hr size="1">
<h2>How do I use it?</h2>
<p>To use DBPool you need to have the JAR file (inside the ZIP file if you downloaded that) in a location where it's available for use by the host system you are using. For standalone applications this is usually simply within the CLASSPATH, but with many application servers a specific directory is recommended for JAR libraries. (For example, when used with Apache Tomcat it can be placed in the <webapp>/WEB-INF/lib directory.)</p>
<p>Usually DBPool is used in two different ways:</p>
<ol>
<li>Direct use of individual connection pools.
<li>Using the ConnectionPoolManager to manage multiple connection pools.
</ol>
<p>If you have never used DBPool before it's recommended that you start by simply integrating a <a href="#ConnectionPool">single connection pool</a> into your application/applet to see how it works and performs. This provides the simplest direct support for pooled database connections and will get you up and running quickly. Once you learn exactly how it behaves and the benefits it can give you can try the <a href="#ConnectionPoolManager">pool manager</a> approach to manage multiple pools if necessary.</p>
<p>If you require connection pooling for an application server hosted project such as a web application it is recommended that you use the <a href="#ConnectionPoolManager">pool manager</a>. This allows you to define the pooling parameters in an external file which allows you to change the parameters without recompilation of a possibly large project.</p>
<hr size="1">
<a name="ConnectionPool"></a><h3>Using a ConnectionPool</h3>
<p>Direct use of ConnectionPool objects can provide substantial performance gains for applications with minimum changes to the previous non-pooling code. A single ConnectionPool object provides a centralized location for access to connections to a single database with specific authentication credentials and parameters.</p>
<p>To create a single connection pool use a line like the following:</p>
<pre>
ConnectionPool pool = new ConnectionPool(<poolname>,
<maxpool>,
<maxconn>,
<expiry>,
<url>,
<username>,
<password>);
</pre>
<p>or...</p>
<pre>
ConnectionPool pool = new ConnectionPool(<poolname>,
<maxpool>,
<maxconn>,
<expiry>,
<url>,
<properties>);
</pre>
<p>For example, to create a connection pool to access a database using the Oracle "thin" driver you could do something similar to this:</p>
<pre>
String url = "jdbc:oracle:thin:@myDB.myISP.com:1521:test";
ConnectionPool pool = new ConnectionPool("local",
10,
30,
180000, // milliseconds
url,
"b_lightyear",
"BeyondInfinity");
</pre>
<p>To obtain a Connection object from the pool and use it you can now do this:</p>
<pre>
Connection con = null;
long timeout = 2000; // 2 second timeout
try
{
con = pool.getConnection(timeout);
if (con != null)
<i>...use the connection...</i>
else
<i>...do something else (timeout occurred)...</i>
}
catch (SQLException sqle)
{
<i>...deal with exception...</i>
}
finally
{
try { con.close(); }
catch (SQLException e) { ... }
}
</pre>
<p>Once you are finished with the entire connection pool you should release the resources held by the pool:</p>
<pre>
pool.release();
</pre>
<p>In addition to this basic use, you can initialize a number of connections within the pool. This is useful on applications startup, for instance, when you would like to create the connections so the first users to access the database don't have to wait for a connection to be created. It is advisable, and it makes the most sense, to do this just after creation of the ConnectionPool object:</p>
<pre>
String url = "jdbc:mysql://localhost:3306/homeDB";
ConnectionPool pool = new ConnectionPool("local", 10, 20, 0, url, "Nemo", "LuckyFin");
pool.init(10);
</pre>
<p>The example above will initialize all the available connections in the pool. The connections are created in a seperate thread, so that the application can get on with other things while the initialization is performed in the background.</p>
<hr size="1">
<a name="ConnectionPoolManager"></a><h3>Using the ConnectionPoolManager</h3>
<p>The ConnectionPoolManager provides comprehensive support for external definition of the behaviour of each connection pool, and additionally can manage multiple pools easily. (Better still, it even has integral support for multiple pool managers, allowing you to define pools from multiple sources, but this is seldom required.)</p>
<p>Conceptually a single pool manager provides access to a number of ConnectionPool objects, each of which provides access to a user-specified database source. For each pool manager the user specifies the JDBC drivers required, the log file for output, and the parameters for each connection pool. With this information the pool manager registers the necessary JDBC drivers and creates the pools ready for use. The log file shows a trace of the manager's activity, along with the activity of each of it's pools.</p>
<p>To use a pool manager you first need to obtain an instance of ConnectionPoolManager. This can be done in several different ways depending on your exact requirements, but the two easiest options are:</p>
<pre>
ConnectionPoolManager.getInstance();
ConnectionPoolManager.getInstance(File);
</pre>
<p>The <code>getInstance()</code> method returns an instance defined by the default properties file, which needs to be within the CLASSPATH (or equivalent location for specific host system). The <code>getInstance(File)</code> method returns an instance defined by the file specified. When either of these methods is called it either returns the requested instance immediately (if it already exists), or first creates the pools required and then returns the instance.</p>
<p>It is also possible to define properties using a user-specified file within the CLASSPATH, or even using a Properties object. Each option is explained in the following table:</p>
<blockquote>
<table border="1" cellspacing="0" cellpadding="3">
<tr>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -