Spring2.x事务管理--@Transactional

前面在Hibernate中曾介绍了使用spring1.x的声明式事务管理([url]http://ch-space.iteye.com/blog/380419[/url]),还是感觉xml文件的配置比较繁琐,这里介绍一下spring2.x基于注解的声明式事务。
这里给出一个完整的例子:对Users类的一个增加操作,测试异常抛出时回滚事务。
所用的框架:Spring2.0+Hibernate3.1
Dao层:

//Users类操作的接口
public interface UserDao {
public void addUser(Users u);
}
//Dao接口对应的实现类
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{

public void addUser(Users u) {
super.getHibernateTemplate().save(u);
}
}

Service层:

//业务接口
public interface UserService {
public void addUser(Users u);
}

//业务实现

@Transactional//对声明式事务进行注解
public class UserServiceImpl implements UserService {
//注入Dao接口
private UserDao uDao;
public void setUDao(UserDao dao) {
uDao = dao;
}
public void addUser(Users u) {
this.uDao.addUser(u);
throw new RuntimeException("exception occurred");//抛出异常,测试是否回滚
}
}

applicationContext.xml配置文件

<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
...
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="hibernateDaoSupport"
class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"
abstract="true">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--开启事务-->
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="UserService" class="com.spring.service.UserServiceImpl">
<property name="UDao">
<bean class="com.spring.dao.UserDaoImpl"
parent="hibernateDaoSupport" />
</property>
</bean>

</beans>

注意这里需要引入<tx:>命名空间xmlns:tx="http://www.springframework.org/schema/tx"
测试类:

public class Main {
public static void main(String[] args) {
ApplicationContext a=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us=(UserService)a.getBean("UserService");
Users u=new Users();
u.setName("spring-hinernate");
us.addUser(u);
}


运行结果:

Hibernate: insert into test.chen.users (name) values (?) select scope_identity()
Exception in thread "main" java.lang.RuntimeException: exception occurred
at com.spring.service.UserServiceImpl.addUser(UserServiceImpl.java:16)
...
at $Proxy6.addUser(Unknown Source)
at Main.main(Main.java:18)


查看数据库,事务异常时自动回滚了。
关于更深入的事务传播属性注解(只读,新事务,挂起等),可参考Spring Reference,这里不再详细介绍。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值