TomCat Context Resource详解


Xml代码 
<?xml version="1.0" encoding="UTF-8"?> 
<Context path="/aaa-app"  
    docBase="D:\\WORK\\AAA\\AAA-BRANCH\\8GBranch\\src\\applications\\aaa-app\\target\\aaa-app" 
    reloadable="false"> 
 
        <Resource auth="Container" 
               url="jdbc:oracle:thin:@localhost:1521:i18n" 
               driverClassName="oracle.jdbc.driver.OracleDriver" 
               name="jdbc/IpbDB" 
               username="aaa8gbranch" password="exigen" 
               factory="org.apache.commons.dbcp.BasicDataSourceFactory" 
               type="javax.sql.DataSource" 
               testOnBorrow="true" testOnReturn="true" timeBetweenEvictionRunsMillis="60000" maxActive="20" maxIdle="5" maxWait="5000" 
        /> 
        <Manager pathname="" checkInterval="60" maxActiveSessions="-1" maxInactiveInterval="60" debug="0" algorithm="MD5" entropy="12345" distributable="false"/> 
</Context> 


Context                                                                                                                                  
1. Context:表示一个web应用程序,通常为WAR文件
2. docBase:应用程序的路径或者是WAR文件存放的路径    
3. path:表示此web应用程序的url的前缀,这样请求的url为http://localhost:8080/aaa-app
4. reloadable:这个属性非常重要,如果为true,则tomcat会自动检测应用程序的/WEB-INF/lib   和/WEB-INF/classes目录的变化,自动装载新的应用程序,我们可以在不重起tomcat的情况下改变应用程序 

Resource                                                                                                                              
1.name,指定JDNI名称(可以自己随意定义)
2.auth,表示认证方式,一般为Container
3.type,数据源类型,使用标准的javax.sql.DataSource
4.maxActive,连接池最大的数据库连接数
5.maxIdle,最大的空闲连接数
6.logAbandoned,被丢弃的数据库连接是否做记录,以便跟踪
7.username,数据库用户名
8.password,数据库用户的密码
9.driverClassName,JDBC Driver
10.url,数据库的URL地址

Tomcat JNDI Resource Common Process                                                                             
第一步,在$CATALINA_HOME$/conf/server.xml配置文件中添置相关Resource信息:

Xml代码 
<Resource> 
      ... ... 
</Resource> 

第二步,在$CATALINA_HOME$/conf/web.xml配置文件中引用上面的Resource信息:

Xml代码 
<resource-ref>   
  <description>DB Connection</description>   
  <res-ref-name>jdbc/mysql</res-ref-name>   
  <res-type>javax.sql.DataSource</res-type>   
  <res-auth>Container</res-auth>   
</resource-ref> 


第三步,在$CATALINA_HOME$/conf/catalina/localhost目录下找到需要进行数据库连接的当前程序的配置信息
譬如:aaa-app.xml,添加如下配置片段:


Java代码 
<Context> 
      ... ... 
      <ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSource"> 
      ... ... 
</Context> 

猜你喜欢

转载自yinqhleave201211055904.iteye.com/blog/1773351