About how to configure the c3p0 connection pool to connect to MySQL in hibernate

Source: Internet
Author: User

I read the hibernate in the book <struts2 + hibernate + spring> of Li Gang a few days ago. The configuration file is also typed into hibernate according to the above. cfg. student. student configured in Java. HBM. the XML file is okay, but the connection can not open is reported. Depressed, I copied the configuration file and source code in the book to a new web project under myeclipse and ran it. Hi, I am down, then <propertyname = "hibernate. connection. URL "> JDBC: mysql: // localhost: 3306/test </property>" test "database, which must be created first. If the connection cannot be opened, an error is reported. Then I ran the depressing project I wrote earlier. Hey, it's okay. I am also very depressed. How can I do it now !!!
I'll post the source code and configuration files. Let's see what it is,
I. hibernate. cfg. xml file <? XML version = '1. 0' encoding = 'gbk'?>
<! Doctype hibernate-configuration public
"-// Hibernate/hibernate configuration DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd>

<! -- Generated by myeclipse hibernate tools. -->
<Hibernate-configuration>
<Session-factory>
<Property name = "connection. driver_class"> com. MySQL. JDBC. Driver </property>
<! -- Specify the URL to connect to the database and the name of the database connected to hibernate -->
<Property name = "connection. url"> JDBC: mysql: // localhost/TT </property>
<! -- Database username -->
<Property name = "connection. username"> root </property>
<! -- Database Password -->
<Property name = "connection. Password"> 108226 </property>
<! -- Use the c3p0 connection pool to connect to the maximum number of connections allowed -->
<Property name = "hibernate. c3p0. max_size"> 20 </property>
<! -- Minimum number of connections allowed by c3p0 -->
<Property name = "hibernate. c3p0. min_size"> 1 </property>
<! -- Specify the time when the connection pool fails -->
<Property name = "hibernate. c3p0. Timeout"> 50 </property>
<! -- Specify the maximum number of statement cached in the connection pool -->
<Property name = "hibernate. c3p0. max_statements"> 100 </property>
<! -- Hibernate. c3p0. Timeout indicates how long the connected object should be destroyed,
Note: it should be ", but who will destroy it? A thread needs to follow hibernate. c3p0. idle_test_period
Set the time interval to automatically verify these linked objects and destroy timeout -->
<Property name = "hibernate. c3p0. idle_test_period"> 30 </property>
<! -- When the connections in the connection pool are used up, c3p0 obtains the new connections -->
<Property name = "hibernate. c3p0. acquire_increment"> 2 </property>
<! -- Database dialect connected to the database -->
<Property name = "dialect"> org. hibernate. dialect. mysqlinnodbdialect </property>
<! -- Verify whether the connection is available every time -->
<Property name = "hibernate. c3p0. Validate"> true </property>
<Property name = "hbm2ddl. Auto"> Create </property>
<Mapping Resource = "Dao/students. HBM. xml"/>
</Session-factory>
</Hibernate-configuration>
Ii. Student. HBM. xml file
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<! --
Mapping File autogenerated by myeclipse persistence tools
-->
<Hibernate-mapping package = "Dao">
<Class name = "Dao. Student" table = "tb_student">
<ID name = "studentid">
<Generator class = "Identity"/>
</ID>
<Property name = "name"> </property>
<Property name = "Age"> </property>
<Property name = "sex"> </property>
</Class>
</Hibernate-mappin
3. Student. Java File

Configuration conf = new configuration (). Configure ();
// Create sessionfactory with configuration
Sessionfactory Sf = Conf. buildsessionfactory ();
// Instantiate the session
Session sess = SF. opensession ();
// Start the transaction
Transaction Tx = sess. begintransaction ();
// Create a message instance
Student s = new student ();
// Set the message title and content
S. setname ("eee ");
S. setage (5 );
S. setsex ("boy ");
S. setstudentid (2 );
Sess. Save (s );
// Submit the transaction
TX. Commit ();
// Close the session
Sess. Close ();

Related Article

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.