Spring3配置声明式事务

c#小王子 c#小王子 2022-04-02 392 Java

1. 配置 Spring3 声明式事务

在 Spring3 中配置声明式事务比早期版本显得更加简便。只需要几行配置文件+注解就可

以实现面向切面的 AOP 事务

2. 配置文件

在 Spring 的配置如下

<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/bean
s
http://www.springframework.org/schema/beans/spring-beans-3.0.x
sd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3
.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-autowire="byName">
<!-- 配置Spring上下文的注解 -->
<context:annotation-config />
<!-- 配置DAO类 -->
<bean id="persondao" class="impl.PersonDAOImpl"> 
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 事务管理配置 -->
<!-- 配置事务管理开始 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransaction
Manager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />

在配置文件中并没有出现像以前版本的事务传播和隔离级别,也就是类似如下配置

<!‐‐ 配置事务的传播特性 ‐‐>
<tx:advice id="txAdvice" transaction‐manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="*" read‐only="true"/>
</tx:attributes>
</tx:advice>
因为可以通过注解@Transactional 完成事务传播、事务隔离的功能。
3. DAO 代码
DAO 实现类代码如下
package impl;
import
org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
import pojo.Test;
@Transactional
public class PersonDAOImpl extends HibernateDaoSupport implements
PersonDAO {
public void save(Test test) {
this.getHibernateTemplate().saveOrUpdate(test);
Test test2 = new Test();
test2.setId(3);
test2.setName("3");
this.getHibernateTemplate().saveOrUpdate(test2);
throw new RuntimeException("主键重复");
} 
}

使用注解将此 DAO 实现类的所有方法进行了事务拦截,当遇到运行是异常时,事务回滚。

不提交到数据库。

当然可以指定单一原子方法进行隔离,方法可以指定

@Transactional(propagation=Propagation.NOT_SUPPORTED)
@Transactional(propagation=Propagation.MANDATORY)
@Transactional(propagation=Propagation.NESTED)
@Transactional(propagation=Propagation.REQUIRED)
@Transactional(propagation=Propagation.REQUIRES_NEW)
@Transactional(propagation=Propagation.SUPPORTS)


同 JTA 标准的事务隔离传播一样,具体参考

http://suhuanzheng7784877.iteye.com/blog/908382

之后在 Service 层可以进行原子操作的业务整合。

4.

以前是如何在 Spring 中配置事务的

正好也总结一下原来的 AOP 事务

在 Spring2.X 中,仅仅使用 Hibernate 作为持久层方案的配置如下

<?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"
default‐autowire="byName">
<!‐‐连接池‐‐>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy‐method="close">
<property name="driverClass" value="org.gjt.mm.mysql.Driver">
</property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/ssh">
</property>
<property name="user" value="root">
</property>
<property name="password" value="">
</property>
<property name="maxPoolSize" value="20">
</property>
<property name="minPoolSize" value="1">
</property>
<property name="initialPoolSize" value="1">
</property>
<property name="maxIdleTime" value="20">
</property>
</bean>
<!‐‐Hibernate 的 sessionFactory‐‐>
<bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"
ref="dataSource">
</property>
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!‐‐事务处理‐‐>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction‐manager="transactionManager">
<tx:attributes>
<tx:method name="addBbs" rollback‐for="Exception"
read‐only="false" />
<tx:method name="addUser" rollback‐for="Exception"
read‐only="false" />
<tx:method name="addBbsType" rollback‐for="Exception"
read‐only="false" />
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="leeService"
expression="execution(* dao.*.*(..))"/>
<aop:advisor advice‐ref="txAdvice"
pointcut‐ref="leeService"/>
</aop:config>
</beans>
在 DAO 实现类中依然使用 Spring 提供的 HibernateDaoSupport .getHibernateTemplate()作
为操作的模板。例如
package dao;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.List;
import java.util.Set;
import org.hibernate.Query;
4 / 6import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import pojo.Bbs;
import pojo.Rebbs;
public class BbsDaoImpl extends HibernateDaoSupport implements BbsDao {
public String addBbs(Bbs bbs) {
Serializable id = this.getHibernateTemplate().save(bbs);
String idStr = id.toString();
return idStr;
}
public void deleteBbsById(String bbsId) {
Bbs bbs = this.loadBbsByBbsId(bbsId);
this.getHibernateTemplate().delete(bbs);
}
@SuppressWarnings("unchecked")
public List<Bbs> getAllBbs() {
String hql = "FROM Bbs";
List<Bbs> list = null;
//Query query =
this.getHibernateTemplate().getSessionFactory().openSession().createQuery(hql);
list = super.getSession().createQuery(hql).list();
return list;
}
@SuppressWarnings("unchecked")
public int getNumReBbsByBbs() {
String sql = "select count(id) from bbs";
Query query =
this.getHibernateTemplate().getSessionFactory().openSession().createSQLQuery(sql);
List<Object> list = query.list();
Object o = list.get(0);
BigInteger numObject = (BigInteger)o;
int num = numObject.intValue();
return num;
}
public Set<Rebbs> getReBbsByBbsId(String bbsId) {
return this.loadBbsByBbsId(bbsId).getReBbs();
}
public Bbs loadBbsByBbsId(String BbsId) {
Bbs bbs = null;
bbs = (Bbs)this.getHibernateTemplate().get(Bbs.class, BbsId);
//bbs = (Bbs)this.getHibernateTemplate().load(Bbs.class, BbsId);
return bbs;
}
public void updateBbsById(Bbs bbs) {
this.getHibernateTemplate().update(bbs);
} 
}

【下载地址】

百度网盘链接:https://pan.baidu.com/s/1VibeXgnp9VsmB7xq2H-WiQ

提取码:4h8y


相关文章


使用-JFreeChart来创建基于web的图表

使用-JFreeChart来创建基于web的图表

XStream使用文档

XStream使用文档

WebService发布过程及常见问题

WebService发布过程及常见问题

webpack实战入门进阶调优分享

webpack实战入门进阶调优分享

weblogic调优参数及监控指标

weblogic调优参数及监控指标

weblogic节点管理

weblogic节点管理

weblogic管理控制台概述

weblogic管理控制台概述

weblogic-部署和启动

weblogic-部署和启动

WebLogic-Server-性能及调优-调优-Java-虚拟机

Java 虚拟机(Java virtual machine,简称 JVM)是一种虚拟“执行引擎”实例,可在微处理器上执行 Java 类文件中的字节码。调整 JVM 的方式会影响 Weblogic Server 和应用程序的性能。

Velocity用户教程

Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。

Velocity用户手册

Velocity 用户手册是帮助页面设计者和内容提供者认识 Velocity 和其简单而功能强大的脚本语言――Velocity 模板语言(VTL)。在手册上的许多例子,都是用 Velocity 插入动态的内容到网页上,但是所有的 VLT 例子都能应用到其他的页面和模板中。


文章热度: 166291
文章数量: 333
推荐阅读

CAD快捷键有哪些?(常用的CAD快捷键命令大全) 64

一、常用绘图快捷键。最基本的一些画图的功能操作,简单来说就是CAD制图的打底部分。(如下图)二、常用编辑快捷键CAD中对图形进行修改的操作。

chatgpt是谁开发的? 108

OpenAI官网显示,为ChatGPT项目做出贡献的人员不足百人(共87人)。从成员毕业高校分布看,校友最多的前5大高校是斯坦福大学(14人)、加州大学伯克利分校(10人)、麻省理工学院(7人)、剑桥大学(5人)、哈佛大学(4人)和佐治亚理工学院(4人)。

怎么注册chatgpt(chatgpt怎么使用) 241

第一步 上网工具。打开上网工具,工具基本是需要付费使用的。注册然后按照教程安装,直到能够测试上网打开即可。

win10版本区别有哪些?怎么选择?哪个更好用? 69

win10系统分为7个版本。家庭版(Windows 10 Home);专业版(Windows 10 Professional);企业版(Windows 10 Enterprise);教育版(Windows 10 Education);移动版(Windows 10 Mobile);移动企业版(Windows 10 Mobile Enterprise);物联网核心版(Windows 10 loT Core)

win10自动更新怎么关闭,教你三步即可搞定 53

方法一:组策略关闭 1、同时按下“Win+R”打开运行窗口,然后输入gpedit.msc回车打开【本地组策略编辑器】。2、在组策略编辑里面,我们依次展开【计算机配置 - 管理模板 - Windows 组件 - Windows 更新】,然后在右侧找到【配置自动更新】选项选中。

知之

知之平台是全球领先的知识付费平台。提供各个领域的项目实战经验分享,提供优质的行业解决方案信息,来帮助您的工作和学习

使用指南 建议意见 用户协议 友情链接 隐私政策 Powered by NOOU ©2020 知之