jndi 数据连接池

 数据连接池配置 全局 、局部配置之分

 下面就我玩的全局的配置记录下来(本人用的是oracle):

 1. 先将准备工作做好 将oracle的 class12.jar放到 tomcat下的comm/lib中;

 2. 接着在tomcat的 conf/service.xml文件的  <GlobalNamingResources>域内

    添加配置代码:

 

 <Resource  name="jdbc/oracle"       
	    auth="Container"       
	    type="javax.sql.DataSource"       
	    driverClassName="oracle.jdbc.driver.OracleDriver"       
	    url="jdbc:oracle:thin:@localhost:1521:oracle"       
	    username="oracle"       
	    password="oracle"       
	    maxActive="50"       
	    maxIdle="20"       
	   maxWait="10000" />    

  3.在tomcat的 conf/context.xml文件中加入代码:

 

<ResourceLink name="jdbc/oracle" global="jdbc/oracle" type="javax.sql.DataSourcer"/>

  若是没在  tomcat的 conf/context.xml文件中加入上边的代码 则会报 Cannot create JDBC driver of class ''   for connect URL 'null' 这是因为你的服务器的全局JNDI资源,而用InitialContext去找server的resource当然找不到了,要想找到server的resource就得在web application中的context环境里加入一个指向该全局resource的ResourceLink。 

 4.接着在你的项目的 web.xml文件中添加:

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

 

 5.配置文件都完成接下来工作就是测试,看配置的jndi 是否正确,下面是测试用的到的java代码:

   
       Context ctx = new InitialContext();
       ctx = new InitialContext();
        //DataSource ds =(DataSource)ctx.lookup("java:comp/env/jdbc/oracle");     //查找JNDI数据源名
     Context envContext = (Context)ctx.lookup("java:/comp/env");
        DataSource ds = (DataSource)envContext.lookup("jdbc/oracle");
        conn = ds.getConnection();
	     	   	 

 

 

  注解:

global -->The name of the linked global resource in the global JNDI context.
name -->The name of the resource link to be created, relative to the java:comp/env context.?
type -->The fully qualified Java class name expected by the web application when it performs a lookup for this resource link.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值