spring 使tomcat连接oracle的数据库连接池

1.首先配置tomcat连接池server.xml里面(不详细说了google上面很多例子)

<Context path="/datatest" docBase="E:/dataSource/webapps" debug="1" reloadable="true">
  <Resource name="jdbc/name" auth="Container" type="javax.sql.DataSource"/>                     
    <ResourceParams name="jdbc/name"> 
    <parameter>
     <name>maxActive</name>
     <!-- Maximum number of DB connections in pool.Set to 0 for no limit. -->
     <value>20</value>
    </parameter>
    <parameter>
     <name>maxIdle</name>
     <!-- Maximum number of idle DB connections to retain in pool.Set to 0 for no limit. -->
     <value>10</value>
    </parameter>
    <parameter>
     <name>maxWait</name>
     <!-- Maximum time to wait for a DB connection to become available in ms.An exception is thrown if this timeout is exceeded.Set to -1 to wait indefinitely. -->
     <value>10000</value>
    </parameter>   
    <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>
        <parameter>                     
          <name>username</name>                     
          <value>biz</value>                     
        </parameter>                     
        <parameter>                     
          <name>password</name>                     
          <value>biz</value>                     
        </parameter>                     
        <parameter>                     
          <name>driverClassName</name>                     
          <value>oracle.jdbc.driver.OracleDriver</value>                     
        </parameter>                     
        <parameter>                     
          <name>url</name>                     
          <value>jdbc:oracle:thin:@192.168.0.2:1521:orcl</value>                     
        </parameter>                     
       </ResourceParams>
        </Context> 

注:把oracle的jdbc连接包拷近tomcat包里面.(路径:/common/lib/)

2.配置spring的.xml配置文件我机子上名称是application-context.xml  (个人习惯名称)

  <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
      <value>java:comp/env/jdbc/name</value>
    </property>
  </bean>

java:comp/env/jdbc/name     这段是tomcat里面配置的连接池名称name

3.在web.xml里面配置

  <resource-ref>
    <description>Oracle Datasource example</description>
    <res-ref-name>jdbc/webbiz</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

4.总结:

4.1.使用的时候不好调试application程序

4.2数据库服务器重启后得重启Tomcat

4.3方便以后管理,连接密码用户名都在一个文件里面,web项目里面和数据库用户名密码脱离.


文章原文地址:http://blog.csdn.net/weiyingyi0/archive/2006/03/28/641404.aspx

你可能感兴趣的:(spring,oracle,tomcat,exception,数据库连接池,数据库服务器)