RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:9:30-18:00
你可能遇到了下面的问题
关闭右侧工具栏
tomcat下数据库连接池的配置
  • 作者:zhaozj
  • 发表时间:2020-12-23 10:59
  • 来源:未知

tomcat数据库连接池配置,在server.xml中添加到<content>中 <!-----------------------------------------------------------------------> <Resource name="jdbc/smsgroup" scope="Shareable" type="javax.sql.DataSource"/>          <ResourceParams name="jdbc/smsgroup">            <parameter>              <name>maxWait</name>              <value>10000</value>            </parameter>            <parameter>              <name>maxActive</name>              <value>100</value>            </parameter>            <parameter>              <name>password</name>              <value>swt</value>            </parameter>            <parameter>              <name>url</name>              <value>jdbc:mysql://localhost:3306/smsgroup?autoReconnect=true</value>            </parameter>            <parameter>              <name>driverClassName</name>              <value>org.gjt.mm.mysql.Driver</value>            </parameter>            <parameter>              <name>maxIdle</name>              <value>30</value>            </parameter>            <parameter>              <name>username</name>              <value>swt</value>            </parameter>          </ResourceParams> <!-------------------------------------------------------> 在web.xml中添加  <resource-ref>      <description>DB Connection</description>      <res-ref-name>jdbc/smsgroup</res-ref-name>      <res-type>javax.sql.DataSource</res-type>      <res-auth>Container</res-auth>  </resource-ref> <!----------------------------------------------------> 连接和关闭数据库代码 public void initConnection() {      //连接池连接数据库        try{ Context ctx = new InitialContext(); DataSource source = (DataSource)ctx.lookup   ("java:comp/env/jdbc/smsgroup"); Connection con = source.getConnection(); con = source.getConnection(); stmt = con.createStatement(); System.out.println("数据库连接成功!");     }catch (Exception e) { e.printStackTrace(); System.out.println("连接数据库失败!"); } } <!----------------------------关闭-------------------------------> public void destroyConnection(){ //关闭数据库(连接池) try {   if (RS!=null) RS.close();   if (stmt!=null) stmt.close();   if (con!=null) con.close();   System.out.println("数据库成功关闭!"); } catch(Exception e) {   System.out.println("数据库繁忙,请按F5刷新页面!");   e.printStackTrace(); } }