Database connection pool This concept should be familiar, in Java connection pool is the database connection pool, it is a use of the idea of connection reuse to avoid multiple connections caused by the waste mechanism of resources .
The most common connection pools are dbcp and c30p, the DBCP connection pool that is used by default in Tomcat, and c30p is used by default in Hibernate. Their difference for the user is most obvious, by default, DBCP does not provide free connection release, need to manually open.
The following describes the configuration and use of the data connection pool in Tomcat.
Introduced
This article relies on a concept--jndi, you can refer to the previous blog: JNDI resource details.
For Jndi, it can be easily understood as a resource pool in Tomcat, with some unique names that correspond to specific resources, like a map, which can be simply obtained by name.
Then the JNDI data source in this article is to get to the database connection by this name in the application by configuring the resource of a data source. This saves you the steps to connect to the database every time.
Connection Pooling principle
The concept of connection pooling should be no stranger. Some of the content can be consulted: several mainstream connection pools
The simple explanation here is that if you use connection pooling in your app alone, you might just create a connection pool when your app is running. The Tomcat configuration data source can initialize the connection pool when the Tomcat container starts, and release the resource when the Tomcat is stopped, and its deployed app can share the resource in the app based on the Jndi declaration.
So one is the connection pool in the application (that is, a different business in an application uses the connection pool, such as registering a new user and buying a product), a connection pool that can be expanded to multiple applications, depending on the business requirements.
In addition, the DBCP connection pool used by default in Tomcat, the jar package is located under Catalina_home/lib,Tomcat-dbcp.jar.
It is important to note that DBCP does not release idle connections by default. For example, when we encode, we get a connection to perform a business operation, but we do not release it. At this point, the DBCP connection pool is not put back into the idle queue. If there is a new connection, another connection is assigned. When the number of connections is too large, it can cause blocking of the connection.
You can automatically reclaim the connection by configuring some properties, first set removeabandoned= "true" to turn on recycling, and then set the time for the connection to removeabandonedtimeout= " It will be withdrawn automatically over that time.
Specific content can be consulted: DBCP documentation
MySQL case
Follow these steps:
1 placing the MySQL driver: can download here
2 Creating a database to insert data
3 Configuring Jndi Resources (Context.xml and Web. xml)
4 Creating JSP Validation results
1 Placing the Drive
Place the MySQL driver in Lib in the Tomcat root directory.
2 Creating a database table and adding data
You can refer to the following SQL script:
/*SQLyog v4.05host-4.1.11-nt:database-test******************************************************************* **server version:4.1.11-nt*/Create Database if not exists' test '; Use' test ';/*table structure for table ' test '. ' Stu '*/Drop Table if exists' test '. ' Stu ';CREATE TABLE' Stu ' (' ID ')int( One) not NULL default '0', ' name 'Char(1)default NULL, ' age 'int( One)default NULL, PRIMARY KEY(' id ')) ENGINE=InnoDBDEFAULTCHARSET=latin1;/*Data for the table ' test '. ' Stu '*/Insert into' Test '. ' Stu 'Values(0,'x', -),(1,'Z', -),(2,'W', -);
3 Configuring Jndi Resources
First add <resource> in Context.xml
<Resourcename= "Jdbc/testdb"Auth= "Container"type= "Javax.sql.DataSource"maxactive= "+"Maxidle= "+"maxwait= "10000"username= "root"Password= "123456"Driverclassname= "Com.mysql.jdbc.Driver"URL= "Jdbc:mysql://localhost:3306/test"/>
Where username is your user name, password is the password.
maxactive Specifies the maximum number of connections, maxidle specifies the maximum number of idle connections (that is, how many connections are saved if there is no connection), and maxwait specifies the maximum number of waiting connections.
Then configure the specified resource name (not required) in Web. xml
<Resource-ref> <Description>DB Connection</Description> <Res-ref-name>Jdbc/testdb</Res-ref-name> <Res-type>Javax.sql.DataSource</Res-type> <Res-auth>Container</Res-auth> </Resource-ref>
4 Creating JSP pages, outputting information
Follow the code below to create and release the connection:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "Import= "Javax.naming.*,java.sql.*,javax.sql.*"pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >Context Initcontext=NewInitialContext (); Context Envcontext= (Context) initcontext.lookup ("Java:/comp/env")); DataSource DS= (DataSource) envcontext.lookup ("Jdbc/testdb"); Connection Conn=ds.getconnection (); String SQL= "SELECT * from Stu"; PreparedStatement St=conn.preparestatement (SQL); ResultSet RS=St.executequery (); while(Rs.next ()) {out.println ("Name:" +rs.getstring (2) + "Age:" +rs.getint (3) + "<br>"); } if(rs!=NULL){ Try{rs.close (); }Catch(Exception e) {e.printstacktrace (); } RS=NULL; } if(st!=NULL){ Try{st.close (); }Catch(Exception e) {e.printstacktrace (); } } if(conn!=NULL){ Try{conn.close (); }Catch(Exception e) {e.printstacktrace (); } } %></body>The result of the final execution:
Other configurationsOther configurations such as Oracle and PostgreSQL are only required for database-driven and created Jndi names that are different:
For example, in Oracle, the CONTEXT.XML is configured as follows:
<Resourcename= "Jdbc/myoracle"Auth= "Container"type= "Javax.sql.DataSource"Driverclassname= "Oracle.jdbc.OracleDriver"URL= "Jdbc:oracle:thin:@127.0.0.1:1521:mysid"username= "Scott"Password= "Tiger"maxactive= " the"Maxidle= "Ten"maxwait= "-1"/>
In PostgreSQL, configure the following:
<Resourcename= "Jdbc/postgres"Auth= "Container"type= "Javax.sql.DataSource"Driverclassname= "Org.postgresql.Driver"URL= "Jdbc:postgresql://127.0.0.1:5432/mydb"username= "MyUser"Password= "MYPASSWD"maxactive= " the"Maxidle= "Ten"maxwait= "-1"/>
The way you use them is pretty much the same.
Reference"1" Several mainstream connection pools: http://developer.51cto.com/art/201006/207768.htm
"2" DBCP Official document: http://commons.apache.org/proper/commons-dbcp/configuration.html
"3" Tomcat JNDI database:http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html
Tomcat 6 Jndi Data Source detailed