Hibernate中Session與本地線程綁定
------------------siwuxie095
Hibernate 中 Session 與本地線程綁定
1、Session 類似於 JDBC 的連接 Connection
2、Session 對象是單線程對象,只能自己使用,不能共用
將 Session 與本地線程綁定,保證 Session 對象絕對是一個單線程對象
3、Hibernate 幫助我們實現了 Session 與本地線程綁定(底層是 ThreadLocal)
4、獲取與本地線程綁定的 Session
(1)在 Hibernate 核心配置文件中進行配置
<!-- 配置 Session 綁定本地線程 --> <property name="hibernate.current_session_context_class">thread</property> |
(2)調用 SessionFactory 的 getCurrentSession() 方法獲取 Session
HibernateUtils.java:
package com.siwuxie095.utils;
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;
public class HibernateUtils {
static Configuration cfg=null; static SessionFactory sessionFactory=null;
// // private static final Configuration cfg; // private static final SessionFactory sessionFactory;
//靜態代碼塊 static { //加載核心配置文件 cfg=new Configuration(); cfg.configure(); sessionFactory=cfg.buildSessionFactory();
}
//提供方法返回 sessionFactory public static SessionFactory getSessionFactory() { return sessionFactory; }
//提供方法返回與本地線程綁定的 Session public static Session getCurrentSession() { return sessionFactory.getCurrentSession(); }
} |
測試函數 testSession():
@Test public void testSession(){
Session session=null; Transaction tx=null;
try {
session=HibernateUtils.getCurrentSession(); //開啟事務 tx=session.beginTransaction();
//添加操作 User user=new User(); user.setUsername("小明"); user.setPassword("8888"); user.setAddress("中國"); session.save(user);
//提交事務 tx.commit(); } catch (Exception e) { //回滾事務 tx.rollback(); } finally { //最後不用關閉 Session,隨著線程 //執行結束,Session 將自動關閉 // //下面一行代碼不用寫,否則將報錯 //session.close(); }
} |
4、與本地線程綁定的 Session 最後不用手動關閉,當線程執行結束後,
Session 將會自動關閉。如果手動關閉,將報錯
「二者是一根繩上的螞蚱,線程一旦結束,Session 就會自動關閉」
註:
另:
User.hbm.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd
該 dtd 文件是用於提示的文件,聯網即有提示信息,也可 手動添加:Window->Preferences->XML->XML Catalog, 點擊 Add 添加即可,Location 即路徑,Key 即上面的鏈接, Key type 即 URI -->
<!-- 根標簽 --> <hibernate-mapping>
<!-- (1) class 標簽:配置實體類和數據庫表的對應; name 屬性:實體類的全路徑,即 全限定名; table 屬性:數據庫表的名稱(數據庫表由 Hibernate 自動生成) --> <class name="com.siwuxie095.entity.User" table="t_user">
<!-- (2) id 標簽:配置實體類 id 和表 id 對應(主鍵); name 屬性:實體類裏 id 屬性名稱; column 屬性:生成表中 id 字段名稱 --> <!-- Hibernate 要求實體類有一個屬性唯一值, Hibernate 要求表中字段有一個屬性唯一值 --> <id name="uid" column="uid"> <!-- 設置數據庫表 id 的增長策略, native:主鍵 id 值自動增長 --> <generator class="native"></generator> </id>
<!-- (3) property 標簽:配置其它屬性和表中字段對應; name 屬性:實體類屬性名稱; column 屬性:生成表中字段名稱 --> <property name="username" column="username"></property> <property name="password" column="password"></property> <property name="address" column="address"></property> </class>
</hibernate-mapping> |
Hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- 根標簽 --> <hibernate-configuration> <session-factory>
<!-- 第一部分:配置數據庫信息(必須) -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 或使用 jdbc:mysql:///hibernate_db 代替,省略 localhost --> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_db</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">8888</property>
<!-- 第二部分:配置 Hibernate 信息(可選) -->
<!-- 輸出底層 sql 語句 --> <property name="hibernate.show_sql">true</property> <!-- 輸出底層 sql 語句格式 --> <property name="hibernate.format_sql">true</property> <!-- Hibernate 幫助創建表,不是自動創建,而需要配置之後。 update:如果已經有表,就更新,如果沒有,就自動創建 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 配置數據庫方言,讓 Hibernate 框架識別不同數據庫自己特有的語句。 如:在 MySQL 中實現分頁的關鍵字 limit,只能在 MySQL 中使用,而 在 Oracle 中實現分頁的關鍵字則是 rownum --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 事務隔離級別: hibernate.connection.isolation = 4
1 - Read uncommitted isolation(未提交讀:臟讀、不可重復讀、幻讀均可能發生) 2 - Read committed isolation(已提交讀:防止臟讀發生,不可重復讀、幻讀均可能發生) 4 - Repeatable read isolation(可重復讀:防止臟讀、不可重復讀發生,幻讀可能發生) 8 - Serializable isolation(可串行化:防止臟讀、不可重復讀、幻讀發生) --> <property name="hibernate.connection.isolation">4</property> <!-- 配置 Session 綁定本地線程 --> <property name="hibernate.current_session_context_class">thread</property>
<!-- 第三部分:引入映射配置文件,把映射配置文件放到核心配置文件(必須) --> <mapping resource="com/siwuxie095/entity/User.hbm.xml"/> </session-factory> </hibernate-configuration> |
【made by siwuxie095】
Hibernate中Session與本地線程綁定