JDBC data sources and connection pooling

Source: Internet
Author: User
Tags auth connection pooling

JDBC data sources and connection pooling
Database connection pooling is the creation of enough database connection pools when the application starts, requiring access in Java programs
Database, you can get an idle connection from the data source and run out of time in the return to the connection pool
Instance:
+mysql5.5 the configuration of data sources and connection pools in Tomcat6.0
(1) Copy the JDBC driver of the database to the <catalina_home>/common/lib directory
(2) Configure the data source and connection pool in <catalina_home>/conf/server.xml, for example
<context path= "/jsp_jdbc" docbase= "Jsp_jdbc" debug= "0" reloadable= "true" >
<resourceparams name= "Jdbc/mydatasource" auth= "Container" type= "Javax.sql.DataSource"/>
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>1000</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>root</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost/test?autoReconnect=true</value>
</parameter>
<ResourceParams>
<Context>


To configure a reference to a data source connection pool in the Web. xml file
<resource-ref>
<description>db connection</description>
<res-ref-name>jdbc/myDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Accessing a database through a data source in a JSP or servlet
<body>
Accessing a database from a data source
<%
Connection Conn=null;
Statement Stat=null;
ResultSet Rs=null;
Getting a connection from a data source connection pool
Context ctx=new InitialContext ();
DataSource ds= (DataSource) ctx.lookup ("Java:comp/env/jdbc/mydatasource");
Conn=ds.getconnection ();
Querying data tables
Stat=conn.createstatement ();
String sql= "SELECT * from user";
Rs.stat.executeQuery (SQL);
Output query results to interface
while (Rs.next ()) {
Out.println ("<li> Account:" +rs.getstring (2). Trim ());
Out.println ("Password:" +rs.getstring (3). Trim () + "</li>");
}
Close the connection and release the resource
Rs.close ();
Stat.close ();
Conn.close ();
%>
</body>

JDBC data sources and connection pooling

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.