日期:2014-05-19 浏览次数:20676 次
<!-- 声明事务 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="pointcut" expression="execution(* com.service.impl.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" /> </aop:config>
package com.service.impl; public class UserServiceImpl implements UserService { private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void saveUser(USM_USER User) { this.userDao.saveObject(User); //做2次插入,按道理第二次会失败,那应该要回滚第一次的插入吧 this.userDao.saveObject(User); } }
public Serializable saveObject(E entity) { Serializable id = null; try { id = getHibernateTemplate().save(entity); } catch (Exception e) { logger.error("保存实体对象异常,对象是" + entity, e); return null; } return id; } public boolean saveOrUpdateObject(E entity) { try { getHibernateTemplate().saveOrUpdate(entity); return true; } catch (Exception e) { logger.error("保存或者更新实体对象异常,对象是" + entity, e); return false; } }