Hibernate uses the Tomcat connection pool (sqlserver2000) through JNDI)

Source: Internet
Author: User

First, configure tomcat to the connection pool with sqlserver2000. jtds is used here. If this package is not available, go to http://jtds.sourceforge.net/and set it to the project lib. the following configuration is added to the Tomcat server. xml file:

<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. basicperformancefactory </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>

Add the following configuration to the project's 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>

Add the following configuration to the hibernate. cfg. xml file:

<Property name = "connection. datasource">
Java: COMP/ENV/jdbc/connectdb
</Property>
<Property name = "show_ SQL"> true </property>
<Property name = "dialect">
Org. hibernate. dialect. sqlserverdialect
</Property>

Finally, if the following exception occurs:
Cocould not create resource Factory
Classnotfoundexception: org. Apache. commons. DBCP. basicdatasourcefactory
Go to the http://jakarta.apache.org/commons/index.html to download the DBCP and pool packages under the project 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 -->

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.