最近使用spring2.0声明式事物集成hibernate3.0,具体集成过程见下:
* 既然使用spring2.0管理hibernate事物,那么必须将hibernatesession和事务管理交给spring的IOC容器。使用AOP添加声明式事物,必须声明AOP的切入点、pointcut、切面等。具体配置如下:
<?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"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <tx:advice id="transactionManagerAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="allService" expression="execution(* com.wj.web.service.*.*(..))" /> <aop:advisor pointcut-ref="allService" advice-ref="transactionManagerAdvice" /> </aop:config> </beans>
注意: 事务传播特性中rollback-for="java.lang.Exception"表示在事务管理中遇到Exception及其子类异常时候回滚事务,默认需要RunTimeException或者其子类才回滚异常。另外,事务管理最好添加到com.wj.web.service业务层。不要添加到DAO层。
关于事务隔离级别和传播特性请关注我的小一篇博客
* 创建一个pojo类。
package com.wj.web.mode; import java.util.Date; public class User { private int id; private String username; private String password; private String email; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + "]"; } }
*创建DAO类:
package com.wj.web.dao; import org.hibernate.Session; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.wj.web.mode.User; import com.wj.web.util.HibernateUtil; public class UserDao extends HibernateDaoSupport{ public int save(User user) throws Exception{ int result = 0; result = (Integer)this.getHibernateTemplate().save(user); return result; } public int delete(User user) throws Exception{ int result = 0; this.getHibernateTemplate().delete(user); return result; } public int update(User user){ int result = 0; this.getHibernateTemplate().update(user); return result; } }
注意:该DAO继承了HibernateDaoSupport ,所有方法都使用HiberanteTemplate来完成数据的CURD操作。
* 创建service层:
package com.wj.web.service; import com.wj.web.dao.UserDao; import com.wj.web.mode.User; public class UserService { private UserDao dao ; public UserDao getDao() { return dao; } public void setDao(UserDao dao) { this.dao = dao; } public int saveService(User user) throws Exception{ dao.save(user); this.deleteService(new User()); return 0; } public int deleteService(User user) throws Exception{ user.setId(2); if(1 == 1) throw new Exception("error test"); return dao.delete(user); } }
*测试代码:
package com.wj.web.client; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.hibernate.transaction.JDBCTransaction; import com.wj.web.mode.User; import com.wj.web.service.UserService; public class client { public static void main(String[] args) throws Exception { BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userSer = (UserService)factory.getBean("userService"); User user = new User(); user.setUsername("zhangsan"); user.setPassword("3234"); user.setEmail("[email protected]"); userSer.saveService(user); // user.setPassword("lisi"); // userSer.saveService(user); // userSer.deleteService(user); } }
开发总结如下:
Spring配置声明式事务:
* 配置SessionFactory
* 配置事务管理器
* 事务的传播特性
* 那些类那些方法使用事务
编写业务逻辑方法
* 继承HibernateDaoSupport类,使用HibernateTemplate来持久化,HibernateTemplate是
Hibernate Session的轻量级封装
* 默认情况下运行期异常才会回滚(包括继承了RuntimeException子类),普通异常是不会滚的
* 编写业务逻辑方法时,最好将异常一直向上抛出,在表示层(struts)处理
* 关于事务边界的设置,通常设置到业务层,不要添加到Dao上