zoukankan      html  css  js  c++  java
  • Tomcat中配置MySQL数据库连接池

    Web开发中与数据库的连接是必不可少的,而数据库连接池技术很好的优化了动态页与数据库的连接,相比单个连接数据库连接池节省了很大的资源。用一个通俗的比喻:如果一个人洗澡需花一桶水,那一百个人就要花一百桶水,太浪费了.如果都在池子里洗,洗多少个人都不怕了。

    1.将MySQL的JDBC驱动复制到Tomcat安装目录里的lib文件夹下。驱动可以从MySQL官网上下载,为jar包。

    2.将Tomcat的配置文件Context.xml做如下修改:

    <Context path="/DBTest" docBase="DBTest"
            debug="5" reloadable="true" crossContext="true">

        <!-- maxActive: Maximum number of dB connections in pool. Make sure you
             configure your mysqld max_connections large enough to handle
             all of your db connections. Set to -1 for no limit.
             -->

        <!-- maxIdle: Maximum number of idle dB connections to retain in pool.
             Set to -1 for no limit.  See also the DBCP documentation on this
             and the minEvictableIdleTimeMillis configuration parameter.
             -->

        <!-- maxWait: Maximum time to wait for a dB connection to become available
             in ms, in this example 10 seconds. An Exception is thrown if
             this timeout is exceeded.  Set to -1 to wait indefinitely.
             -->

        <!-- username and password: MySQL dB username and password for dB connections  -->

        <!-- driverClassName: Class name for the old mm.mysql JDBC driver is
             org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
             Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
             -->
       
        <!-- url: The JDBC connection url for connecting to your MySQL dB.
             The autoReconnect=true argument to the url makes sure that the
             mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
             connection.  mysqld by default closes idle connections after 8 hours.
             -->

      <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
                   maxActive="100" maxIdle="30" maxWait="10000"
                   username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
                   url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>

    </Context>

    注意代码中红色部分:DBTest 改为自己的项目路径;TestDB改为自己的数据源名,但是后面使用时候要与这里的配置保持一致;javauser和 javauser改为自己MySQL的用户名密码;url的格式依次为jdbc:mysql://{你的数据库服务所在的IP,如果为本机就为localhost}:{你的数据库服务端口号}/{MySQL中要使用的数据库名称}?autoReconnect=true  。

    3.修改项目WEB-INF/web.xml 配置文件(若无,请新建),在“</web-app>”之上添加如下代码:

      <resource-ref>
          <description>DB Connection</description>
          <res-ref-name>jdbc/TestDB</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
      </resource-ref>

    上步中若修改了数据源名此步中红色部分请保持与上步中的一致。

    4.代码示例:

    Context initContext = new InitialContext();
    Context envContext  = (Context)initContext.lookup("java:/comp/env");
    DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB");
    Connection conn = ds.getConnection();
    Statement st = null;
    ResultSet rs = null;
    st = conn.createStatement();
    rs = st.executeQuery(yoursql);

    注意红色部分与上两步中的一致;yoursql处写你的sql代码。

    通过1-3步就在Tomcat中配置好了MySQL的数据库连接池。

  • 相关阅读:
    PHPsession实现用户登陆功能
    asp.net core mvc基于Redis实现分布式锁,C# WebApi接口防止高并发重复请求,分布式锁的接口幂等性实现
    OpenSSL 下载和私钥证书、CERTIFICATE证书生成
    Java byte[] 转C# byte[]
    如何在Etherscan.io 部署ETH以太坊智能合约 如何在15分钟内创建你的加密货币
    JavaScript 关于金额、数字验证的正则表达式
    web api .net C# mvc API返回XML文档的解析并取值
    C#和PHP加密结果一致的DES加密解密算法。php实现和c#一致的DES加密解密
    C#常用的图片处理方法-图片剪切、图片压缩、多图合并代码
    BitMap 图片格式与Base64Image格式互转方法
  • 原文地址:https://www.cnblogs.com/zhengah/p/4971233.html
Copyright © 2011-2022 走看看