Introduction
The JDBC Connection Pool org.apache.tomcat.jdbc.pool
is a replacement or an alternative to the Apache Commons DBCP connection pool.
So why do we need a new connection pool?
Here are a few of the reasons:
- Commons DBCP 1.x is single threaded. In order to be thread safe Commons locks the entire pool for short periods during both object allocation and object return. Note that this does not apply to Commons DBCP 2.x.
- Commons DBCP 1.x can be slow. As the number of logical CPUs grows and the number of concurrent threads attempting to borrow or return objects increases, the performance suffers. For highly concurrent systems the impact can be significant. Note that this does not apply to Commons DBCP 2.x.
- Commons DBCP is over 60 classes. tomcat-jdbc-pool core is 8 classes, hence modifications for future requirement will require much less changes. This is all you need to run the connection pool itself, the rest is gravy.
-
Commons DBCP uses static interfaces. This means you have to use the right
version for a given JRE version or you may see
NoSuchMethodException
exceptions. - It's not worth rewriting over 60 classes, when a connection pool can be accomplished with a much simpler implementation.
- Tomcat jdbc pool implements the ability retrieve a connection asynchronously, without adding additional threads to the library itself.
- Tomcat jdbc pool is a Tomcat module, it depends on Tomcat JULI, a simplified logging framework used in Tomcat.
-
Retrieve the underlying connection using the
javax.sql.PooledConnection
interface. - Starvation proof. If a pool is empty, and threads are waiting for a connection, when a connection is returned, the pool will awake the correct thread waiting. Most pools will simply starve.
Features added over other connection pool implementations
- Support for highly concurrent environments and multi core/cpu systems.
-
Dynamic implementation of interface, will support
java.sql
andjavax.sql
interfaces for your runtime environment (as long as your JDBC driver does the same), even when compiled with a lower version of the JDK. - Validation intervals - we don't have to validate every single time we use the connection, we can do this when we borrow or return the connection, just not more frequent than an interval we can configure.
- Run-Once query, a configurable query that will be run only once, when the connection to the database is established. Very useful to setup session settings, that you want to exist during the entire time the connection is established.
-
Ability to configure custom interceptors. This allows you to write custom
interceptors to enhance the functionality. You can use interceptors to
gather query stats, cache session states, reconnect the connection upon
failures, retry queries, cache query results, and so on. Your options are
endless and the interceptors are dynamic, not tied to a JDK version of
a
java.sql
/javax.sql
interface. - High performance - we will show some differences in performance later on
- Extremely simple, due to the very simplified implementation, the line count and source file count are very low, compare with c3p0 that has over 200 source files(last time we checked), Tomcat jdbc has a core of 8 files, the connection pool itself is about half that. As bugs may occur, they will be faster to track down, and easier to fix. Complexity reduction has been a focus from inception.
-
Asynchronous connection retrieval - you can queue your request for a
connection and receive a
Future<Connection>
back. - Better idle connection handling. Instead of closing connections directly, it can still pool connections and sizes the idle pool with a smarter algorithm.
- You can decide at what moment connections are considered abandoned, is it when the pool is full, or directly at a timeout by specifying a pool usage threshold.
-
The abandon connection timer will reset upon a statement/query activity.
Allowing a connections that is in use for a long time to not timeout. This
is achieved using the
ResetAbandonedTimer
- Close connections after they have been connected for a certain time. Age based close upon return to the pool.
-
Get JMX notifications and log entries when connections are suspected for
being abandoned. This is similar to the
removeAbandonedTimeout
but it doesn't take any action, only reports the information. This is achieved using thesuspectTimeout
attribute. -
Connections can be retrieved from a
java.sql.Driver
,javax.sql.DataSource
orjavax.sql.XADataSource
This is achieved using thedataSource
anddataSourceJNDI
attributes. - XA connection support
How to use
The Tomcat connection pool offers a few additional features over what most other pools let you do:
-
initSQL
- the ability to run an SQL statement exactly once, when the connection is created -
validationInterval
- in addition to running validations on connections, avoid running them too frequently. -
jdbcInterceptors
- flexible and pluggable interceptors to create any customizations around the pool, the query execution and the result set handling. More on this in the advanced section. -
fairQueue
- Set the fair flag to true to achieve thread fairness or to use asynchronous connection retrieval
A. Download File Sqljdbc4.jar (Copy File Sqljdbc4.jar to folder library)
Create File Context.xml
<Context path="/app" docBase="app" debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/app" auth="Container" type="javax.sql.DataSource"
username="user01"
password="123456"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=dbapps"
maxActive="20"
maxIdle="10"
validationQuery="select 1"/>
</Context>
Connection file jdbc pool. You can read this article :
http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html
D. Note
-
Create File XML Connection Java With SqlServer (JDBC) :
https://qr-notes.blogspot.com/2021/08/create-file-xml-connection-java-zk.html
-
Create File Java Connection :
https://qr-notes.blogspot.com/2021/09/create-file-java-connection-database.html
- Create Form Index.zul #Part 1 Simple For Beginner : https://qr-notes.blogspot.com/2020/06/java-web-zk-framework-sql-server-tips.html
-
Create Form Login :
https://qr-notes.blogspot.com/search?q=login
- Create Form Menu Side Bar : https://qr-notes.blogspot.com/2021/05/java-web-zk-framework-sql-server-tips.html
No comments:
Post a Comment