【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. 在业务层代码上使用 ...
随机推荐
- 黄聪:JS实现复制到剪贴板功能,兼容所有浏览器(转)
两天前听了一个H5的分享,会议上有一句话,非常有感触:不是你不能,而是你对自己的要求太低.很简单的一句话,相信很多事情不是大家做不到,真的是对自己的要求太低,如果对自己要求多一点,那么你取得的进步可能 ...
- windows初始化后做了哪些事情
保护视力,将WINDOWS背景色变成淡绿色 绿色和蓝色对眼睛最好,建议大家在长时间用电脑后,经常看看蓝天.绿地,就能在一定程度上缓解视疲劳.同样的道理,如果我们把电脑屏幕和网页的底色变为淡淡的苹果绿, ...
- solr基于jetty容器配置后台登陆角色用户名密码
solr版本4.0.0 我的solr下载后解压在d盘,你的随意. 在D:\apache-solr-4.0.0\apache-solr-4.0.0\example\etc目录下新建role.proper ...
- "this class is not key value coding-compliant for the key ..."问题的解决
今天出现跟着MJ的思路敲的代码,自己最后运行出现这个 错误,发现是 自己在将属性和相关联的控件连线时出现了 错误.一开始取名时出现了错误,发现线连重复了. 在网上又找到了一些出现该类错误的相关解释: ...
- BIP_开发案例07_将原有Report Builer报表全部转为XML Publisher形式(案例)
2014-05-31 Created By BaoXinjian
- PLSQL_闪回删除FlashBack Drop表误删除如何进行恢复(案例)
2014-06-25 Created By BaoXinjian
- 玄机论坛Socket类库源码 当前版本 2.6.3 更新日期:10-09/2015 z
http://bbs.msdn5.com/thread-27-1-1.html 本类库采用TcpLister,TcpClient高度封装, 采用NetworkStream进行异步模式读取数据. 采用S ...
- SPREAD_NET6
SPREAD_NET6 下载地址 http://www.gcpowertools.com.cn/downloads/trial/Spread.NET/EN_SPREAD_NET6_SETUP_RA_6 ...
- 8 个 Git 的小技巧
git 已经成为了我日常必备工具之一,我总结我几乎每天使用的8个有用(且简洁)的git技巧. 使用-p选择性添加 当你想提交内容时,你可以通过使用 git commit -am 来选择所有文件或使 ...
- linux 查看占用端口并kill掉
主要可以使用ps 命令 或 netstat 命令 weihong@data1:~/jd_parser/jd_parser$ ps aux | head USER PID %CPU %MEM VSZ ...