Connection pooling with MySQL connector/J

Source: Internet
Author: User
Tags apache tomcat

In last month's article, "Does MySQL connector/J support multithreaded programming? "), I covered the problems with sharing a JDBC connection between multiple threads.

Most of these problems can be overcome with a technique called "Connection pooling. "In addition to fixing the problems mentioned in the previous article, Connection Pooling generally leads to more predictable resource usage under load, and allows deployment of applications on smaller or less-powerful hardware than wocould be normally required.

What is Connection Pooling?

Connection pooling is a technique of creating and managing a pool of connections that are ready for use by any thread that needs them.

This technique of "pooling" connections is based on the fact that most applications only need a thread to have access to a JDBC connection when they are actively processing a transaction, which usually take only milliseconds to complete. when not processing a transaction, the connection wocould otherwise sit idle. instead, Connection pooling allows the idle connection to be used by some other thread to do useful work.

In practice, when a thread needs to do work against a MySQL or other database with JDBC, it requests a connection from the pool. when the thread is finished using the connection, it returns it to the pool, so that it may be used by any other threads that want to use it.

When the connection is "loaned out" from the pool, it is used exclusively by the thread that requested it. From a programming point of view, it is the same as if your thread calledDrivermanager. getconnection ()Every time it needed a JDBC connection, however with Connection pooling, your thread may end up using either a new, or already-existing connection.

What benefits are there to Connection Pooling?

Connection pooling can greatly increase the performance of your Java application, while bridging overall resource usage. The main benefits to Connection pooling are:

Reduced connection Creation Time

while this is not usually an issue with the quick connection setup that MySQL offers compared to other databases, creating connections still has networking and JDBC driver overhead that will be avoided if connections are "recycled. "

Simplified Programming Model

when using Connection pooling, each individual thread can act as though it has created its own JDBC connection, allowing you to use straight-forward JDBC programming techniques.

Controlled resource usage

if you don't use Connection pooling, and instead create a new connection every time a thread needs one, your application's resource usage can be quite wasteful and lead to unpredictable behavior under load.

Remember that each connection to MySQL has overhead (memory, CPU, context switches, etc) on both the client and server side. every connection limits how many resources there are available to your application as well as the MySQL server. usage of these resources will be used whether or not the connection is actually doing any useful work!

Connection pools can be tuned to maximize performance, while keeping resource utilization below the point where your application will start to fail rather than just run slower.

How do I implement Connection Pooling?

Luckily, Sun has standardized the concept of Connection pooling in JDBC through the JDBC-2.0 "optional" interfaces, and all major application servers have implementations of these APIs that work fine with MySQL connector/J.

generally, you configure a connection pool in your application server configuration files, and access it via the Java Naming and Directory Interface (JNDI ). the following code shows how you might use a connection pool:

Import Java. SQL. connection; import Java. SQL. sqlexception; import Java. SQL. statement; import javax. naming. initialcontext; import javax. SQL. datasource; public class myservletjsporejb {public void dosomething () throws exception {/** create a JNDI initial context to be able To * lookup the datasource ** in production-level code, this shoshould be cached as * an instance or static variable, as it can * be quite exp Ensive to create a JNDI context. ** Note: This Code only works when you are using servlets * or ejbs in a J2EE application server. if you are * using Connection pooling in standalone Java code, you * will have to create/configure CES using whatever * mechanic ISMs your particle Connection Pooling library * provides. */initialcontext CTX = new initialcontext ();/** lookup the datasource, which will Be backed by a pool * that the application server provides. datasource instances * are also a good candidate for caching as an instance * variable, as JNDI lookups can be expensive as well. */datasource DS = (datasource) CTX. lookup ("Java: COMP/ENV/jdbc/mysqldb");/** the following code is what wocould actually be in your * servlet, JSP or EJB 'service' method... where you need * to work with a JDBC connection. */Connection conn = NULL; statement stmt = NULL; try {conn = Ds. getconnection ();/** now, use normal JDBC programming to work with * MySQL, making sure to close each resource when you're * finished with it, which allows the connection pool * resources to be recovered as quickly as possible */stmt = conn.createstatement(;;stmt.exe cute ("Some SQL query"); stmt. close (); stmt = NULL; Conn. close (); Conn = NULL ;} Finally {/** close any JDBC instances here that weren't * explicitly closed during normal code path, so * That we don't 'leak' resources... */If (stmt! = NULL) {try {stmt. close ();} catch (sqlexception sqlex) {// ignore -- as we can't do anything about it here} stmt = NULL;} If (Conn! = NULL) {try {Conn. close ();} catch (sqlexception sqlex) {// ignore -- as we can't do anything about it here} conn = NULL ;}}}}

As shown in the example above, after obtaining the JNDI initialcontext, and looking up the datasource, the rest of the code shoshould look familiar to anyone who has done JDBC programming in the past.

The most important thing to remember when using Connection pooling is to make sure that no matter what happens in your code (exceptions, flow-of-control, etc), connections, and anything created by them (statements, result sets, etc) are closed, so that they may be re-used, otherwise they will be "Stranded, "which in the best case means that the MySQL server resources they represent (buffers, locks, sockets, etc) may be tied up for some time, or worst case, may be tied up forever.

What's the best size for my connection pool?

As with all other configuration rules-of-thumb, the answer is "It depends. "while the optimal size depends on anticipated load and average database transaction time, the optimum connection pool size is smaller than you might CT. if you take Sun's Java PetStore blueprint application for example, a connection pool of 15-20 connections can serve a relatively moderate load (600 concurrent users) using MySQL and tomcat with response times that are acceptable.

To correctly size a connection pool for your application, you should create load test scripts with tools such as Apache jmeter or the grinder, and load test your application.

An easy way to determine a starting point is to configure your connection pool's maximum number of connections to be "unbounded," run a load test, and measure the largest amount of concurrently used connections. you can then work backwards from there to determine what values of minimum and maximum pooled connections give the best performance for your particle application.

Connection Pooling Configuration documentation

Since the object of this article is not to discuss the specific connection pool configurations of varous application servers, I will leave you with the following pointers to read about logging ing connection pools with the most common open source application servers.

Until next time, have fun trying out Connection pooling with connector/J!

P.s. I m always on the lookout for Article ideas-if there's a particle ctor/J topic you wowould like to see covered, please E-mail me at mark@mysql.com!

Apache Tomcat 4.1

Place a copyMysql-connector-Java-[version]-bin. JarIn$ Catalina_home/common/lib/. Then, follow the instructions in the section MySQL DBCP example of the Tomcat documentation.

Notice that connector/J 3.0 and newer work with the same settings:Http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-datasource-examples-howto.html

Tomcat uses the Apache Software Foundation's DBCP libraries, which you can also use in your own non-J2EE code:Http://jakarta.apache.org/commons/dbcp/

JBoss

Place a copyMysql-connector-Java-[version]-bin. JarIn$ Jboss_home/Server/All/lib. Then, follow the example configuration file namedMysql-ds.xmlIn$ Jboss_home/docs/examples/JCADirectory that comes with a JBoss binary installation. to activate your datasource, place an XML file that follows the formatMysql-ds.xmlInDeploySubdirectory in either$ Jboss_home/Server/all,$ Jboss_home/Server/Default, Or$ Jboss_home/Server/[yourconfig]As appropriate.

Jetty with the "plus" Package

PutMysql-connector-Java-[version]-bin. JarIn$ Jetty_home/lib/extra/EXT, And then follow the directions locatedHttp://jetty.mortbay.org/jetty/plus/To configure a datasource. UseCom. MySQL. JDBC. DriverFor the JDBC driver classname, and follow the jdbc url format that is given in the README that comes with connector/J.

Orionserver

To use Connection pooling with orionserver, putMysql-connector-Java-[version]-bin. JarIn$ Orion_home/libAnd add the following to data-sources.xml:

<Data-sourcename = "mysqlds" location = "JDBC/mysqlpooledds" class = "com. mySQL. JDBC. jdbc2.optional. mysqlconnectionpooldatasource "Max-connections =" 100 "Min-connections =" 5 "inactivity-Timeout =" 200 "Wait-Timeout =" 10 "username =" username "Password =" password" url = "JDBC: mySQL: // localhost: 3306/test? Useunicode = true & amp; characterencoding = cp1251 "/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.