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的图表
XStream使用文档
WebService发布过程及常见问题
webpack实战入门进阶调优分享
weblogic调优参数及监控指标
weblogic节点管理
weblogic管理控制台概述
weblogic-部署和启动
WebLogic-Server-性能及调优-调优-Java-虚拟机
Java 虚拟机(Java virtual machine,简称 JVM)是一种虚拟“执行引擎”实例,可在微处理器上执行 Java 类文件中的字节码。调整 JVM 的方式会影响 Weblogic Server 和应用程序的性能。
Velocity是一个基于java的模板引擎(template engine)。它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象。
Velocity 用户手册是帮助页面设计者和内容提供者认识 Velocity 和其简单而功能强大的脚本语言――Velocity 模板语言(VTL)。在手册上的许多例子,都是用 Velocity 插入动态的内容到网页上,但是所有的 VLT 例子都能应用到其他的页面和模板中。
FlashFXP绿色版网盘下载,附激活教程 2561
FlashFxp百度网盘下载链接:https://pan.baidu.com/s/1MBQ5gkZY1TCFY8A7fnZCfQ。FlashFxp是功能强大的FTP工具
Adobe Fireworks CS6 Ansifa绿色精简版网盘下载 2361
firework可以制作精美或是可以闪瞎眼的gif,这在广告领域是需要常用的,还有firework制作下logo,一些原创的图片还是很便捷的,而且fireworks用法简单,配合dw在做网站这一块往往会发挥出很强大的效果。百度网盘下载链接:https://pan.baidu.com/s/1fzIZszfy8VX6VzQBM_bdZQ
navicat for mysql中文绿色版网盘下载 2200
Navicat for Mysql是用于Mysql数据库管理的一款图形化管理软件,非常的便捷和好用,可以方便的增删改查数据库、数据表、字段、支持mysql命令,视图等等。百度网盘下载链接:https://pan.baidu.com/s/1T_tlgxzdQLtDr9TzptoWQw 提取码:y2yq
火车头采集器(旗舰版)绿色版网盘下载 2362
火车头采集器是站长常用的工具,相比于八爪鱼,简洁好用,易于配置。火车头能够轻松的抓取网页内容,并通过自带的工具对内容进行处理。站长圈想要做网站,火车头采集器是必不可少的。百度网盘链接:https://pan.baidu.com/s/1u8wUqS901HgOmucMBBOvEA
Photoshop(CS-2015-2023)绿色中文版软件下载 2401
安装文件清单(共46G)包含Window和Mac OS各个版本的安装包,从cs到cc,从绿色版到破解版,从安装文件激活工具,应有尽有,一次性打包。 Photoshop CC绿色精简版 Photoshop CS6 Mac版 Photoshop CC 2015 32位 Photoshop CC 2015 64位 Photoshop CC 2015 MAC版 Photoshop CC 2017 64位 Adobe Photoshop CC 2018 Adobe_Photoshop_CC_2018 Photoshop CC 2018 Win32 Photoshop CC 2018 Win64