Tomcat Configuration database Connection pool

Source: Internet
Author: User

The importance of using a database connection pool and how to write a connection pool with Java code is documented in the previous blog post, which is checked online.

found that the original server, such as Tomcat with a connection pool, can be used directly through the configuration file, the following describes how to use.

This example configures the database connection pool for myeclipse+tomcat7.0+sqlserver2008

Specific steps:

1.

Set up the data source in Server.xml, taking the SQL Server 2008 database as an example, as follows:
Join in <GlobalNamingResources> </GlobalNamingResources> node,
<resource
Name= "Jdbc/dbpool"
Type= "Javax.sql.DataSource"
Password= "AAAAAA"
Driverclassname= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"
Maxidle= "2"
maxwait= "5000"
Username= "SA"
Url= "Jdbc:sqlserver://localhost:1433;databasename=coffee"
Maxactive= "4"/>

Property Description: Name, data source name, usually takes "jdbc/xxx" format;
Type, "Javax.sql.DataSource";
Password, database user password;
Driveclassname, database-driven;
Maxidle, maximum idle number, maximum idle time for database connections. Over idle time, the database is connected
The connection will be marked as unavailable and then released. Set to 0 to indicate no limit.
Maxactive, the maximum number of database connections for the connection pool. Set to 0 to indicate no limit.
Maxwait, the maximum connection wait time is established. If this time is exceeded, the exception will be received. Set to-1 to indicate
Unlimited.
2.

Set up the data source reference in your Web application's XML, as follows:
Join in the <web-app></web-app> node,
<resource-ref>
<description>db Connection pool</description>
<res-ref-name>jdbc/DBPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
Child node Description: Description, description information;
Res-ref-name, refer to the data source name, the property name of the previous step;
Res-type, resource type, "Javax.sql.DataSource";
Res-auth, "Container";
Res-sharing-scope, "shareable";
3.

Set the data source link in Context.xml in the Tomcat directory, as follows:
Add in <Context></Context>:
<resourcelink
Name= "Jdbc/dbpool"
Type= "Javax.sql.DataSource"
Global= "Jdbc/dbpool"/>

Property Description: Name, same as 2nd and 3rd steps of the property name value, and child node Res-ref-name value;
Type, also take "Javax.sql.DataSource";
Global, the same name value.

4. Test:

Import Java.sql.connection;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.sqlexception;import Javax.naming.context;import Javax.naming.initialcontext;import Javax.naming.namingexception;import Javax.sql.datasource;public class Test {private static DataSource Pool;public  static void Main (string[] args) {Context env = null;                  try {env = (Context) new InitialContext (). Lookup ("java:comp/env");               Pool = (DataSource) env.lookup ("Jdbc/dbpool");                  if (pool==null) System.err.println ("' Dbpool ' is an unknown DataSource");                  } catch (Namingexception ne) {ne.printstacktrace (); } Connection conn;try {conn = Pool.getconnection (); String sql = "SELECT * from Allbook"; PreparedStatement ps;ps = conn.preparestatement (sql); ResultSet Rs=ps.executequery (); while (Rs.next ()) {System.out.println (rs.getstring ("BookName"));}} catch (SQLException e) {e.printstAcktrace ();}}} 


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.