新手学习spring----spring事物管理配置方法及会出现的问题

来源:互联网 发布:mac充电器坏了怎么办 编辑:程序博客网 时间:2024/06/01 16:16

spring事物管理共有五种配置方式,在此不做详细解释,仅介绍其中的两种。(有兴趣的可以参考http://blog.csdn.net/it_man/article/details/5074371)

引用:

Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource、TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分。

    DataSource、TransactionManager这两部分只是会根据数据访问方式有所变化,比如使用Hibernate进行数据访问时,DataSource实际为SessionFactory,TransactionManager的实现为HibernateTransactionManager。


原创:

1、所有Bean共享一个代理基类

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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

  <!-- JDBC Data Source  注册数据源,常见的有JDBC或JNDI-->
   
              <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
       
              <property name="driverClassName"  value="oracle.jdbc.driver.OracleDriver" />
       
            <property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL" />
       
            <property name="username"  value="sys as SYSDBA" />
       
            <property name="password"  value="123" />
   
             </bean>

<!-- 配置事务管理器,若已经配置datasource(即数据库配置) ,则将上面的配置删除, -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource" />
</bean>

<!-- 配置基类事务的传播特性    事物的传播特性共有7种,最常用的是PROPAGATION_REQUIRED,有兴趣的可以考http://blog.csdn.net/it_man/article/details/5074371-->
         <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
   <property name="transactionManager" ref="transactionManager" />
   <property name="transactionAttributes">
       <props>
           <prop key="add*">PROPAGATION_REQUIRED</prop>
           <prop key="edit*">PROPAGATION_REQUIRED</prop>
           <prop key="remove*">PROPAGATION_REQUIRED</prop>
           <prop key="insert*">PROPAGATION_REQUIRED</prop>
           <prop key="update*">PROPAGATION_REQUIRED</prop>
           <prop key="save*">PROPAGATION_REQUIRED</prop>
           <prop key="find*">PROPAGATION_REQUIRED</prop>
           <prop key="del*">PROPAGATION_REQUIRED</prop>
           <prop key="*">readOnly</prop>
       </props>
   </property>
</bean>

  <!-- 为具体的类配置事物拦截      为cxVOService接口配置事务拦截器,baseTransactionProxy是事务拦截器 -->
         <bean id="cxVOService" parent="baseTransactionProxy">
   <!-- 设置target,也就是cxVOService的实现类  也就是要进行事物管理的类-->
   <property name="target" ref="cxvoServiceImpl"/>
</bean>
</beans>看了很多文章到此处就结束了,读者copy代码进行修改未必有用,原因在于:

(1)、<property name="target" ref="cxvoServiceImpl"/>  此处引用cxvoServiceImpl,那么声明cxvoServiceImpl,代码如下:

<bean id="cxvoServiceImpl" class="com.foresee.mybatis3spring3intg.service.impl.CxVOServiceImpl"></bean>

(2)、spring怎么知道我为CxVOServiceImpl类配置了事物管理,尽管tomcat会加载了配置的文件,其中一种方式就是用bin加载cxVOService(<bean id="cxVOService" parent="baseTransactionProxy">)代码如下:

CxVOService Service = (CxVOService)ContextInit.getContext().getBean("cxVOService");//CxVOServiceCxVOServiceImpl的接口

(3)、xml配置及java文件都没问题,为什么事物管理还是不起作用?此处需要说明的是,事物管理的方法内不能进行异常捕捉(try{}catch(){}),否则spring不知道有异常,那么就不会回滚。示例代码:

CxVOServiceImpl.java文件

    @Override
   
    public void save(CxVO vo) {
            mapper.saveTid(vo);
           
 mapper.save(vo);//若该方法出错,则saveTid()操作的数据也将回滚

   }


CxVOMapperImpl.java文件

@Override
public void save(CxVO vo) {
// TODO Auto-generated method stub
int result = 0;
String mes = "";

result = super.getSqlSession().update(CxVO.class.getName()+".updateSfysb", vo);

//或者这么写
/*try{
result = super.getSqlSession().update(CxVO.class.getName()+".updateSfysb", vo);
}catch(Exception e){
if(result == 0)
throw new RuntimeException(e);
}*/

//不可以这么写
/*try{
result = super.getSqlSession().update(CxVO.class.getName()+".updateSfysb", vo);
}catch(Exception e){
e.printStackTrace();
}*/


mes = (result==1)?"更新sfysb成功":"更新sfysb失败";
System.out.println(mes);
}


2、使用tx标签配置的拦截器

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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">


  <!-- JDBC Data Source  注册数据源,常见的有JDBC或JNDI-->
   
              <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
       
              <property name="driverClassName"  value="oracle.jdbc.driver.OracleDriver" />
       
            <property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL" />
       
            <property name="username"  value="sys as SYSDBA" />
       
            <property name="password"  value="123" />
   
             </bean>

<!-- 配置事务管理器,若已经配置datasource(即数据库配置) ,则将上面的配置删除, -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource" />
</bean>

 <bean id="TestAop" class="com.spring.aop.TestAop"/>(此处代码及下面用到此id的都可省略,自己可以研究下其作用)
   
      <!-- 配置事务的传播特性 -->
      <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="Read*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

 <aop:config proxy-target-class="true">
  <aop:pointcut id="allManagerMethod" expression="execution(* com.foresee.mybatis3spring3intg..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
   <aop:aspect id="myAop" ref="TestAop">
     <aop:before method="before" pointcut-ref="allManagerMethod"/>
     <aop:after method="after" pointcut-ref="allManagerMethod"/>
   </aop:aspect>
 </aop:config>
</bean>注意事项请参考上面的内容。





0 0