In many situations, such as web applications, there will be a large number of small database
transactions. When this is the case, it usually makes sense to create a pool of database connections
available for web requests as needed. Although MySQL does not spawn an extra process when a
connection is made, there is still a small amount of overhead to create and set up the connection.
Pooling of connections also alleviates problems such as collecting large amounts of sockets in the
TIME_WAIT
Setting up pooling of MySQL connections with Spring is as simple as changing the data source
configuration in the application context. There are a number of configurations that we can use. The
first example is based on the
configuration that was based on
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.jdbcurl}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="initialSize" value="3"/>
</bean>
The configuration of the two solutions is very similar. The difference is that DBCP will pool connections
to the database instead of creating a new connection every time one is requested. We have also set
a parameter here called initialSize. This tells DBCP that we want three connections in the pool
when it is created.
Another way to configure connection pooling is to configure a data source in our J2EE application
server. Using JBoss as an example, you can set up the MySQL connection pool by creating a file
called
mysql-local-ds.xml
have this setup, we can use JNDI to look it up. With Spring, this lookup is very simple. The data source
configuration looks like this.
<jee:jndi-lookup id="dataSource" jndi-name="java:MySQL_DS"/>
20.3.14. Using Connector/J with GlassFish
This section explains how to use MySQL Connector/J with Glassfish ™ Server Open Source Edition
3.0.1. Glassfish can be downloaded from the
Once Glassfish is installed you will need to make sure it can access MySQL Connector/J. To do this
copy the MySQL Connector/J JAR file to the directory GLASSFISH_INSTALL/glassfish/lib. For
example, copy
\lib. Restart the Glassfish Application Server.
You are now ready to create JDBC Connection Pools and JDBC Resources.
Creating a Connection Pool
1. In the Glassfish Administration Console, using the navigation tree navigate to Resources, JDBC,
Connection Pools.
2. In the JDBC Connection Pools frame click New. You will enter a two step wizard.
3. In the Name field under General Settings enter the name for the connection pool, for example
enter MySQLConnPool.
4. In the Resource Type field, select
5. In the Database Vendor field, select
page of the wizard.
Using Connector/J with GlassFish
state.
Jakarta Commons DBCP
DriverManagerDataSource
and placing it in the server/default/deploy directory in JBoss. Once we
mysql-connector-java-5.1.12-bin.jar
library. The example below replaces the source
Glassfish
website.
javax.sql.DataSource
from the drop-down listbox. Click Next to go to the next
MySQL
2110
with DBCP's BasicDataSource.
to
C:\glassfishv3\glassfish
from the drop-down listbox.
Need help?
Do you have a question about the 5.0 and is the answer not in the manual?
Questions and answers