Tomcat 7-DHCP configuration database connection pool

Source: Internet
Author: User

principle


As for the connection pool, everyone knows to restrict the connection to the database. The basic principle is to pre-put a certain idle connection in the buffer pool, when the program needs to interact with the database, not directly create a new database connection but directly in the connection pool, and then put back to the connection pool. Why do you sacrifice a buffer to store the connections that would otherwise be used? One of the benefits of this is that you can limit the number of connections so that no More than N of the database connection is last down, and you have a connection pool that can be used to listen to these connections and facilitate management.


Configuration
1.copy-relatedJar


be aware that the connection pool is not used to directly manipulate the database, ultimately related operations or related JDBC Drive. If it is a tomcat Server, copy the drive directly to tomncat lib . For Java is ojdbc6. jar; for SQL Server The Tomcat-dbcp.jar, Servlet-ap.jar, and SQL Server drivers Sqljdbc4.jar packages to the Lib directory under the Web-inf folder of the project file.


2.Configurationcontext.xml


<resource                     name= "JDBC/DRP"                    auth= "Container"                    type= "Javax.sql.DataSource"                    factory= " Org.apache.tomcat.jdbc.pool.DataSourceFactory "                    maxactive=" maxidle= "       maxwait=" 10000       " Username= "DRP1"       password= "Drp1" driverclassname= "Oracle.jdbc.driver.OracleDriver" url= "      jdbc:o Racle:thin: @localhost: 1521:drp "/>



if it is a global configuration, the context.xml files are placed in Tomcat the configuration folder conf If you want to have local control , you need to context.xml placed in the specified project Webroot of the Meta-inf , it will only work on the project itself.


Note:


Name: Specify the name of the connection pool

Auth: is the connection pool Management property, container represents the container management

type : Data Source Type

Factory : This is in Tomcat 5 Then for commons-dhcp A processing scheme, which is handled by such a specific implementation class. Performance is more superior and can be compatible with DHCP.

maxactive : Maximum number of allocated connections.

Maxidle : Idle is idle, so this is when Tomcat at startup, the buffer pool is the number of new connections that were created.

maxwait : The maximum response time in this article is 10s .

URL : is the database address.

SQL Server The format: jdbc:sqlserver://localhost:1433;databasename= name ;

Driverclassname : Drive address.

SQL Server for the following: Com.microsoft.sqlserver.jdbc.SQLServerDriver

3.Validation


Connectionconn=null; Preparedstatementpstmt=null; resultsetrs=null;try{//instance DHCP connection pool contextctx=new InitialContext ()//Access specified connection pool datasourceds= (DataSource) through Jndi Ctx.lookup ("JAVA:COMP/ENV/JDBC/DRP");//Instantiate Database connection conn=ds.getconnection ();//Query Statement pstmt=conn.preparestatement (" select* from T_user "); Rs=pstmt.executequery (); if (Rs.next ()) {System.out.print (rs.getstring (" user_id ") + Rs.getstring ("user_name")); System.out.print (conn);}} catch (Sqlexceptione) {}finally{}


Post

The previous version of Tomcat 5, which can be configured in localhost:8080/admin or in XML, discards the process of a view configuration. Always for the processing of data connection, there is his advantage, but also need developers in normal programming to develop good habits, if the connection is not closed to open, when the connection pool is large enough to affect performance, if the peak, then the program directly down. It also reduces the time we have to create a database connection. Of course, in addition to DHCP, there are c3p0,poolmen such a treatment scheme.


Tomcat 7-DHCP configuration database connection pool

Related Article

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.