前面在Hibernate中曾介绍了使用spring1.x的声明式事务管理([url]http://ch-space.iteye.com/blog/380419[/url]),还是感觉xml文件的配置比较繁琐,这里介绍一下spring2.x基于注解的声明式事务。
这里给出一个完整的例子:对Users类的一个增加操作,测试异常抛出时回滚事务。
所用的框架:Spring2.0+Hibernate3.1
Dao层:
Service层:
applicationContext.xml配置文件
注意这里需要引入<tx:>命名空间xmlns:tx="http://www.springframework.org/schema/tx"
测试类:
运行结果:
查看数据库,事务异常时自动回滚了。
关于更深入的事务传播属性注解(只读,新事务,挂起等),可参考Spring Reference,这里不再详细介绍。
这里给出一个完整的例子:对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,这里不再详细介绍。
本文介绍Spring 2.x中基于注解的声明式事务管理,并提供了一个完整的示例,展示如何通过注解配置事务并在异常情况下实现事务回滚。
2373

被折叠的 条评论
为什么被折叠?



