JNDI数据源配置

在Java Web开发中都要与数据库打交道,为了不频繁地打开和关闭数据库,以减少数据库操作负荷,可使数据库在开发过程中保持打开状态,在这里我们采用配置数据源的方式(JNDI),而不是传统地JDBC方式。下面就针对常规型的MySQL5.0.15和Tomcat6.0的数据源的基本配置进行简单的介绍:    首先声明,如果数据源没有配置好的话,在开发过程中会抛出诸如下列异常等:    1、org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'  
  2、Caused by: java.sql.SQLException: No suitable driver    3、Name jdbc is not bound in this context  
现在开始讲下如何配置好数据源同时也是解决上述异常的办法:    方案一:  
  步骤一、在Tomcat6.0解压目录conf下找到context.xml,在其中的<Context></Context> 中加入如下代码(要根据自己的情况稍加修改): <Resource  name="jdbc/myznt"   auth="Container"  
 type="javax.sql.DataSource"  username="root"   password="localhost"  
driverClassName="org.gjt.mm.mysql.Driver"  url="jdbc:mysql://localhost/myznt"  maxActive="100"   maxIdle="30"   maxWait="5000"  
removeAbandoned="true"  
removeAbandonedTimeout="60"  logAbandoned="true" />   
<Resource…/>里德属性介绍: 
  name:指定Resource的JNDI名字   auth: 指定管理Resource的Manager,它有两个可选值:Container,     Application。Container表示有容器创建Resource,     Application表示由Web应用来创建和管理Resource。   type:指定Resource所属的java类名。  
  步骤二、在工程应用中/WEB-INF/下的web.xml中加入如下代码(要根据自己的情况稍加修改):  <resource-ref>  
    <description>MySQL DataSource</description>       <res-ref-name>jdbc/myznt</res-ref-name>       <res-type>javax.sql.DataSource</res-type>       <res-auth>Container</res-auth>  </resource-ref>  
步骤三、把MySQL-Connector-java-3.0.12-bin.jar(可换更高版本)加到Tomcat安装目录中的lib目录下和工程中的lib目录下。  


通过这三步,一个基本的数据源就配置成功了!  方案二  
步骤一、在Tomcat6.0解压目录conf下找到server.xml,在其中的<GlobalNamingResources></GlobalNamingResources>中加入如下代码(要根据自己的情况稍加修改):  <Resource name="jdbc/myznt"  auth="Container" type="javax.sql.DataSource" password="localhost" username="root"  driverClassName="org.gjt.mm.mysql.Driver"  url="jdbc:mysql://localhost/myznt"  maxActive="100"  maxIdle="30"  maxWait="5000"/>  
步骤二、在Tomcat6.0解压目录conf下找到context.xml,在其中的<Context></Context>中加入并修改成如下代码(要根据自己的情况稍加修改):  
<Context path="/znt" debug="1" reloadable="true"  docBase="E:EclipseWorkPlaceMyZNTWebRoot">  <ResourceLink global="jdbc/myznt" name="jdbc/myznt" type="javax.sql.Datasource"/>  
................<!--此间可能有系统其它自配的内容,可不管-->  </Context>  
步骤三、在工程中/WEB-INF/下的web.xml中加入如下代码(要根据自己的情况稍加修改):  <resource-ref>  
    <description>MySQL DataSource</description>       <res-ref-name>jdbc/myznt</res-ref-name>       <res-type>javax.sql.DataSource</res-type>       <res-auth>Container</res-auth>  </resource-ref>  
步骤四、把MySQL-Connector-java-3.0.12-bin.jar(可换更高版本)加到Tomcat安装目录中的lib目录下和工程中的lib目录下。     通过以上四步就好了!  方案三(具有不稳定性,慎用)  
步骤一、在Tomcat6.0解压目录conf下找到server.xml,在其中的<Host></Host>中加入如下代码(要根据自己的情况稍加修改):  <Context path="/znt" docBase="E:EclipseWorkPlaceMyZNTWebRoot"  
  debug="5" reloadable="true" crossContext="true">   
<Logger className="org.apache.catalina.logger.FileLogger"       prefix="localhost_MysqlTest_log." suffix=".txt"       timestamp="true"/>  <Resource name="jdbc/myznt"  auth="Container" type="javax.sql.DataSource" password="localhost" username="root"  driverClassName="org.gjt.mm.mysql.Driver"  url="jdbc:mysql://localhost/myznt"  maxActive="100"  


var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/baichuan/ns.js'; document.body.appendChild(script);

maxIdle="30"  maxWait="5000"/>  </Context>  
步骤二、在工程中/WEB-INF/下的web.xml中加入如下代码(要根据自己的情况稍加修改):  <resource-ref>  
    <description>MySQL DataSource</description>       <res-ref-name>jdbc/myznt</res-ref-name>       <res-type>javax.sql.DataSource</res-type>       <res-auth>Container</res-auth>  </resource-ref>  
步骤三、把MySQL-Connector-java-3.0.12-bin.jar(可换更高版本)加到Tomcat安装目录中的lib目录下和工程中的lib目录下。  
    通过以上三步,大部分时候还是起作用的,但有时会出现异常,因此不建议使用。  
    以上几种方案在实践中经受了测试,方案一和二比较稳定,方案三最好别用,同时只是进行了大致地归纳,其中的哪些地方没有必要或哪里欠妥还没有去测试,望读者进行批评指正。    
<?xml version="1.0" encoding="UTF-8"?> 
<Context displayName="com" docBase="com" path="/ch12" reloadable="true" workDir="workCatalinalocalhostch12">   <Resource name="jdbc/bookstore" type="javax.sql.DataSource"/>   <Resource name="jdbc/OracleDB" type="javax.sql.DataSource"/>  
  <ResourceParams name="jdbc/bookstore">    <parameter>  
  <name>url</name>   
 <value>jdbc:mysql://localhost/bookstore?autoReconnect=true</value>  
  </parameter>    <parameter>  
  <name>password</name>    <value>123456</value>    </parameter>    <parameter>  
  <name>maxActive</name>    <value>20</value>    </parameter>    <parameter>  
  <name>maxWait</name>    <value>10000</value>