connectionpoolservlet.java~18~

来自「Describes basic connections from java to」· JAVA~18~ 代码 · 共 88 行

JAVA~18~
88
字号
package com.jsc.database;

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.jsc.utils.*;

public class ConnectionPoolServlet extends HttpServlet {

    private Vector m_v = null;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        try {
            Configurator configurator = new Configurator(ConfData.CONFIG_FILE);
            Configuration configuration = configurator.getConfiguration();
            Vector v = configuration.getDBConfig();
            m_v = v;
            for (int i=0; i<v.size(); i++) {
                DBConfig dbc = (DBConfig)v.elementAt(i);
                createPool(dbc);
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("This Servlet does not service requests!");
        out.close();
    }

    public void destroy() {
        destoryPool("mysql");
    }

    public String getServletInfo() {
        return "ConnectionPoolServlet Information";
    }

    public void createPool(DBConfig dbc) {
        String dbname = dbc.m_name;
        ConnectionPool pool = new ConnectionPool();
        try {
            String DBDriver = dbc.m_driver;
            String DBURL = dbc.m_url;
            int DBConnectSize = dbc.m_size;
            String DBUsername = dbc.m_username;
            String DBPassword = dbc.m_password;

            System.out.println("########################################");
            System.out.println("# start " + dbname + " connection pool");
            pool.setDriver(DBDriver);
            pool.setURL(DBURL);
            pool.setUsername(DBUsername);
            pool.setSize(DBConnectSize);
            pool.setPassword(DBPassword);

            // Initialize the pool
            pool.initializePool();

            // Once the pool is Initailized, add it to the
            // Global ServletContext.  This makes it available
            // To other servlets using the same ServletContext.
            ServletContext context = getServletContext();
            context.setAttribute(dbname.toUpperCase() + "_POOL", pool);
            System.err.println("# CONTEXT \"" + dbname.toUpperCase() + "_POOL\" ADDED");
            System.out.println("########################################");
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    public void destoryPool(String nodeName) {
        ServletContext context = getServletContext();
        ConnectionPool pool = (ConnectionPool)context.getAttribute(nodeName.toUpperCase() + "_POOL");
        if ( pool != null ) {
            pool.emptyPool();
            context.removeAttribute(nodeName.toUpperCase() + "_POOL");
        } else {
            System.err.println("Could not get a reference to Pool!");
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?