FROM:http://hua3392.blog.163.com/blog/static/8630511920091013112859610/
Spring 事务配置例:
<!-- hibernate事务管理 器-->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- springmvc事务管理 器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 声明式事务管理 -->
<!-- 事务拦截器 -->
<bean id="myTransactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="myTransactionManager"/>
</property>
<property name="transactionAttributes">
<!-- 下面定义事务传播属性-->
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- BeanNameAutoProxyCreator 自动生成事务代理-->
<bean id="autoProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<value>myTransactionInterceptor</value>
</list>
</property>
<property name="beanNames">
<list>
<value>ProductService</value>
<value>PersonService</value>
<!-- 在此添加Bean -->
</list>
</property>
</bean>
<!-- 应用BEAN配置 -->
<bean id="PersonDAO" class="com.myweb.object.person.PersonDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="myPersonServiceTarget" class="com.myweb.object.person.PersonServiceImpl">
<property name="dao">
<ref bean="PersonDAO"/>
</property>
</bean>
<bean id="PersonService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.myweb.object.person.PersonService</value>
</property>
<property name="interceptorNames">
<list>
<value>myHibernateInterceptor</value>
<value>myPersonServiceTarget</value>
</list>
</property>
</bean>
<bean id="ProductDAO" class="com.myweb.object.product.ProductDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="ProductService" class="com.myweb.object.product.ProductService">
<property name="dao">
<ref bean="ProductDAO"/>
</property>
</bean>
在ProductService和PersonService的update*等方法中,不能对RuntimeException异常进行处理,否则事物不会回滚.
public void updateName()
{
for(int i=0;i<10;i++)
{
PersonId personid = new PersonId();
personid.setId(27);
personid.setAge(i+10+"");
personid.setName("test"+i);
Person person = new Person();
person.setId(personid);
dao.save(person);
int j = 1/0; //不能对该异常用try...catch处理,否则不会回滚事务
}
}
public static void main(String[] args)
{
try
{
ApplicationContext ctx = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/classes/applicationContext.xml");
PersonService per = (PersonService)ctx.getBean("PersonService");
ProductService pro = (ProductService)ctx.getBean("ProductService");
per.updateName();
//pro.updateName();
}catch(Exception e)
{
e.printStackTrace();
}
}