![]()
|
|||||||
OOP PooledStatement : Pooling Mechanism of JDBC PreparedStatement / Thread Safe JSP or Servlet on Apache Tomcat
With a few lines of code, JSP or Servlets on Apache Tomcat can use JDBC PreparedStatement. The pool of JDBC PreparedStatement makes JSP or Servlets on Apache Tomcat truly Thread Safe.
Putting it all togather : OOP Bento frameworkThis Java API is now a part of OOP Bento framework. In this MVC framework by XML, you can write all the GUIs in the web application in the normal HTML, XHTML, WML, HDML or any other mark-up languages. And you no longer need to bother about the user authentication and the session tracking. Because its full working source code example will be the good starting point of this Java API, please also visit the web site of Bento framework. Related Pages:
|
|||||||
For details about JDBC, please visit JDBC (TM) Home page. What is it?
The main class of this API is PooledPS. The instance of PoolesPS caches the pairs of JDBC Connection and JDBC PreparedStatement in its pools.
There may be a case that SQL database does not support pre-compilation of queries, even if its JDBC driver implements JDBC PreparedStatement. For example, MM.Mysql Driver is the complete JDBC 2.0 implementation, but MySQL database itself does not actually cache the pre-compiled queries. Thus, it is not guaranteed that the pre-compiled queries are always cached on the database side. If it is true for your database, PooledPS does not necessarily result in the significant performance improvement. (Please refer to the documentation of your SQL database and its JDBC driver.) But even in such a case, re-using the JDBC PreparedStatement instances will result in the higher performance to some extent.
Are you realy Thread Safe?Your JSP or Servlet on Apache Tomcat should prepare for a case that multiple requests arrive in concurrently. As you know, this can be done either by:
If SingleThreadModel interface is implemented, the JSP/Servlets server (for example, Apache Tomcat) will solve the thread issue by:
But, is it enough? Is your Servlets or JSP on Apache Tomcat is realy ready for the concurrent request? The answer is in most cases YES, but sometimes NO. Please imagine if 100 requests are dispatched to your Servlet. Its service( ) method is implemented in a thread safe way, and service( ) method accesses to the SQL database through JDBC. Each thread will establish the JDBC Connection to the SQL database, which results in at most a hundreds of JDBC Connections. Is this what you really intended? It may depend on the amount of system resource allocated to your environment, but you may want to avoid a hundreds of JDBC Connections at a time. If it is true for you, how can you prevent this situation? But, don't worry. Voilà, OOP PooledStatement! It is easy to restrict the number of the JDBC PreparedStatement per each SQL statement. The maximum number of JDBC PreparedStatement per each SQL statement can be specified in the property resource file. Assume that we set it to 2 and some thread (Thread1) is just going to invoke getPS("..."): ![]() If the 2 free instances of JDBC PreparedStatement are available in the pool for the specified SQL statement at this time, one of them (the older one will be selected, i.e. by the first-in first-out rule) will be passed to the caller thread: ![]() And when the subsquent request for the same SQL statement is dispatched by another thread (Thread2), another free instance of JDBC PreparedStatement is still available in the pool: ![]() So another one will be passed to the caller thread: ![]() As you notice, the pool for the same SQL statement is empty after the Thread2 got the remaining instance. If one more another request for the same SQL statement is dispatched by the third thread (Thread3) at this moment, no more instance of JDBC PreparedStatement will be created, because we set the maximum number per each SQL statement to 2. And the Thread3 will be suspended: ![]() The Thread3 will stay suspended until some instance of JDBC PreparedStatement will be put back into the pool by another thread. For example, if the Thread1 push the instance back to the pool, the Thread3 can take over it: ![]()
Easy programming interface
Thus, PooledPS takes care of all the tasks related with JDBC Connection and JDBC PreparedStatement. And the source code of your Java program can be as simple as possible. It looks like this:
To ensure the integrity of the data, we should invoke close( ) both on JDBC PreparedStatement and JDBC Connection. This can be done by invoking close( ) on the instance of PooledPS. Then, the instance of PooledPS invokes close( ) on all the JDBC PreparedStatement and JDBC Connection in its pools. Another method, closeIfOpen( ), the static version of close( ) is responsible for the same task. For example, your Servlets should implement destroy( ) as follows:
Or, in case of JSP:
will be enough. Because the JSP/Servlets server, such as Apache Tomcat, invokes destroy( ) on all the instances of Servlets and jspDestroy( ) on those of JSP, you can sure that the JDBC Connections to your SQL server are closed before exiting JVM. For details, please read the API documentation of OOP PooledStatement
The max age of PreparedStatement
To ensure the integrity of the JDBC PreparedStatement and JDBC Connection, it is desirable to restrict the max age of the JDBC PreparedStatement. PooledPS is also responsible for keeping only the young JDBC PreparedStatement in its pools, i.e. when some JDBC PreparedStatement reaches the specified age since the last access, PooledPS discards it.
Documentation
Before you start to develop your JSP/Servlet by this API, please read the Tips for the Java API from OOP-Research.
For details, please read
the API documentation of OOP PooledStatement
RequirementThe following API is required :
DownloadThis API is now a part of Bento framework. There are the better documentations and source code examples on the web site for Bento framework. There, you will also find the link to download the entire framework or each API in it. |
|
||||||
|
Java and all Java-based trademarks and logos are trademarks or registered of Sun Microsystems, Inc. in the United States and other countries. |
|||||||
|
ALL CONTENTS COPYRIGHT 1997-2007, OOP-Research Corporation. All rights reserved. |
|||||||