配置tomcat7自带数据库连接池

1.配置tomcat下conf/context.xml

"jdbc/drp"
        auth="Container"   
        type="javax.sql.DataSource"                    factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"   
        maxActive="100"  //最大激活连接
        maxIdle="30"     //最大空闲连接 
        maxWait="1000"  //最大等待数
        username="root"  //数据库用户名
        password="root" //数据库密码 
        driverClassName="com.mysql.jdbc.Driver" 
        url="jdbc:mysql://192.168.100.39:3306/spider_copy?characterEncoding=UTF-8" /> 
    />

配置过后放到项目下META_INF下

2.将MySQL的jdbc驱动放在tomcat目录下lib文件夹下

3.编写一个Servlet测试(注:需进行Web测试,本地测试无用)

package com.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.jdbc.pool.DataSource; 


public class MyServlet extends HttpServlet{

    private static final long serialVersionUID = 1L;
    private Connection con = null;  
    private static DataSource datasource;  

    private DataSource getInstance() throws NamingException {  
        if (datasource == null) {  
            Context initContext = new InitialContext();  
            Context envContext = (Context) initContext.lookup("java:/comp/env");  
            datasource = (DataSource) envContext.lookup("jdbc/drp");  
        }  
        return datasource;  
    }  

    @Override 
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
        try {
            process(request, response);
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
        try {
            process(request, response);
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void process(HttpServletRequest request, HttpServletResponse response) throws IOException, NamingException, SQLException, InterruptedException, ExecutionException{     


        datasource = getInstance();           
         //连接池同步  
         Future future = datasource.getConnectionAsync();  
         while (!future.isDone()) {  
             // 等待连接池同步  
             try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
         }  

         // 获取连接池  
         con = future.get();  
         System.out.println("connect success");
    }

}

你可能感兴趣的:(数据库)