Use the database connection pool that comes with Tomcat 7

Source: Internet
Author: User

1. In the conf \ Catalina \ localhost directory under the tomcat installation directory, create a file with the alias you intend to give the web application: Name: alias. xml

My example is Gentoo. xml. Note that the name must be the same as the value in path.

<Context path="/gentoo" docBase="D:\workplace\TomcatJDBC\WebRoot" debug="0" privileged="true"><Resource name="jdbc/gentoo"      auth="Container"      type="javax.sql.DataSource"      factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"      testWhileIdle="true"      testOnBorrow="true"      testOnReturn="false"      validationQuery="SELECT 1"      validationInterval="30000"      timeBetweenEvictionRunsMillis="30000"      maxActive="100"      minIdle="10"      maxWait="10000"      initialSize="10"      removeAbandonedTimeout="60"      removeAbandoned="true"      logAbandoned="true"      minEvictableIdleTimeMillis="30000"      jmxEnabled="true"      jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"      username="root"      password="yang"      driverClassName="com.mysql.jdbc.Driver"      url="jdbc:mysql://localhost:3306/web"/></Context> 

2. Use:

Context initContext = new InitialContext();Context envContext  = (Context)initContext.lookup("java:/comp/env");DataSource ds = (DataSource)envContext.lookup("jdbc/gentoo");Connection conn = ds.getConnection();


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.