Hibernate之事务

1.事务的四大特性


2.三大并发问题


3.四大隔离级别

READ_UNCOMMIT 读未提交。可能导致脏读、不可重复读、幻读。
READ_COMMIT 读已提交。可能导致不可重复读、幻读。
REPEATABLE_READ 可重复读。可能导致幻读。
SERIALIZABLE 串行化。可避免脏读、不可重复读、幻读。

【事务的控制应该在Service层,而不是Dao层】



案例:获取与本地线程绑定的session

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> <!-- 驱动类 -->
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb1</property> <!-- 路径 -->
		<property name="hibernate.connection.username">root</property> <!-- 用户名 -->
		<property name="hibernate.connection.password">1980217948</property> <!-- 密码 -->
		
		<!-- 可选:配置Hibernate信息 -->
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 方言 -->
		<property name="hibernate.show_sql">true</property> <!-- 显示SQL语句 -->
		<property name="hibernate.format_sql">true</property> <!-- 格式化SQL语句 -->
		<property name="hibernate.hbm2ddl.auto">update</property> <!-- 无表创建,有表更新 -->
		
		<!-- 获取与本地线程绑定的session,需要以下配置 -->
		<property name="hibernate.current_session_context_class">thread</property>
		
		<!-- 必须:引入映射文件 -->
		<mapping resource="zh/hibernate/entity/User.hbm.xml"/>
	
	</session-factory>

</hibernate-configuration>

HibernateUtils.java

package zh.hibernate.utils;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {

	private static Configuration configuration;
	private static SessionFactory sessionFactory;

	// 静态代码块,加载src下的hibernate.cfg.xml
	static {
		configuration = new Configuration();
		configuration.configure();
		// 创建SessionFactory时,会根据实体类和表的映射关系,在数据库中创建表
		sessionFactory = configuration.buildSessionFactory();
	}

	// 获取sessionFactory
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
	
}

CurrentSessionDemo.java

package zh.hibernate.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import zh.hibernate.entity.User;
import zh.hibernate.utils.HibernateUtils;

public class CurrentSessionDemo {

	public static void main(String[] args) {

		SessionFactory sessionFactory = null;
		Session session = null;
		Transaction transaction = null;
		try {
			sessionFactory = HibernateUtils.getSessionFactory();
			/**
			 * 获取与本地线程绑定的session;
			 * 前提:在hibernate.cfg.xml中配置
			 * <property name="hibernate.current_session_context_class">thread</property>
			 */
			session = sessionFactory.getCurrentSession();
			transaction = session.beginTransaction();// 开启事务

			User user1 = session.get(User.class, 2);// 立即发送查询语句
			System.out.println(user1);

			transaction.commit();// 提交事务
		} catch (Exception e) {
			transaction.rollback();// 回滚事务
		} finally {
			// session.close();// 【注意】与本地线程绑定的session,线程介绍后它自动关闭;手动关闭,会报错
			sessionFactory.close();// 实际开发中,不需要关闭
		}

	}

}

猜你喜欢

转载自blog.csdn.net/qq_41706150/article/details/81009687