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

一、关键配置示例:
<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 {(来源:www.iocblog.net)
   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;

posted on 2008-04-29 11:58 刘铮 阅读(815) 评论(0)  编辑  收藏 所属分类: Spring


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

导航

统计

留言簿(1)

文章分类(141)

文章档案(147)

搜索

最新评论