Spring事务配置笔记(实现不同Service间调用事务)

长平狐 发布于 2012/10/17 16:19
阅读 1K+
收藏 0
作者:fbysss
msn:jameslastchina@hotmail.com 
blog:blog.csdn.net/fbysss
声明:本文由fbysss原创,转载请注明出处
 
关键字:Spring,事务处理

一、关键配置示例:
< bean  id ="sessionFactory"
        class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
        
< property  name ="dataSource" >
            
< ref  local ="dataSource"   />
        
</ property >
    
        
<!--  bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/ -->
    
< bean  id ="transactionManager"  class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
        
< property  name ="sessionFactory" >
            
< ref  local ="sessionFactory"   />
        
</ 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,-BussException </ 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"   />
                
< idref  bean ="jdbcTransactionInterceptor"   />
            
</ list >
        
</ property >
        
< property  name ="beanNames" >
            
< value > *Service </ value >  
            
<!-- list>
                <idref bean="bookService" />                                        
            </list
-->
            
<!-- 添加Service bean -->
        
</ property >
    
</ bean >
    
< bean  id ="hibernateDaoTemplate"  abstract ="true" >
        
< property  name ="sessionFactory" >
            
< ref  bean ="sessionFactory"   />
        
</ property >
    
</ bean >
    
<!--  Dao 定义  -->
    
< bean  id ="imageDao"  class ="com.fbyssssite.dao.ImageDao"
        parent
="hibernateDaoTemplate"   />
    <bean id="imageFolderDao" class="com.fbyssssite.dao.ImageFolderDao"
        parent
="hibernateDaoTemplate" />
    
<!--  Service 定义  -->
    
< bean  id ="imageService"  class ="com.fbyssssite.bo.ImageService"  autowire ="byName" >
        
< property  name ="dao"  ref ="imageDao" ></ property >
    
</ bean >     
    <bean id="imageFolderService" class="com.fbyssssite.bo.ImageFolderService" autowire="byName">
        
<property name="dao" ref="imageFolderDao"></property>
    
</bean>    

二、测试代码:  

public   void  delete(Object entity,IBaseUser user)  throws  BussException  {
   
super.delete(entity, user);
   ImageFolder folder 
= (ImageFolder)entity;
   String folderId 
= folder.getId();   
   IImageService imageService  
= ServiceFactory.getImageService();
   imageService.deleteAllInFolder(folderId,user);   
  }

  
public   void  deleteAllInFolder(String id,IBaseUser sysUser)  throws  BussException  {
   log.info(
"删除图片夹'"+id+"'中的图片...");
   
throw new BussException(1,"临时测试:测试事务是否生效的异常。测试完毕后删除此语句。",log);
   
/*
   StringBuffer sql = new StringBuffer("delete ");
   sql.append("from ");
   sql.append(getEntityClassName());
   SqlBuilder sqlBuilder  = SqlBuilder.getInstance();
   sqlBuilder.appendWhereCondition(sql, "folderId='"+id+"'");
   bulkUpdate(sql.toString(), sysUser);
   
*/
   
  }


三、测试效果:
抛出BussException之后,imageFolder实体并没有被删除,而是回滚了。
四、注意事项:
1.其中的 BussException是一个CheckedException(继承自Exception),这里不过是一个例子,如果其他方法需要同样的方式处理事务,也要加上-BussException,否则不能回滚;如果是一个UnCheckedException,就不需要在这里配置,默认就会回滚。
2.如果采用MySQL,show table status可以查看表的ENGINE类型,MyISAM是不支持事务的,如果需要事务,应该改成InnoDB:alter table [tablename] engine = InnoDB;


原文链接:http://blog.csdn.net/fbysss/article/details/1744874
加载中
返回顶部
顶部