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 final 亦可,不过此时不能等于 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】

原文地址:https://www.cnblogs.com/siwuxie095/p/7286931.html