private UserDAO userDAO; public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } /** * 异常只有抛出事物才有回滚机制 */ public void saveUser(User user) throws Exception{ userDAO.save(user); }
public void save(User transientInstance) throws Exception { log.debug("saving User instance"); try { getHibernateTemplate().save(transientInstance); int i = 5/0; log.debug("save successful"); } catch (Exception re) { re.printStackTrace(); log.error("save failed", re); throw new Exception(); } }
<?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:p="http://www.springframework.org/schema/p" 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-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property> <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=plusoft_test"></property> <property name="username" value="sa"></property> <property name="password" value="admin"></property> <property name="defaultAutoCommit" value="false"/> <property name="maxActive" value="150" /> <property name="maxIdle" value="100" /> <property name="maxWait" value="60000" /> <property name="minIdle" value="5" /> <property name="testOnBorrow" value="true"/> <property name="testWhileIdle" value="true"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect </prop> </props> </property> <property name="mappingResources"> <list> <value>com/ssh2/bean/User.hbm.xml</value> </list> </property> </bean> <bean id="hibernateDaoTemplate" abstract="true"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <!-- Dao 定义 --> <bean id="userDAO" class="com.ssh2.dao.UserDAO" parent="hibernateDaoTemplate" ></bean> <!-- Service 定义 --> <bean id="userService" class="com.ssh2.service.impl.UserServiceImpl" autowire="byName"> <property name="userDAO" ref="userDAO"></property> </bean> <bean id="userAction" class="com.ssh2.action.UserAction"> <property name="userService" ref="userService"></property> </bean> <!-- Hibernate Transaction Interceptor Definition --> <bean id="hibernateTransactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor" parent="transactionIntercetorTemplate"> <property name="transactionManager"> <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> </property> </bean> <!-- transction Intercetor's Template --> <bean id="transactionIntercetorTemplate" abstract="true"> <property name="transactionAttributes"> <props> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="is*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="check*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <!-- 事物回滚 --> <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- 定义自动代理生成器--> <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="interceptorNames"> <list> <idref bean="hibernateTransactionInterceptor" /> </list> </property> <property name="beanNames"> <value>*Service</value> </property> </bean> </beans>