hibernate通过JNDI使用tomcat连接池

首先配置Tomcat到和SQLServer2000的连接池,这里使用的是jtds,没有这个包的去这里下http://jtds.sourceforge.net/,放到项目的lib下面,Tomcat 的server.xml文件中添加类似如下配置:

<Context path="/quality" docBase="quality" debug="0" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_quality_log." suffix=".txt" timestamp="true"/>
<Resource name="jdbc/connectDB" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/connectDB">
<parameter>
      <name>maxActive</name>
      <!-- Maximum number of DB connections in pool.Set to 0 for no limit. -->
      <value>100</value>
</parameter>
<parameter>
      <name>maxIdle</name>
      <!-- Maximum number of idle DB connections to retain in pool.Set to 0 for no limit. -->
      <value>30</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>
      <!-- Database User Name -->
      <value>Iorishinier</value>
</parameter>
<parameter>
      <name>password</name>
      <!-- User Password -->
      <value>mypasswd</value>
</parameter>
<parameter>
      <name>driverClassName</name>
      <!-- Database Driver Class Name -->
      <value>net.sourceforge.jtds.jdbc.Driver</value>
</parameter>
<parameter>
      <name>url</name>
      <!-- Database Address -->
      <value>jdbc:jtds:sqlserver://127.127.127.127:1433/Northwind</value>
</parameter>
</ResourceParams>
</Context>

项目的web.xml添加如下配置:

<web-app>
....
<resource-ref>
<description>connectDB test</description>
<res-ref-name>jdbc/connectDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
....
</web-app>

hibernate.cfg.xml文件添加如下配置:

<property name="connection.datasource">
      java:comp/env/jdbc/connectDB
</property>
<property name="show_sql">true</property>
<property name="dialect">
      org.hibernate.dialect.SQLServerDialect
</property>

最后,如果出现如下异常
Could not create resource factory
ClassNotFoundException:org.apache.commons.dbcp.BasicDataSourceFactory
请到 http://jakarta.apache.org/commons/index.html 下载 DBCP和Pool两个包放到项目lib下

=================================================================


       <parameter>
          <name>driverClassName</name>
          <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
       </parameter>
       <parameter>
         <name>url</name>
         <value>jdbc:microsoft:sqlserver://172.16.7.169:1433;DatabaseName=guohua</value>
       </parameter>
    
       <!--parameter>
          <name>driverClassName</name>
          <value>net.sourceforge.jtds.jdbc.Driver</value>
       </parameter>
       <parameter>
         <name>url</name>
         <value>jdbc:jtds:sqlserver://172.16.7.169:1433;DatabaseName=guohua;charset=GBK</value>
       </parameter-->

你可能感兴趣的:(apache,tomcat,Hibernate,jdbc,Microsoft)