论坛首页 Java版 Tomcat

JNDI使用小指南

浏览 1160 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2008-06-18
1、配置Tomcat5.5.X的Server.xml,在<host>下面加上
<Context path="/JNDIDemo" docBase="D:\workspace\JNDIDemo\WebRoot" debug="0" reloadable="true" crossContext="true"> 
<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_quality_log." suffix=".txt" timestamp="true"/> 
  <Resource 
    name="jdbc/test" <!-- JNDI数据池名称 --> 
    type="javax.sql.DataSource" <!-- 数据类 --> 
    password="karid"     <!-- 密码 --> 
    driverClassName="oracle.jdbc.driver.OracleDriver"  <!-- 驱动 --> 
    maxIdle="2"               <!-- 最少可用lia --> 
    maxWait="5000"        <!-- 最大等待时间 5秒 --> 
    username="karid"       <!-- 用户名 --> 
    url="jdbc:oracle:thin:@127.0.0.1:1521:karid" 
    maxActive="4" <!-- 最大可用连接 --> />       

<ResourceParams name="jdbc/test"> 
   
<parameter> 
  <name>removeAbandoned</name> 
  <!-- Abandoned DB connections are removed and recycled --> 
  <value>true</value> 
</parameter> 
<parameter> 
  <name>removeAbandonedTimeout</name> 
  <!-- Use the removeAbandonedTimeout parameter to set the number of seconds a DB connection has been idle before it is considered abandoned.  --> 
  <value>60</value> 
</parameter> 
<parameter> 
  <name>logAbandoned</name> 
  <!-- Log a stack trace of the code which abandoned --> 
  <value>false</value> 
</parameter> 

<parameter> 
  <name>factory</name> 
  <!--DBCP Basic Datasource Factory --> 
  <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> 
</parameter> 

</ResourceParams> 

2、配置web.xml
<description>MySQL Test App</description> 
<resource-ref> 
     <description>DB Connection</description> 
     <res-ref-name>jdbc/test</res-ref-name> 
     <res-type>javax.sql.DataSource</res-type> 
     <res-auth>Container</res-auth> 
</resource-ref> 

3、JNDI使用
public class DataSourceFactory 
{ 
    private static DataSource ds; 
    public static DataSource createDataSourde() 
    { 
      if (ds == null) 
      { 
         try 
         { 
             Context initContext = new InitialContext(); 
             if (initContext == null) 
                 System.out.println("无配置环境"); 
             Context envContext = (Context) initContext.lookup("java:/compenv"); 
             ds = (DataSource) envContext.lookup("jdbc/test"); //根据名称取得数据源 
          } 
          catch (NamingException e) 
          { 
              e.printStackTrace(); 
          } 
       } 
       return ds; 
    } 
}
   
时间:2008-06-18
这是通过JNDI找到Tomcat上配置的数据源吧???
   
0 请登录后投票
时间:2008-06-18
Context envContext = (Context) initContext.lookup("java:comp/env");

个人觉得通过Tomcat Manager配置更简单吧
   
0 请登录后投票
时间:2008-06-19
写个数据源要这么多的配置代码,太浪费了
   
0 请登录后投票
时间:2008-06-30
JMS如何在JNDI中配置消息队列目的啊?
   
0 请登录后投票
时间:2008-06-30
那些容器有管理界面生成JNDI的!
我用过Tomcate和WebLogic都有。
   
0 请登录后投票
时间:2008-06-30
chen-516888 写道
Context envContext = (Context) initContext.lookup("java:comp/env");

个人觉得通过Tomcat Manager配置更简单吧


Tomcate6的安装文件里面Admin没有了!!需要自己重新下。真麻烦
   
0 请登录后投票
时间:2008-07-13
配置连接池的话用DBCP会不会更方便
   
0 请登录后投票
时间:2008-07-18
现在一般的公司服务器都是配置好了JNDI,然后给你JNDI名称,你无需知道数据库用户名和密码(也有可能不想告诉你)。
   
0 请登录后投票
时间:2008-07-18
通过管理界面当然可以生成,但是哪一天你接触不到管理界面怎么办?写文件的方式还是有必要知道的吧。
   
0 请登录后投票
论坛首页 Java版 Tomcat

跳转论坛:
JavaEye推荐