Posted In: Server, Tomcat
Tomcat 9 – Why you should start using org.apache.tomcat.jdbc.pool
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:
1. Multi threaded. Support for highly concurrent environments and multi core/cpu systems.
2. Tomcat jdbc pool implements the ability retrieve a connection asynchronously, without adding additional threads to the library itself.
3. 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.
4. Connections can be retrieved from a java.sql.Driver, javax.sql.DataSource or javax.sql.XADataSource This is achieved using the dataSource and dataSourceJNDI attributes.
5. XA connection support
6. Close connections after they have been connected for a certain time. Age based close upon return to the pool.
How to use org.apache.tomcat.jdbc.pool through context.xml
<Resource name="jdbc/javausecasedb" auth="Container" type="javax.sql.DataSource" factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" testWhileIdle="true" testOnBorrow="true" testOnReturn="false" validationQuery="SELECT 1" validationInterval="30000" timeBetweenEvictionRunsMillis="30000" maxActive="100" minIdle="10" maxWait="10000" initialSize="10" removeAbandonedTimeout="60" removeAbandoned="true" logAbandoned="true" minEvictableIdleTimeMillis="30000" jmxEnabled="true" jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState; org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer" username="root" password="password" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/javausecasedb" />
How to use org.apache.tomcat.jdbc.pool through Java code
import org.apache.tomcat.jdbc.pool.DataSource; import org.apache.tomcat.jdbc.pool.PoolProperties; PoolProperties p = new PoolProperties(); p.setUrl("jdbc:mysql://localhost:3306/javausecase_db"); p.setDriverClassName("com.mysql.jdbc.Driver"); p.setUsername("root"); p.setPassword("password"); DataSource datasource = new DataSource(); datasource.setPoolProperties(p); Connection conn = datasource.getConnection();
How to use XADataSource org.apache.tomcat.jdbc.pool through context.xml
<Resource factory="org.apache.tomcat.jdbc.naming.GenericNamingResourcesFactory" name="jdbc/xajavausecasedb" type="com.mysql.cj.jdbc.MysqlXADataSource" databaseName="javausecase_db" serverName="localhost" portNumber="3306" user="root" password="password" /> <Resource factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" dataSourceJNDI="xajavausecasedb" name="jdbc/xajavausecasedb" auth="Container" type="javax.sql.XADataSource" testWhileIdle="true" testOnBorrow="true" testOnReturn="false" validationQuery="SELECT 1" validationInterval="30000" timeBetweenEvictionRunsMillis="5000" maxActive="100" minIdle="10" maxIdle="20" maxWait="10000" initialSize="10" removeAbandonedTimeout="60" removeAbandoned="true" logAbandoned="true" minEvictableIdleTimeMillis="30000" jmxEnabled="true" jdbcInterceptors="ConnectionState;StatementFinalizer;SlowQueryReportJmx(threshold=10000)" abandonWhenPercentageFull="75" />
import javax.sql.XAConnection; import javax.sql.XADataSource; ..... @Resource(name = "jdbc/xajavausecasedb") private XADataSource ds; XAConnection conn = ds.getXAConnection();
COMMON ERRORS
Check type class
java.sql.SQLException: com.mysql.cj.jdbc.MysqlXADataSource cannot be cast to java.sql.Driver
- Apache (13)
- Build Tools (2)
- Gradle (2)
- Caching (1)
- cpanel (1)
- cURL (1)
- Database (7)
- Hibernate (5)
- Java Core (38)
- Java Script (15)
- Bootstrap (1)
- File Upload (7)
- jQuery (3)
- React (3)
- JEE (13)
- JSON (41)
- GSON (13)
- Jackson 1X (1)
- Jackson 2X (12)
- jsoniter (1)
- Logging (2)
- Apache Commons Logging (1)
- Apache Log4J (1)
- Logback (1)
- SLF4J (1)
- MongoDB (1)
- OS (1)
- Linux (1)
- Security (5)
- Server (4)
- Tomcat (4)
- Service (2)
- Micro (2)
- Spring (46)
- Pattern (2)
- Spring Boot (20)
- Spring Data (4)
- Spring MVC (8)
- Spring REST (13)
- Spring Security (7)
- Testing (11)
- XML (5)
- JDOM XML Parser (1)