【spring 6】Spring和Hibernate的整合:编程式事务
一、编程式事务简介
在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择。用过 Hibernate 的人都知道,我们需要在代码中显式调用beginTransaction()、commit()、rollback()等事务管理相关的方法,这就是编程式事务管理。通过 Spring 提供的事务管理 API,我们可以在代码中灵活控制事务的执行。在底层,Spring 仍然将事务操作委托给底层的持久化框架来执行。
二、实例分析
2.1,首先,导入Spring、Hibernate的相关jar包,以及所使用数据库的驱动jar,搭建其基本环境
2.2,其次,编写相关代码
ApplicationContext配置文件:
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- 注入D层实现 -->
<bean id="logManagerImpl" class="com.angel.usermanager.manager.LogManagerImpl"></bean>
<bean id="userManagerImpl" class="com.angel.usermanager.manager.UserManagerImpl">
<property name="logManager" ref="logManagerImpl"></property>
<property name="log" ref="logModel"></property>
</bean> <!-- 注入实体 -->
<bean id="userModel" class="com.angel.usermanager.domain.User"></bean>
<bean id="logModel" class="com.angel.usermanager.domain.Log"></bean> </beans>
在Hibernate的配置文件中添加:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><property name="hibernate.current_session_context_class">thread</property></span>
注意:
* 如果是本地事务(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
* 如果是全局事务(jta事务)
<property name="hibernate.current_session_context_class">jta</property>
HibernateUtils类:
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.angel.usermanager.util; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class HibernateUtils { private static SessionFactory factory; private HibernateUtils() {
} static {
try {
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
}catch(Exception e) {
e.printStackTrace();
throw new java.lang.RuntimeException(e);
}
} public static SessionFactory getSessionFactory() {
return factory;
} public static Session getSession() {
return factory.openSession();
} public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
}
</span>
UserManagerImpl类:
</pre><pre name="code" class="java">package com.angel.usermanager.manager; import java.util.Date; import org.hibernate.Session; import com.angel.usermanager.domain.Log;
import com.angel.usermanager.domain.User;
import com.angel.usermanager.util.HibernateUtils; public class UserManagerImpl implements UserManager { private LogManager logManager;
public void setLogManager(LogManager logManager) {
this.logManager = logManager;
} private Log log;
public void setLog(Log log){
this.log=log;
} public void addUser(User user) {
Session session = null;
try {
<span style="color:#ff6666;">session = HibernateUtils.getSessionFactory().getCurrentSession();</span>
session.beginTransaction(); session.save(user); log.setType("操作日志");
log.setTime(new Date());
log.setDetail("Agel7"); logManager.addLog(log); session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}
} }
测试类:
<pre name="code" class="java">package com.angel.usermanager.test; import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.angel.usermanager.domain.User;
import com.angel.usermanager.manager.UserManager; public class Client {
public static void main(String[] args) {
// 读取配置文件
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager userManager = (UserManager) factory.getBean("userManagerImpl");
User user = (User) factory.getBean("userModel");
user.setName("张三7"); userManager.addUser(user); }
}
三、总结
openSession和getCurrentSession的区别:openSession必须手动关闭,currentSession在事务结束后自动关闭;openSession没有和当前线程绑定,currentSession和当前线程绑定
采用编程式事务,方便了我们对于事务的理解。但是,事务管理的代码散落在业务逻辑代码中,破坏了原有代码的条理性,并且每一个业务方法都包含了类似的启动事务、提交/回滚事务的样板代码。这就导致了相同代码的反复出现,有没有什么解决方案呢?第一,spring提供了简化方法,在数据访问层非常常见的模板回调模式。第二,声明式事务
【spring 6】Spring和Hibernate的整合:编程式事务的更多相关文章
- 全面分析 Spring 的编程式事务管理及声明式事务管理
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- Spring学习8-Spring事务管理(编程式事务管理)
一.Spring事务的相关知识 1.事务是指一系列独立的操作,但在概念上具有原子性. 比如转账:A账号-100, B账号+100,完成.这两个操作独立是没问题的. 但在逻辑上,要么全部完成,要么一 ...
- spring 编程式事务管理和声明式事务管理
编程式事务管理 Spring 的编程式事务管理概述 在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择.用过 Hibernate 的人都知道,我们需要在代码中显式调用be ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理--转
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- 开涛spring3(9.3) - Spring的事务 之 9.3 编程式事务
9.3 编程式事务 9.3.1 编程式事务概述 所谓编程式事务指的是通过编码方式实现事务,即类似于JDBC编程实现事务管理. Spring框架提供一致的事务抽象,因此对于JDBC还是JTA事务都是 ...
- spring事务管理——编程式事务、声明式事务
本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本教程假定您已经掌握了 ...
- 8.spring:事务管理(上):Spring的数据库编程、编程式事务管理
Spring的数据库编程 Spring框架提供了JDBC模板模式------>JdbcTemplate 简化了开发,在开发中并不经常是使用 实际开发更多使用的是Hibernate和MyBatis ...
- Spring编程式事务管理及声明式事务管理
本文将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. Spring 事务属性分析 事务管理 ...
- 【Spring】编程式事务和声明式事务
一.概述 二.准备工作 1. 创建表 2. 创建项目并引入Maven依赖 3. 编写实体类 4. 编写Dao层 5. 业务层 6. XML中的配置 7. 测试 三.编程式事务 1. 在业务层代码上使用 ...
随机推荐
- C#数字千分位问题
1.C#中用最简单的方法把数字(不含小数)转换为千分位格式: 如1234567变成1,234,567 方法:1234567.ToString("###,###") 或 ...
- FindViewByMe插件
FindViewByMe 是一个自动生成 FindViewById 代码的 IDEA/Android Studio 插件, 支持 Activity.Fragment 和 ViewHolder 中的 f ...
- PLSQL_性能优化工具系列10_Automatic Database Diagnostic Monitor - ADDM
2014-09-06 Created By BaoXinjian
- php 预定义接口
Traversable Traversable { } 作用:检测一个类是否可以使用 foreach 进行遍历的接口. php代码中不能用.只有内部的PHP类(用C写的类)才可以直接实现Travers ...
- Spring中IOC和AOP的详细解释
我们是在使用Spring框架的过程中,其实就是为了使用IOC,依赖注入,和AOP,面向切面编程,这两个是Spring的灵魂. 主要用到的设计模式有工厂模式和代理模式. IOC就是典型的工厂模式,通过s ...
- Mac下的eclipse按住ctrl点击无法查看类文件
问题主要是项目的.project或者.build文件有问题,github上check的另一个项目就没有这样的现象,可以新建一个项目,然后把新建项目的这两个文件一一替换无法ctrl点击的项目中的文件. ...
- android tween动画效果
anim文件夹下 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android=&quo ...
- Eclipse中Build Workspace 优化
在开发中,发现eclipse 的 Build Workspace 很慢很慢,只要是由于验证js, css, xml 等等静态的文件导致. 我遇到的问题是,项目中有一个js文件很大,怎么都不能Build ...
- dede数据库类使用方法$dsql【转】
http://www.cnblogs.com/xcxc/p/3601909.html dedecms的数据库操作类,非常实用,在二次开发中尤其重要,这个数据库操作类说明算是奉献给大家的小礼物了. 引入 ...
- Grid++Report的几点总结
一.同事解决方案: 1.在View文件夹下建立报表文件A,用来作为报表呈现的载体.这个页面负责加载报表模板与加载数据源.其中报表模板由于后缀名为grf,在MVC中不做任何修改的情况下,系统会做路由处理 ...