这里是利用 cglib 的代理功能实现业务类的统一事务管理,以达到在业务类的每个方法上自动开启与关闭事务,而免区在每个业务方法里写事务代码的烦恼。且业务类既可以实现接口使代理返回接口,或不实现接口使代理返回对象。
在这里用到了 Hibernate 的 getCurrentSession() ,因此需要在 hibernate.cfg.xml 里加上下面这句,仅示例:
<property name="hibernate.current_session_context_class">thread</property>
实体 Student 定义如下:
public class Student {
private Integer id;
private String name;
private String phone;
// 。。。。
}
hibernate.cfg.xml 如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/tthibernate?useUnicode=true&characterEncoding=utf8
</property>
<property name="connection.username">root</property>
<property name="connection.password">sa</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="test1/vo/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
获取 hibernate factory 的工具类:
public class HibernateUtil {
private static SessionFactory sessionFactory;
static{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
if(sessionFactory==null){
Configuration conf = new Configuration().configure();
sessionFactory = conf.buildSessionFactory();
}
return sessionFactory;
}
}
动态添加事务的拦截器:
public class TransctionInterceptor implements MethodInterceptor {
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy mp) throws Throwable {
// TODO Auto-generated method stub
SessionFactory sf = HibernateUtil.getSessionFactory();
Session ss = sf.getCurrentSession();
Transaction ts = ss.beginTransaction();
Object rObj = null;
try{
rObj = mp.invokeSuper(obj, args);
ts.commit();
}
catch(Exception e){
e.printStackTrace();
ts.rollback();
}
return rObj;
}
}
实现业务类代理的类:
public class HibernateBusiness {
// 返回需要业务代理对象
public static Object getBusiness(Class clazz){
Enhancer eh = new Enhancer();
eh.setSuperclass(clazz);
eh.setCallback(new TransctionInterceptor());
return eh.create();
}
}
数据库操作层:
public class StudentDao {
private SessionFactory sf;
public StudentDao(){
sf = HibernateUtil.getSessionFactory();
}
public void insertRightStudent()throws Exception{
Student stu = new Student();
stu.setName("b");
stu.setPhone("22");
try {
Session ss = sf.getCurrentSession();
ss.save(stu);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new Exception("exception 1");
}
}
public void insertErrorStudent()throws Exception{
Student stu = new Student();
stu.setName("a");
stu.setPhone("11");
Session ss = sf.getCurrentSession();
try {
// 前提数据库中不存在 id 为 99 的学生,这样下面语句会报错,从而达到事务回滚
Student stus = (Student)ss.load(Student.class, new Integer(99));
stus.getName();
// 数据为插入
ss.save(stu);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new Exception("exception 2");
}
}
}
业务接口与实现,在这里也可以不实现该接口,以避免每个业务类都需要继承接口:
public interface StudentBusinessIf {
public void testRightStudent()throws Exception;
public void testErrorStudent()throws Exception;
}
public class StudentBusiness implements StudentBusinessIf{
private StudentDao studao;
public StudentBusiness(){
studao = new StudentDao();
}
public void testRightStudent()throws Exception{
// 将成功插入数据
studao.insertRightStudent();
}
public void testErrorStudent()throws Exception{
// 插入数据将失败
studao.insertErrorStudent();
}
}
最后是测试类,实际中可为action 等:
public class StudentAction {
public static void main(String[] args){
// TODO Auto-generated method stub
/*
* 下面两种代理均可
*/
// 得到业务代理对象,接口代理,
StudentBusinessIf sb = (StudentBusinessIf)HibernateBusiness.getBusiness(StudentBusiness.class);
// 得到业务代理对象, 实际对象代理
//StudentBusiness sb = (StudentBusiness)HibernateBusiness.getBusiness(StudentBusiness.class);
try {
// 正确插入数据测试
sb.testRightStudent();
// 错误插入数据测试
sb.testErrorStudent();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这样就完成了统一事务管理的整个过程,所以如果想让自己的业务类或其他类的方法具有事务自动开启与关闭功能,只需要象下面这样返回代理即可:
// 得到业务代理对象,接口代理, 用于实现接口的业务类等
StudentBusinessIf sb = (StudentBusinessIf)HibernateBusiness.getBusiness(StudentBusiness.class);
// 或是下面这样
// 得到业务代理对象, 实际对象代理,用于未实现接口的业务类等
StudentBusiness sb = (StudentBusiness)HibernateBusiness.getBusiness(StudentBusiness.class);
分享到:
相关推荐
为了更好地与持久层框架整合,Spring还提供了统一的异常处理体系和事务管理方法。 24.1 概述 24.1.1 概述 如果Spring与Hibernate进行了整合,则Hibernate便处于被Spring管理的状态下,Hibernate所需的基础资源,都由...
Spring(IOC DI AOP 声明式事务处理) SpringMVC(支持Restful风格) Hibernate Validator(参数校验) Mybatis(最少配置方案) shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的权限动态...
14.2.5管理hibernate事务 282 14.3项目实战——学生成绩查询系统 283 本章小结 292 课后练习 293 第15章图书馆管理系统 294 15.1系统功能解析 294 15.2系统数据库设计 295 15.2.1数据库分析 295 15.2.2数据...
- Spring(IOC DI AOP 声明式事务处理) - SpringMVC(支持Restful风格) - Hibernate Validator(参数校验) - Mybatis(最少配置方案) - shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的...
Spring 承担的角色是对应用中各层次的接口和类采用 IOC 模式进行统一管理配置,同时,还采用 AOP 和 Spring 对事务的处理机制来管理事务,并根据 Spring 与 Hibernate 的集成,调用 Spring 提供的 HibernateTemplate...
Spring(IOC DI AOP 声明式事务处理) SpringMVC(支持Restful风格) Hibernate Validator(参数校验) Mybatis(最少配置方案) shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的权限动态...
声明式事务处理) SpringMVC(支持Restful风格) Hibernate Validator(参数校验) Mybatis(最少配置方案) shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的权限动态分配(到按钮级别);添加...
一个最简单的SSH框架Struts 、 spring 、 Hibernate 在各层的作用 ...统一的 javabean 管理方法 4 .声明式事务管理 5. 集成 Hiberante 3 ) Hiberante ,负责持久化层,完成数据库的 crud 操作
本文总结了 Java 框架笔试题中的重要知识点,涵盖了依赖注入、AOP、Bean 的注入、ApplicationContext 接口、事务管理、持久化支持、Spring 配置文件、自动装配等方面的知识。 一、依赖注入 依赖注入是一种设计模式...
Spring(IOC DI AOP 声明式事务处理) SpringMVC(支持Restful风格) Hibernate Validator(参数校验) Mybatis(最少配置方案) shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的...
声明式事务处理) SpringMVC(支持Restful风格) Hibernate Validator(参数校验) Mybatis(最少配置方案) shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的权限动态分配(到按钮级别);添加...
声明式事务处理) SpringMVC(支持Restful风格) Hibernate Validator(参数校验) Mybatis(最少配置方案) shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的权限动态分配(到按钮级别);添加...
10.5.2 Hibernate+Spring JDBC混合框架的事务管理 10.6 特殊方法成漏网之鱼 10.6.1 哪些方法不能实施Spring AOP事务 10.6.2 事务增强遗漏实例 10.7 数据连接泄漏 10.7.1 底层连接资源的访问问题 10.7.2 Spring JDBC...
10.5.2 Hibernate+Spring JDBC混合框架的事务管理 10.6 特殊方法成漏网之鱼 10.6.1 哪些方法不能实施Spring AOP事务 10.6.2 事务增强遗漏实例 10.7 数据连接泄漏 10.7.1 底层连接资源的访问问题 10.7.2 Spring JDBC...
6. 事务管理:Spring 框架提供了一种持续的事务管理接口,可以扩展到上至当地事务下至全局事务(JTA)。 7. 异常处理:Spring 框架提供了一种便捷的 API 把详细技术有关的异常(例如由 JDBC、Hibernate 或 JDO 抛出...
声明式事务处理) SpringMVC(支持Restful风格) Hibernate Validator(参数校验) Mybatis(最少配置方案) shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的权限动态分配(到按钮级别);添加...
声明式事务处理) SpringMVC(支持Restful风格) Hibernate Validator(参数校验) Mybatis(最少配置方案) shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的权限动态分配(到按钮级别);添加...
声明式事务处理) SpringMVC(支持Restful风格) Hibernate Validator(参数校验) Mybatis(最少配置方案) shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的权限动态分配(到按钮级别);添加...
声明式事务处理) SpringMVC(支持Restful风格) Hibernate Validator(参数校验) Mybatis(最少配置方案) shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的权限动态分配(到按钮级别);添加...
声明式事务处理) SpringMVC(支持Restful风格) Hibernate Validator(参数校验) Mybatis(最少配置方案) shiro权限控制,结合ajax实现了异步认证与异步授权,同时实现了细粒度的权限动态分配(到按钮级别);添加...