Data Persistence hibernate standard database connection pool configuration)

Source: Internet
Author: User

From: http://hi.baidu.com/lanruijin/blog/item/b57938d1267e19d2562c8453.html

Taking Tomcat + MYSQL as an example, Hibernate has two ways to connect to the database: 1. The database connection pool managed by hibernate 2. hibernate uses the connection pool configured by the current container to view many hibernate configuration tutorials on the Internet, the actual tutorials are complicated.
Disorganized. It is a note at most. For those who need to know about it, read
These spam do more harm than good. Forgive me for using the word spam, but isn't it,
I just need to adjust it and pile up a "Technical article" with just a few words, no
It is rubbish to check whether others can understand the rules and regulations. So I write this article, it is not rubbish, so I don't talk much about it, Hibernate
+ Tomcat configures the standard process of the database connection pool. Preparations:
1. download the latest hibernate from www.hibernate.org. 3.0 is recommended
.
2. Install MySQL and copy the MySQL driver to common/lib in Tomcat.
3. Create a new Web Application in Tomcat.
).
4. Copy all the jar files in the library lib folder under hibernate
Web application WEB-INF/lib.
5. Create the hibernate. cfg. xml file under the web application WEB-INF/classes directory
The content is as follows:
<? XML version = '1. 0' encoding = 'gbk'?>
<! Doctype hibernate-Configuration
Public "-// hibernate/hibernate configuration DTD // en"
Http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd>
In fact, this step creates an XML file without any configuration. 1. database connection pool managed by hibernate
As the name suggests, the Hibernate framework includes its own methods for managing database connections,
You only need to configure the hibernate. cfg. xml file.
Complete hibernate. cfg. xml: <? XML version = '1. 0' encoding = 'gbk'?>
<! Doctype hibernate-Configuration
Public "-// hibernate/hibernate configuration DTD // en"
Http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd>
<! -- Hibernate self-Manager connection pool -->
<Hibernate-configuration>
<Session-factory>
<Property name = "show_ SQL"> true </property>
<Property name = "dialect"> org. hibernate. dialect. mysqldialect </property>
<Property name = "connection. driver_class"> com. MySQL. JDBC. Driver </property>
<Property name = "connection. url"> JDBC: mysql: // localhost: 3306/nihongo? Useunicode = true </property>
<Property name = "connection. username"> root </property>
<Property name = "connection. Password"> Kevin </property>
<Mapping Resource = "cat/cat. HBM. xml"/>
</Session-factory>
</Hibernate-configuration> modify the attribute values based on your needs. 2. hibernate uses the connection pool configured by the current container
The container (such as Tomcat) establishes a connection pool and configures JNDI. hibernate calls JNDI
Use the container connection pool. This method is a little complicated. You need to configure three XML files: 1.% atat_home %/CONF/server. XML in server. in XML, <Resource Name = "JDBC/mysql5" auth = "Container"
Type = "javax. SQL. datasource" driverclassname = "com. MySQL. JDBC. Driver"
Url = "JDBC: mysql: // localhost: 3306/nihongo? Useunicode = true"
Username = "root" Password = "Kevin" maxactive = "100" maxidle = "10"
Maxwait = "-1"/> </context> 2. Complete hibernate. cfg. xml: <? XML version = '1. 0' encoding = 'gbk'?>
<! Doctype hibernate-Configuration
Public "-// hibernate/hibernate configuration DTD // en"
Http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd>
<! -- Hibernate use JNDI to access container's connection pool -->
<Hibernate-configuration>
<Session-factory>
<Property name = "connection. datasource"> JAVA: COMP/ENV/jdbc/mysql5 </property>
<Property name = "show_ SQL"> true </property>
<Property name = "dialect"> org. hibernate. dialect. mysqldialect </property>
<Mapping Resource = "cat/cat. HBM. xml"/>
</Session-factory>
</Hibernate-configuration> 3. Web. xml file of the Web application: Add the following configuration before web. xml </Web-app>: <! -- Hibernate use container's connection pool -->
<Resource-ref>
<Description> dB connection </description>
<Res-ref-Name> JDBC/mysql5 </RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
</Resource-ref> in the preceding two methods, you can configure hibernate to connect to the database. The specific difference is incomplete.

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.