JNDI配置

此文之前挂在朋友的一台服务器上,后来后来就没有后来了,现在转过这边来,还是这里比较稳定吧!

OK,进入正题:

使用myeclipse 10,新建一个web项目

在Web-Root下的META-INF文件夹下面建立一个名为content.xml文件,里面加入如下内容:

<span style="font-size:18px;"><Context>
 <Resource
          name="jdbc/test"
          type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"
username="root"
          password="gg@qq"
          maxIdle="2"
          maxWait="50"
          maxActive="10"/>
</Context>
</span>

上面的每一项的含义就不过多解释了,可以去apache-tomcat的官网上查看相关的doc文档,上面有清楚详细的解释,其中name可以随便定一个,但是要跟后面的web.xml文件中的一个配置相同,后面再说;password是数据库的密码;driverClassName这边用的是mysql的driver;username就是mysql数据库的用户名;url后面的test就是我这边用来测试的数据库名称。

然后修改web.xml文件,

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <resource-ref>
    <res-ref-name>jdbc/test</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
  
</web-app>
</span>

此处的<res-ref-name>jdbc/test</res-ref-name>

必须与前面的  name="jdbc/test" 相同

注:

resource-ref子元素的描述如下:

  ● res-ref-name是资源工厂引用名的名称。该名称是一个与java:comp/env上下文相对应的JNDI名称,并且在整个Web应用中必须是惟一的。

  ● res-auth表明:servlet代码通过编程注册到资源管理器,或者是容器将代表servlet注册到资源管理器。该元素的值必须为Application或Container。

  ● res-sharing-scope表明:是否可以共享通过给定资源管理器连接工厂引用获得的连接。该元素的值必须为Shareable(默认值)或Unshareable。

配置好了之后,就是测试啦。。。

然后,然后,很自以为是的用了单元测试来测试上面的JNDI配置是否成功,得到结果不用说:一大串错误信息如下,

<span style="font-size:18px;">javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
	at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
	at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
	at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
	at javax.naming.InitialContext.lookup(Unknown Source)
	at TestJNDI.test(TestJNDI.java:23)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
failure
</span>

然后的然后,你懂得,请教google去了,看了之后才懂,问题应该是这个数据源不支持这样测试,因为要在容器(tomcat)中才可以,所以,所以,只好老老实实的在jsp页面中加入java代码进行测试,具体的代码如下:

<span style="font-size:18px;"><%    
    Context ctx = new InitialContext();      
    String strLookup = "java:comp/env/jdbc/test"; 
    DataSource ds =(DataSource) ctx.lookup(strLookup);
    Connection con = ds.getConnection();
    if (con != null){
        out.print("success too");
    }else{
        out.print("failure");
    }        
%>
</span>

最后将tomcat7跑起来,部署项目后,输入地址后,在页面中显示如下:


OK!这样的话,表示JNDI数据源配置成功。

之后,又进一步去了解了下JNDI,了解到上面的配置,只是局部的配置方式,意思大概是这个数据源只能是这个webproject才可以使用,其他的webproject就不能使用次数据源了。至于配置全局的JNDI,个人觉得不是很实用,因此就把它抛弃了。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值