zoukankan      html  css  js  c++  java
  • Tomcat7配置数据源

    在JDBC的操作中,打开和关闭数据库连接,是最耗费数据库资源的,我们可以在服务器上存放一定量的数据库连接,每当程序要连接数据库时,就将这些链接给它们,而一旦程序退出数据库操作时,要将这些链接返回给服务器。

    下图是数据源的基本概念:

    1:通过Connection Pool 管理数据库连接

    2:通过Data Source 去管理Connection Pool

    3: Data Source 被JNDI Tree 绑定

     

    一:JNDI配置方式

    现在来讲一下如何到服务器上对 Data Source 进行配置?

    服务器: Tomcat 7

    数据库:MySQL

    1:将下面的代码添加到Tomcat服务器上conf/context.xml中的<Context></Context>标签中

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

    上下文context.xml中的参数的解析如下:

      其中的name属性是数据源名称,通常采取jdbc/**.

       driverClassName属性是驱动程序名称。

       username,password,数据库名称和密码

       url:访问的数据库路径。其中url的内容组成解析上篇博客中已经分析

       maxActive属性是并发连接的最大数。设置为0则无限制。

       maxWait属性是等待连接的最大连接的时间。

       maxIdle属性是连接池中空闲的连接的个数。

     

    2. 修改web.xml
     打开%TOMCAT_HOME%\conf\web.xml,在</web-app>的前面添加以下内容:

    <description>MySQL Test App</description>
      <resource-ref>
          <description>DB Connection</description>
          <res-ref-name>jdbc/shopping</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
      </resource-ref>

    3:建立测试文件

    test.jsp

    <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    
    <sql:query var="rs" dataSource="jdbc/shopping">
    select id, name, normalprice from product
    </sql:query>
    
    <html>
      <head>
        <title>DB Test</title>
      </head>
      <body>
    
      <h2>Results</h2>
    
    <c:forEach var="row" items="${rs.rows}">
        name ${row.name}<br/>
        normalprice ${row.normalprice}<br/>
    </c:forEach>
    
      </body>
    </html>

    4.添加jar包 

    JDBC驱动程序mysql-connector-java-5-bin.jar放置在%TOMCAT_HOME%\lib和应用的WEB-INF\lib下,复制 jstl.jar standard.jar 到 你的 WEB-INF/lib 目录.


    5:配置名称name="jdbc/mldn"可以任意,配置完成后,需要通过名称查找的方式,去找到数据源,本示例代码运用的Tomcat服务器,所以在查找时需要对名称进行定位:java:comp/env

    <%@ page import="java.sql.*"%>
    <%@ page import="javax.sql.*"%>
    <%@ page import="javax.naming.*"%>
    <%!
        final String JNDINAME = "java:comp/env/jdbc/shopping" ;
    %>
    <%
        Connection conn = null ;
        try
        {
            // 初始化查找命名空间
            Context ctx = new InitialContext() ;
            // 找到DataSource
            DataSource ds = (DataSource)ctx.lookup(JNDINAME) ;
            conn = ds.getConnection() ;
        }
        catch(Exception e)
        {
            System.out.println(e) ;
        }
    %>
    <%=conn%>
    <%
        // 将连接重新放回到池中
        conn.close() ;
    %>

    配置spring

    配置spring
     <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
      <property name="jndiName"><value>java:comp/env/jdbc/xxx</value></property>
     </bean>

     

  • 相关阅读:
    NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.
    QOpenGLShaderProgram: could not create shader program
    ubuntu loading initial ramdisk 卡住
    ubuntu 下pip install sasl报错fatal error: sasl/sasl.h: No such file or directory
    ImportError: No module named managers
    python docker 多进程提供 稳定tensorflow gpu 线上服务
    侧脸生成正脸概论与精析(一)Global and Local Perception GAN
    pytorch安装 caffe2 安装:git 慢 caffe2 cannot find -lopencv_dep_cudart ,undefined reference to 'pthread_create'
    undefined symbol: PyFPE_jbuf
    Metasploit后渗透模块开发
  • 原文地址:https://www.cnblogs.com/ITtangtang/p/2511749.html
Copyright © 2011-2022 走看看