歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Hibernate連接配置文件

Hibernate連接配置文件

日期:2017/3/1 10:39:45   编辑:Linux編程
  1. <!-- C3P0數據源管理連接池 -->
  2. <?xml version='1.0' encoding='utf-8'?>
  3. <!DOCTYPE hibernate-configuration PUBLIC
  4. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  5. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  6. <hibernate-configuration>
  7. <session-factory>
  8. <!-- Database connection settings -->
  9. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  10. <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
  11. <property name="connection.username">root</property>
  12. <property name="connection.password">root</property>
  13. <!-- JDBC connection pool (use the built-in)
  14. 不使用內建連接詞,hibernate參考手冊的一段
  15. Hibernate 自帶的連接池算法相當不成熟。它只是為了讓你快些上手,並不適合用於產品系統或性能測試中。
  16. 出於最佳性能和穩定性考慮你應該使用第三方的連接池。只需要用特定連接池的設置替換 hibernate.connection.pool_size 即可。
  17. <property name="connection.pool_size">5</property>
  18. -->
  19. <!-- 自建c3p0連接池,要加入c3p0-0.9.1.jar-->
  20. <property name="hibernate.c3p0.min_size">5</property>
  21. <property name="hibernate.c3p0.max_size">20</property>
  22. <property name="hibernate.c3p0.timeout">1800</property>
  23. <!-- 最大的PreparedStatement的數量 -->
  24. <property name="hibernate.c3p0.max_statements">100</property>
  25. <!--配置默認schema
  26. <property name="hibernate.default_schema" value="myschema"/>
  27. -->
  28. <!-- SQL dialect -->
  29. <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
  30. <!-- Enable Hibernate's automatic session context management -->
  31. <property name="current_session_context_class">thread</property>
  32. <!-- Disable the second-level cache -->
  33. <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
  34. <!-- Echo all executed SQL to stdout -->
  35. <property name="show_sql">true</property>
  36. <!-- Drop and re-create the database schema on startup -->
  37. <property name="hbm2ddl.auto">update</property>
  38. <mapping resource="net/hnspi/entity/Record.hbm.xml" />
  39. <mapping resource="net/hnspi/entity/Account.hbm.xml" />
  40. </session-factory>
  41. </hibernate-configuration>

數據源配置:

[html]

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Context path="/">
  3. <Resource auth="Container"
  4. driverClassName="com.mysql.jdbc.Driver"
  5. type="javax.sql.DataSource"
  6. name="jdbc/datasuourcePool"
  7. url="jdbc:mysql://127.0.0.1:3306/hibernate?useUnicode=true&characterEncoding=GBK&autoReconnect=true"
  8. username="zhangh"
  9. password="123456"
  10. logAbandoned="true"
  11. maxActive="5"
  12. maxIdle="2"
  13. maxWait="5000"
  14. removeAbandoned="true"
  15. removeAbandonedTimeout="300"
  16. testOnReturn="true"
  17. testWhileIdle="true"
  18. validationQuery="select now()"/>
  19. </Context>

容器管理連接池,連接通過數據源取得

注意:數據庫連接jar包要放到Tomcat(小弟用的是tomcat)的lib目錄,不然會報找不到Driver的錯誤。我想原因大概是由容器管理連接池,就是在服務器啟動的時候,就會建立好連接池,這個時候肯定是需要數據庫連接的jar包。也就是連接的jar包在應用運行之前就已經使用到了,所以放在應用的lib下也不會解決問題。

[html]
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5. <hibernate-configuration>
  6. <session-factory>
  7. <!-- 交由容器管理數據庫連接池 -->
  8. <property name="connection.datasource">java:comp/env/jdbc/datasuourcePool</property>
  9. <property name="connection.provider_class">org.hibernate.connection.DatasourceConnectionProvider</property>
  10. <!-- 配置默認schema <property name="hibernate.default_schema" value="myschema"/> -->
  11. <!-- SQL dialect -->
  12. <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
  13. <!-- Enable Hibernate's automatic session context management -->
  14. <property name="current_session_context_class">thread</property>
  15. <!-- Disable the second-level cache -->
  16. <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
  17. <!-- Echo all executed SQL to stdout -->
  18. <property name="show_sql">true</property>
  19. <!-- Drop and re-create the database schema on startup -->
  20. <property name="hbm2ddl.auto">update</property>
  21. <!-- 配置實體映射 -->
  22. <mapping resource="com/akwolf/bean/Event.hbm.xml"/>
  23. </session-factory>
  24. </hibernate-configuration>
Copyright © Linux教程網 All Rights Reserved