關於hibernate配置c3p0串連池串連mysql問題

來源:互聯網
上載者:User

前些天看了李剛<struts2+hibernate+spring>那書上的hibernate,設定檔也是按照上面敲進hibernate.cfg.xml檔案的,Student.java配置的Student.hbm.xml檔案覺得也沒什麼問題,但不知到怎麼回事就是報 connection can not open。鬱悶啊,後來我就把書裡面的設定檔和源碼,複製到MyEclipse下面的一個新web項目中,運行,嗨真下行了,這說明下,那<propertyname="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>"test"資料庫,必須是先建立的,要不是會報connection 打不開的錯誤。於是接著運行下我之前寫的那鬱悶項目,嘿,居然也行了。我也很鬱悶啊,之前不一樣的配置麼,怎麼現在就行了呢!!!
我貼下源碼 和設定檔吧,大俠 幫忙看下到底是咋啦,
一:hibernate.cfg.xml檔案<?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>
<!-- 指定串連資料庫的url,hibernate串連的資料庫名 -->
<property name="connection.url">jdbc:mysql://localhost/tt</property>
       <!-- 資料庫使用者名稱 -->
        <property name="connection.username">root</property>
       <!-- 資料庫密碼 -->
       <property name="connection.password">108226</property>
        <!--用c3p0串連池串連所允許的最大串連數-->
        <property name="hibernate.c3p0.max_size">20</property>
        <!-- c3p0所允許的最少串連數 -->
        <property name="hibernate.c3p0.min_size">1</property>
        <!-- 指定串連池串連失效的時間 -->
        <property name="hibernate.c3p0.timeout">50</property>
        <!-- 指定串連池緩衝最大的儲存多少個statement -->
        <property name="hibernate.c3p0.max_statements">100</property>
        <!-- hibernate.c3p0.timeout表示連線物件多長時間應該被銷毀,
          注意,是”應該“,但是誰來銷毀它呢,需要一個線程按照hibernate.c3p0.idle_test_period
         設定的時間間隔去自動校正這些連結化物件並銷毀timeout的 -->
        <property name="hibernate.c3p0.idle_test_period">30</property>
       <!--  當串連池裡面的串連用完的時候,C3P0一下擷取的新的串連數  -->
        <property name="hibernate.c3p0.acquire_increment">2</property>
        <!-- 串連資料庫的資料庫方言 -->
       <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
        <!-- 每次都驗證串連是否可用 -->
        <property name="hibernate.c3p0.validate">true</property>
        <property name="hbm2ddl.auto">create</property>
        <mapping resource="dao/students.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
二、student.hbm.xml檔案
<?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
三、Student.java檔案

Configuration conf = new Configuration().configure();
//以Configuration建立SessionFactory
SessionFactory sf = conf.buildSessionFactory();
//執行個體化Session
Session sess = sf.openSession();
//開始事務
Transaction tx = sess.beginTransaction();
//建立訊息執行個體
Student s = new Student();
//設定訊息標題和訊息內容
s.setName("eee");
s.setAge(5);
s.setSex("boy");
s.setStudentId(2);
sess.save(s);
//提交事務
tx.commit();
//關閉Session
sess.close();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.