`

tomcat6.0下配置JNDI 以及在spring配置文件中使用

    博客分类:
  • java
 
阅读更多

 

使用jndi代替数据连接池
第一步:在tomcat6.0的conf文件夹下找到 context.xml 文件,在<context></context>中加入:
 

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


第二步:在web.xml中加入:


<!-- 使用tomcat6.0的jndi -->
   <resource-ref>
          <!-- 调用jndi的名称 -->
       <res-ref-name>myjndi</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <!-- 此处要和tomcat下的context.xml中的auth 一致 -->
       <res-auth>Container</res-auth> 
   </resource-ref>
 

第三步:在spring配置文件中配置dataSource


<!-- 使用tomcat的jndi-->
  <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
       <property name="jndiName" value="java:comp/env/myjndi"></property> 
  </bean>

 

大家在使用是注意相同颜色部分不要弄错

 

因为配置文件中不允许使用中文所以具体说明大家看一下下边:

<Resource name="myjndi" <!-- JNDI名称 -->
auth="Container" <!-- 此处和web.xml中对应 -->
type="javax.sql.DataSource" <!-- 数据源类型 -->
password="1234" <!-- 数据库访问密码 -->
username="root" <!-- 数据库访问用户名 -->
driverClassName="com.mysql.jdbc.Driver" <!-- 数据库驱动类 -->
url="jdbc:mysql://localhost:3306/ash2" <!-- 数据库访问url -->
maxActive="100" <!-- 最大活动数 -->
maxIdle="30"
maxWait="5000" <!-- 最大等待时间 -->
/>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics