1. What is a database connection pool The database connection pool is responsible for allocating, managing, and freeing the database connection, which allows the application to reuse an existing database connection instead of re-establishing a database connection that has been idle for more than the maximum idle time to avoid missing database connections due to the absence of a database connection being freed.
2 , why to use the database connection pool
Span style= "font-family: ' Microsoft yahei UI '; font-size:10.5pt; line-height:1.5 "> in the web application, the user needs to get a link to the database for each request. Database creation connections typically require a relatively large amount of resources and a longer creation time. Suppose the site is 10 10 million connections, great waste of database resources, and very easy to cause database server memory overflow, extension machine. So use the database connection pool here to avoid this problem.
3. Connection Pool configuration 1) Connection pool configuration becauseall Tomcat projects share a connection pool configuration: In the Tomcat6->conf->context.xml filein whichto add between <context></context>:
<Context> <resource name= "Jdbc/<span style=" Font-family:simsun; " >drp</span> " auth=" Container " type=" Javax.sql.DataSource " maxactive=" " maxidel=" 10 " maxwait= " username=" root "password=" " driverclassname=" Com.mysql.jdbc.Driver " url= "Jdbc:mysql://127.0.0.1:3306/test" > </Resource>
2)then <web-app></web-app> in Web. Xml adds:
<resource-ref> <RES-REF-NAME>JDBC/DRP </res-ref-name> <res-type> Javax.sql.DataSource </res-type> <res-auth>Container</res-auth>
3) Get connection
public static Connection getconnection () { Connection conn = null; PreparedStatement Pstmt=null; ResultSet set = null; try{ Context ctx=new initialcontext (); Find DataSource DataSource ds= (DataSource) ctx.lookup ("JAVA:COMP/ENV/JDBC/DRP") through Jndi; conn = Ds.getconnection (); } catch (ApplicationException e) { e.printstacktrace (); } return conn; }
4) Ifeach Web project is configured independently with its own connection pool:put the XML content into a specific project directoryin the project directory of Meta-inf, create context.xml, content with the above.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Introduction to connection Pooling