改变世界的是这样一群人,他们寻找梦想中的乐园,当他们找不到时,他们亲手创造了它

JNDI学习总结(二):tomcat配置全局和私有JNDI数据源的几种方式

下面介绍几种常用的JNDI数据源配置方式

环境:IDEA+tomcat7

 

全局:

1. 修改tomcat的context.xml的<context>标签

  在<context>标签内加入如下内容(以配置mysql数据源为例):

 

<Resource name="jdbc/mysql"     //名称可以自定义
    auth="Container" 
    type="javax.sql.DataSource" 
    username=""   //改成自己的数据库用户名和密码
    password="" 
    maxIdle="30" 
    maxWait="10000" 
    maxActive="100"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/数据库名" />

 

配置好后重启tomcat。 可以选择在项目中引入mysql驱动,或者直接将驱动jar包扔进tomcat安装目录中的lib文件夹下。

最后在项目的web.xml中配置这段代码(其实可有可无,但最好加上,后文会解释原因):

<resource-ref>
    <res-ref-name>jdbc/mysql</res-ref-name> //和上面的name属性值相同
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
  </resource-ref>

使用下面这段代码测试(注意,不能在main中测试,否则会报错。在jsp页面中或者servlet中可以,需要web环境):

Context initial = new InitialContext(); //得到上下文引用
DataSource datasource = (DataSource) initial.lookup("java:comp/env/jdbc/mysql");

2. 修改tomcat的server.xml的<GlobalNamingResources>

    在<GlobalNamingResources>标签中加入下面代码:

 

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

 

打开$tomcat_home\conf\context.xml, 在<context>标签中加入下面代码:

 

<ResourceLink name="jdbc/mysql" global="globalResource" type="javax.sql.DataSource"/>

 

注意:<Resource>中的name属性要和<ResourceLink>中的global属性对应才能引用到,<ResourceLink>中的name才是数据源的名字,要通过这个名字找到数据源。     同样在web.xml中加入下面代码:

<resource-ref>
    <res-ref-name>jdbc/mysql</res-ref-name> //和上面的name属性值相同
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

 

 

私有:

1. 修改tomcat的server.xml的<Host>

找到<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true"></Host>,在其中加入以下内容:

 

 

<Context path="/xxxx" docBase="项目的绝对路径" reloadable="true">
    <Resource name="jdbc/mysql" 
        auth="Container" 
    type="javax.sql.DataSource" 
    username="" 
    password="" 
    maxIdle="30" 
    maxWait="10000" 
    maxActive="100"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://127.0.0.1:3306/数据库名"/>
</Context>

注意:<context>的path属性必须和项目访问路径一致且开头必须加“/”。 什么叫项目访问路径?如果你用eclipse+tomcat启动项目,那path属性为“/你的项目名”。 若你使用IDEA+tomcat启动项目,这里的path必须等于下图圈起来的地方:

若在IDE中启动tomcat,其实docBase这个属性设不设置都可以,设置了乱填也不影响。但为什么我还是写上了,因为当你使用cmd启动tomcat时,若不设置该属性启动时会抛出“NamingException: No naming context bound to this class loader”。所以必须将docBase设置为项目的绝对路径。


然后在web.xml中加入:

<resource-ref>
    <res-ref-name>jdbc/mysql</res-ref-name> //和上面的name属性值相同
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

2. 在项目的META-INF文件夹下创建context.xml

内容如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true">
    <Resource name="jdbc/mysql" 
        auth="Container" 
    type="javax.sql.DataSource" 
    username="" 
    password="" 
    maxIdle="30" 
    maxWait="10000" 
    maxActive="100"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://127.0.0.1:3306/项目名"/>
</Context>

 

 

 

同时存在多种配置方式且同名,会获取哪一个:

首先,全局配置大于私有配置,也就是说若全局和私有同时存在,优先获取全局配置的数据源

全局:

    在context.xml中,<Resource>和<ResourceLink>(引用server.xml中<GlobalNamingResources> )同时存在,哪个标签在前获取到哪一个,即按顺序获取。

私有:

    1. 若只存在<Host>标签中的配置,不存在META-INF中的context.xml,则优先获取<Host>中配置的第一个数据源。

    2. 若两种私有配置方式都存在,会获取META-INF中context.xml配置的数据源。

为什么web.xml中的配置可有可无,却要加上呢?

详情见tomcat的docs:jndi-resources-howto.html

其中有这么一句话:

 

If a resource has been defined in a <Context> element it is not necessary for that resource to be defined in /WEB-INF/web.xml. However, it is recommended to keep the entry in /WEB-INF/web.xml to document the resource requirements for the web application.

 

也就是说,在web.xml加入的那段代码仅起到说明作用,说明这个Web应用引用了哪些资源!详情请翻阅文档

 

 

 

 

 

原文链接:https://blog.csdn.net/wn084/article/details/80736253

 

posted @ 2023-01-31 14:23  水狼一族  阅读(1398)  评论(0编辑  收藏  举报
改变世界的是这样一群人,他们寻找梦想中的乐园,当他们找不到时,他们亲手创造了它