需要事务处理的方法:
该方法所在类不需要实现任何接口!!
public class aservers{
@Autowired
private DeptDAO deptopt;//spring 来完成deptopt的初始化 不需要写getter setter
public void insertDemoData() throws Exception{//一定不要自己catch抛出异常,否则不能回滚
Dept dept1=new Dept();
dept1.setName("事业部");
deptopt.save(dept1);
int i=1/0; //异常
}
}
spring 配置文件:
<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.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 定义切面 -->
<aop:config>
<aop:pointcut id="serviceOperation"
<!-- 模糊匹配需要拦截的包 -->
expression="execution(* com.casw.ssh.dept.actions.*.*(..))"/>
<aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>
</aop:config>
<!-- 定义需要管理的方法 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<!-- 模糊匹配需要拦截的方法 -->
<tx:method name="find*" read-only="true" />
<tx:method name="insert*" />
</tx:attributes>
</tx:advice>
<!-- 定义事务管理器,使用适用于Hibernte的事务管理器-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- HibernateTransactionManager bean需要依赖注入一个SessionFactory bean的引用-->
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!--其他配置略-->
OK 配置完毕, 出异常的话就会回滚,注释掉 int i=1/0, 就会更新数据库。
其实这样配置的最大好处就是aservers不需要 实现什么接口了,
分享到:
相关推荐
struts2.0 spring2.5 hibernate3.2 注解最新框架
这是一个struts 1.2 + spring 2.5 + hibernate 3.2框架demo, 运行环境为eclipse 3.2 + tomcat 5.5 + oracle 11g
Spring2.5+hibernate搞定分布式事务Spring2.5+hibernate搞定分Spring2.5+hibernate搞定分布式事务布式事务 第三部分
Spring2.5.chm Hibernate_3.2.0.chm Spring2.5和Hibernate3.2中文手册
spring2.5 + hibernate3.3 声明式事务 和所需jar包,spring2.5 + hibernate3.3 声明式事务 和所需jar包,spring2.5 + hibernate3.3 声明式事务 和所需jar包,
NULL 博文链接:https://ilyon.iteye.com/blog/725910
struts2+spring2.5+hibernate3.2 找了半天没看到。自己整了一个。
这是我在网上找的spring2.5 hibernate api,希望对大家有用,分有点高大家不要见怪.
spring2.5 + hibernate3.2x 标注(annotation)开发的简单示例 http://blog.csdn.net/IamHades/archive/2008/01/11/2038188.aspx
spring2.5+hibernate基于xml配置的实例
Struts2.0+spring2.5+hibernate3.2
struts2+spring2.5+hibernate3.2整合完整项目,带数据库脚本 ,mysql,tomcat,myeclipse完整工程
spring 2.5 hibernate struts2 整合例子
spring2.5+strust1+hibernate3.0JAR包集合
Struts2 Spring2.5 Hibernate3详细介绍,不容错过!!!
最新Struts2.1+Spring2.5+Hibernate3.3整合开发,很好用,配合注解开发
struts2 spring2.5 hibernate3.0 annotation 整合
struts2 spring 2.5 hibernate3整合 CRUD实例含分页 整合版本struts2 spring 2.5 hibernate3 简单的实现了一个用户的CRUD操作(列表展示有分页版本) 设计到了Validate效验,excel生成下载
经常有人在集成ssh2时,会出现jar包冲突而出现一些奇怪的问题,这里是struts2.0_spring2.5_hibernate3.2组合的jar集合,下载后放在lib目录中即可
这是一个spring2.5+hibernate3.1+struts1.2的搭建案例,主要是一个转帐的案例,通过spring添加事务