随笔 - 63  文章 - 0  trackbacks - 0
<2009年3月>
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(2)

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

配置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 
         

    1. <description>MySQL Test App</description>    
    2. <resource-ref>    
    3.      <description>DB Connection</description>    
    4.      <res-ref-name>jdbc/test</res-ref-name>    
    5.      <res-type>javax.sql.DataSource</res-type>    
    6.      <res-auth>Container</res-auth>    
    7. </resource-ref>   

    3、JNDI使用 

         

    1. public class DataSourceFactory    
    2. {    
    3.     private static DataSource ds;    
    4.     public static DataSource createDataSourde()    
    5.     {    
    6.       if (ds == null)    
    7.       {    
    8.          try    
    9.          {    
    10.              Context initContext = new InitialContext();    
    11.              if (initContext == null)    
    12.                  System.out.println("无配置环境");    
    13.              Context envContext = (Context) initContext.lookup("java:/compenv");    
    14.              ds = (DataSource) envContext.lookup("jdbc/test"); //根据名称取得数据源    
    15.           }    
    16.           catch (NamingException e)    
    17.           {    
    18.               e.printStackTrace();    
    19.           }    
    20.        }    
    21.        return ds;    
    22.     }    
    23. }  
    posted on 2009-03-24 11:43 lanxin1020 阅读(133) 评论(0)  编辑  收藏 所属分类: DB

    只有注册用户登录后才能发表评论。


    网站导航: