一、编程式事务简介

在 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的整合:编程式事务的更多相关文章

  1. 全面分析 Spring 的编程式事务管理及声明式事务管理

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  2. Spring学习8-Spring事务管理(编程式事务管理)

    一.Spring事务的相关知识   1.事务是指一系列独立的操作,但在概念上具有原子性. 比如转账:A账号-100, B账号+100,完成.这两个操作独立是没问题的. 但在逻辑上,要么全部完成,要么一 ...

  3. spring 编程式事务管理和声明式事务管理

    编程式事务管理 Spring 的编程式事务管理概述 在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择.用过 Hibernate 的人都知道,我们需要在代码中显式调用be ...

  4. 全面分析 Spring 的编程式事务管理及声明式事务管理--转

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  5. 开涛spring3(9.3) - Spring的事务 之 9.3 编程式事务

    9.3  编程式事务 9.3.1  编程式事务概述 所谓编程式事务指的是通过编码方式实现事务,即类似于JDBC编程实现事务管理. Spring框架提供一致的事务抽象,因此对于JDBC还是JTA事务都是 ...

  6. spring事务管理——编程式事务、声明式事务

    本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本教程假定您已经掌握了 ...

  7. 8.spring:事务管理(上):Spring的数据库编程、编程式事务管理

    Spring的数据库编程 Spring框架提供了JDBC模板模式------>JdbcTemplate 简化了开发,在开发中并不经常是使用 实际开发更多使用的是Hibernate和MyBatis ...

  8. Spring编程式事务管理及声明式事务管理

    本文将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. Spring 事务属性分析 事务管理 ...

  9. 【Spring】编程式事务和声明式事务

    一.概述 二.准备工作 1. 创建表 2. 创建项目并引入Maven依赖 3. 编写实体类 4. 编写Dao层 5. 业务层 6. XML中的配置 7. 测试 三.编程式事务 1. 在业务层代码上使用 ...

随机推荐

  1. file_get_content和curl的性能比较

    今天在获取微信一张二维码图片时发现使用php中的file_get_content方式和curl方式竟然相差了50倍左右,直接晕倒!!!

  2. 转--2014年最新810多套android源码2.46GB免费一次性打包下载

    转载自:http://www.eoeandroid.com/thread-497046-1-1.html 感谢该博客主人无私奉献~~ 下面的源码是从今年3月份开始不断整理源码区和其他网站上的安卓例子源 ...

  3. JDBC常用代码

    try { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://127.0.0 ...

  4. ubuntu 命令学习大全

    http://wiki.ubuntu.org.cn/UbuntuSkills#.E6.98.BE.E7.A4.BA.E5.BD.93.E5.89.8D.E7.A1.AC.E4.BB.B6.E4.BF. ...

  5. CentOS7 备忘录

    //安装CentOS7 1.用LinuxLive USB Creator,LiveUSB Creator设置U盘安装不成功,UltraISO可以. 2.旧电脑(x8le)安装时报"/dev/ ...

  6. C++学习39 异常处理入门(try和catch)

    编译器能够保证代码的语法是正确的,但是对逻辑错误和运行时错误却无能为力,例如除数为 0.内存分配失败.数组越界等.这些错误如果放任不管,系统就会执行默认的操作,终止程序运行,也就是我们常说的程序崩溃( ...

  7. [Flex] IFrame系列 —— IFrame嵌入html后Alert弹出窗口被IFrame遮挡问题

    <?xml version="1.0" encoding="utf-8"?> <!--- - - - - - - - - - - - - - ...

  8. [ActionScript 3.0] AS3.0 调试出现安全沙箱冲突错误解决办法

    提示 *** 安全沙箱冲突 ***到 http://api.map.baidu.com/telematics/v3/weather?location=%E6%88%90%E9%83%BD&ou ...

  9. dropdownlist 控件的判断

    问题: 三个级联 下拉框.在不点击第一级别直接点击第二级别时,弹出提示窗口."请先选择XXX"之类的,理想的结果是想要下图 这样的: 由于使用的微信公众号的内置浏览器,所以问题来了 ...

  10. Contains DuplicateII

    超时版: /*Contains Duplicate II Given an array of integers and an integer k, find out whether there the ...