• 如何正确的在java web配置数据池


    在tomcat context.xml中配置数据

    <Context reloadable="true">

        <!-- Default set of monitored resources -->    

    <WatchedResource>WEB-INF/web.xml</WatchedResource>    

    <Resource name="jdbc/bookstore" auth="Container"      type="javax.sql.DataSource"      driverClassName="com.mysql.jdbc.Driver"      maxActive="100" maxIdle="30" maxWait="10000"      username="root" password="root"      url="jdbc:mysql://localhost:3306/bookstore" />

        <!-- Uncomment this to disable session persistence across Tomcat restarts -->    

    <!--    

    <Manager pathname="" />    

    -->

        <!-- Uncomment this to enable Comet connection tacking (provides events         

    on session expiration as well as webapp lifecycle) -->    

    <!--    

    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />    

    -->

    </Context>

    在server.xml中修改如下

    <GlobalNamingResources>    

    <!-- Editable user database that can also be used by          UserDatabaseRealm to authenticate users     -->    

    <Resource name="UserDatabase" auth="Container"               type="org.apache.catalina.UserDatabase"               description="User database that can be updated and saved"               factory="org.apache.catalina.users.MemoryUserDatabaseFactory"               pathname="conf/tomcat-users.xml" />

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

      </GlobalNamingResources>

    在项目的xml中配置

       <resource-ref>

      <description>MySQL Datasource v3hd</description>

      <res-ref-name>jdbc/bookstore</res-ref-name>

      <res-type>javax.sql.DataSource</res-type>

      <res-auth>Container</res-auth>

     </resource-ref>

    访问代码

    private DataSource ds=null;
     public BookDBBean() throws NamingException
     {
      Context ctx=new InitialContext();
      ds=(DataSource) ctx.lookup("java:comp/env/jdbc/bookstore");
     }
     public Connection getConnection() throws SQLException
     {
      return ds.getConnection();
     }

  • 相关阅读:
    atitit.TokenService v3 qb1 token服务模块的设计 新特性.docx
    Atitit attilax在自然语言处理领域的成果
    Atitit 图像清晰度 模糊度 检测 识别 评价算法 原理
    Atitit (Sketch Filter)素描滤镜的实现  图像处理  attilax总结
    atitit。企业的价值观 员工第一 vs 客户第一.docx
    Atitit 实现java的linq 以及与stream api的比较
    Atitit dsl exer v3 qb3 新特性
    Atititi tesseract使用总结
    Atitit 修改密码的功能流程设计 attilax总结
    atitit.TokenService v3 qb1  token服务模块的设计 新特性.docx
  • 原文地址:https://www.cnblogs.com/tuifeideyouran/p/3535116.html
Copyright © 2020-2023  润新知