zoukankan      html  css  js  c++  java
  • tomcat配置JNDI获取数据源

      各个web工程可以通过工程内的xml文件配置访问数据库的数据源,这样的配置是各个工程私有的。基于JNDItomcat配置数据源,则可以做成全局的,各工程只需要通过便签引用数据源即可。

      1.需要将数据库的连接驱动mysql-connector-java-5.1.21.jar及数据库连接池的jardruid-0.2.9.jar放到Tomcat 6.0lib下。

    2.修改tomcat的配置文件,基于JNDI配置数据源,Tomcat 6.0confcontext.xml改成如下即可,原文件中多余的部分删掉:

    <?xml version='1.0' encoding='utf-8'?>
    <Context>
        <WatchedResource>WEB-INF/web.xml</WatchedResource>
        <Resource 
             name="jdbc/mysqldb"
             type="javax.sql.DataSource"
             description="DruidDataSource"
             factory="com.alibaba.druid.pool.DruidDataSourceFactory"
             scope="Shareable"   
             auth="Container"        
             driverClassName="com.mysql.jdbc.Driver"
             url="jdbc:mysql://localhost:13306/nnm5"
             username="root"
             password="OSSDB123"
             maxActive="50"
             maxWait="10000"
             removeabandoned="true"
             removeabandonedtimeout="60"
             logabandoned="false"
             filters="stat"/>
        <ResourceLink global="mysqldb" name="mysqldb" type="javax.sql.DataSource"/>   
    </Context>

    这里配置的是Druid数据库连接池,配置正确的用户名,密码,url即可。

    3.在工程的web.xml<web-app>标签之间加上下面的代码:

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

    4.在需要数据库连接池的配置文件中,通过jee:jndi-lookup标签引入连接池即可。需要引入jee命名空间。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/jee
    http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    ">
    
        <jee:jndi-lookup id = "dataSource"
            jndi-name ="java:comp/env/jdbc/mysqldb"
            resource-ref = "true"/>

    这里需要注意jndi-name不要写错。其他的地方引用iddataSourcebean即可。

     

     

     

  • 相关阅读:
    React.js学习笔记之事件系统
    彻底解决Webpack打包慢的问题:npm run build:dll
    gulp详细入门教程
    cmd、node、npm 常用命令
    ant design中ES6写法个人总结
    自定义浏览器滚动条的样式,打造属于你的滚动条风格
    js相关知识
    day31-python阶段性复习五
    day30-python阶段性复习四
    day29-python阶段性复习三
  • 原文地址:https://www.cnblogs.com/lnlvinso/p/4194741.html
Copyright © 2011-2022 走看看