spring+mybatis事务不回滚

对spring,mybatis进行整合时发现事务不能进行回滚处理,上网查了很多资料依旧还没解释,很多都是说要抛出一个runtimeException才能回滚的,但尝试过多种还不能,代码如下:
applicationContext.xml
        ...
        <!-- 定义易受环境影响的变量 -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<property name="ignoreResourceNotFound" value="true" />
		<property name="locations">
			<list>
				<value>classpath*:/jdbc.properties</value>				
			</list>
		</property>
	</bean>	
	
	<!-- 扫描业务组件 -->
	<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
	<context:component-scan base-package="com.myframe" />
	<context:component-scan base-package="com.topone" />

	<!-- 数据源配置, 使用应用中的DBCP数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<!-- Connection Info -->
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.databaseurl}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />

		<!-- Connection Pooling Info -->	
		<property name="initialSize" value="${dbcp.initialSize}" />
		<property name="maxActive" value="${dbcp.maxActive}" />
		<property name="maxIdle" value="${dbcp.maxIdle}" />
		<property name="maxWait" value="${dbcp.maxWait}" />
	</bean>		
	
	<!-- jdbcTemplate -->
	<bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate"> 
         <constructor-arg>   
              <ref bean="dataSource" />   
         </constructor-arg>  
	</bean>

	<!-- define the SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.myframe,com.topone" />
        <property name="configLocation" value="classpath:/spring/mybatis_configuration.xml"/>
        <property name="mapperLocations">
        	<list>
        		<value>classpath*:com/myframe/security/mapper/*.xml</value>
        		<value>classpath*:com/topone/**/**/mapper/*.xml</value>
        	</list>
        </property>       
    </bean>
<!-- scan for mappers and let them be autowired -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.myframe,com.topone" />
    </bean>
    
    <bean id="gridDao" class="com.myframe.admin.grid.GridDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
	
	
	<!-- transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!--  使用annotation定义事务-->  
  	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

    ...


BolgService:
@Service
@Transactional(rollbackFor=Exception.class)
public class BlogService {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private BlogflowService blogflowService;
     
    //保存数据
    @Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED, readOnly=true) 
    public String save(Blog entity, Map<String,String> map) throws Exception{
     ...
      int isok = blogMapper.save(entity); //没发生回滚
      BlogFlow vo = new BlogFlow();
      //get/set方法
      vo.setId();
      ...
      blogflowService.save(vo);         //调用blogflowService保存方法
     ...
    }
}


BolgFlowService:
@Service
@Transactional(rollbackFor=Exception.class)
public class BlogflowService {
    @Autowired
    private BlogMapper blogflowMapper;
     
    //保存数据
    @Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED, readOnly=true) 
    public String save(Blog entity, Map<String,String> map) throws Exception{
     ...
      entity.setTouserid(null);  //故意设置为null,抛出异常
      int isok = blogflowMapper.save(entity);
     ...
    }
}


查看数据库后,由于bolgFolw设置为null了,抛出异常,所以没有增加到数据库,但bolgService里已经将一条记录添加数据库里了,没有发生回滚事件,请问是那里的设置出问题了呢,如果用编码式来控制的话是可以回滚的。估计应该是配置或代码写得不对的问题,但看了别人的例子,也是这样写的,不明白原因在那里。

另外,我是用*mapper.java这种方式来实现dao层来。

目录路径为:
com
  topone
      approval       
          blog   
             service
             mapper
             contorller
             entity
          blogflow
             service
             mapper
             contorller
             entity


       

猜你喜欢

转载自tcrct.iteye.com/blog/1497602