tomcat下连接池的解决方案

来源:互联网 发布:光环大数据 编辑:程序博客网 时间:2024/04/30 02:59
tomcat下连接池的解决方案先在server.xml及web.xml内配置数据源
//server.xml
<Context path="/harvester" reloadable="true" docBase="/home/sxhong/workspace/harvester" workDir="/home/sxhong/workspace/harvester/work">
            <!--配置JDBC 数据源 -->
            <Resource name="jdbc/harvester" auth="Container" type="javax.sql.DataSource"
               maxActive="50" maxIdle="30" maxWait="10000"
               username="sxhong" 
               password="test" 
               driverClassName="org.postgresql.Driver"
               url="jdbc:postgresql://localhost:5432/harvester"/>
</Context>

//web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
    <!--JDBC 数据源-->  
     <resource-ref>
      <description>
        用于访问postgresql harvester数据库的连接池数据源
      </description>
      <res-ref-name>
        jdbc/harvester
      </res-ref-name>
      <res-type>
        javax.sql.DataSource
      </res-type>
      <res-auth>
        Container
      </res-auth>
    </resource-ref>
</web-app>
在servlet内的ini()t及destory()方法内将得到的DataSource 设置为整个服务共享.
//全局变量
    Context ctx;
    DataSource ds;
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        ctx=(Context)config.getServletContext().getAttribute("context");
        DataSource=(DataSource)config.getServletContext().getAttribute("DataSource");
        if(ds!=null) return;
        try {
            ctx = new InitialContext();
            DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/harvester");
            config.getServletContext().setAttribute("context", ctx);//存储到整个服务
            config.getServletContext().setAttribute("DataSource", ds);//存储到整个服务
        } catch (NamingException e) {
            e.printStackTrace();
        }
       
    }

    public void destroy() {
        super.destroy();
        try {
            ctx.close();
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

在doXXX()方法内使用DataSource:
    try{
                String sql="select set_id,setname from sets where repository_id="+id;
                con=DBconnect.getConnection();
                st=con.createStatement();
                rs=st.executeQuery(sql);
    //因为使用的是连接池,不会添加物理连接数据库的开销,这样有利于合理的利用资源               
                rs.close();
                st.close();
                con.close();
    }catch (SQLException e) {
            e.printStackTrace();
    }



注意:
在使用的过程中注意资源的释放,因为对于支持连接池的数据源来说,连接的关闭只是逻辑上的关闭,不是物理上的关闭,资源的释放会使得其它的数据库连接可以得以重用且不消耗为建立连接而形成的物理连接消耗,所以在使用的过程中一定要注意关闭(Statement,PreStatement,ResultSet).

如在不能立即能够释放资源(如跨会话的检索等)为保证同一次会话不占用太多的连接可以将连接保存到会话或者请求中:
        //得到数据库的查询连接
        con=(Connection)session.getAttribute("connection");
        if(con==null){
            try {
                con=DBconnect.getConnection();
                session.setAttribute("connection", con);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
原创粉丝点击