Tomcat7 connection pool Preparation Method

Source: Internet
Author: User

There are two methods:

1. Simple Preparation Method
Add the following context between the Code

<Context Path = "/test" docbase = "test" DEBUG = "0" reloadable = "true" crosscontext = "true">
<Resource Name ="JDBC/test"Auth =" Container "type =" javax. SQL. datasource "maxactive =" 10 "maxidle =" 5 "maxwait =" 10000 "username =" sa "Password =" Sasa "driverclassname =" net. sourceForge. jtds. JDBC. driver "url =" JDBC: jtds: sqlserver: // localhost: 1433/mydb "/>
</Context>

2. officially recommended methods

First, configure the context. xml file under conf under tomcat, and add the connection pool as follows:

<Resource Name ="JDBC/test"Auth =" Container "type =" javax. SQL. datasource "maxactive =" 10 "maxidle =" 5 "maxwait =" 10000 "username =" sa "Password =" Sasa "driverclassname =" net. sourceForge. jtds. JDBC. driver "url =" JDBC: jtds: sqlserver: // localhost: 1433/test "/>

Add the following content to the Web. xml file of your application:

<Resource-ref>
<Description> dB connection </description>
<Res-ref-Name>JDBC/test</RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
</Resource-ref>

-----------------------------------------------------------

Test JSP page code:

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
<% @ Page import = "Java. SQL. *" %>
<% @ Page import = "javax. SQL. *" %>
<% @ Page import = "javax. Naming. *" %>
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/";
%>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Base href = "<% = basepath %>">
<Title> tomcat7 connection pool configuration test </title>
<Meta http-equiv = "Pragma" content = "no-Cache">
<Meta http-equiv = "cache-control" content = "no-Cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "Description" content = "this is my page">
</Head>
<Body>
<%
Try {
Datasource DS = NULL;
Initialcontext CTX = new initialcontext ();
DS = (datasource) CTX. Lookup ("Java: COMP/ENV/ JDBC/test ");
Connection conn = Ds. getconnection ();
Statement stmt = conn. createstatement ();
String strsql = "select * from T1 ";
Resultset rs = stmt.exe cutequery (strsql );
While (Rs. Next ()){
Out. println (Rs. getstring (1) + "..." + Rs. getstring (2) + "</BR> ");
}
Stmt. Close ();
Rs. Close ();
Conn. Close ();

} Catch (sqlexception SE ){
Out. Print ("exception:" + Se. tostring ());
}
%>
</Body>
</Html>

 
 

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.