当前位置: 代码迷 >> J2EE >> springMVC 调整 mybatis 事务不回滚
  详细解决方案

springMVC 调整 mybatis 事务不回滚

热度:731   发布时间:2016-04-17 23:08:25.0
springMVC 整合 mybatis 事务不回滚
本帖最后由 nhy338 于 2015-07-08 10:02:16 编辑
下面是我的配置文件

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="20" />
<property name="maxWait" value="10000" />
    </bean>
 <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->  
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>
    
    <tx:advice id="baseServiceAdvice"  transaction-manager="transactionManager">
     <tx:attributes>
     <tx:method name="save*"  propagation="REQUIRED"/>
     <tx:method name="add*"  propagation="REQUIRED"/>
     <tx:method name="update*"  propagation="REQUIRED"/>
     <tx:method name="delete*"  propagation="REQUIRED"/>
     <tx:method name="remove*"  propagation="REQUIRED"/>
     <tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
     </tx:attributes>
    </tx:advice>
    
    <aop:config>
     <aop:pointcut id="baseServiceMethod"  expression="execution(* com.guogong.weixin.service.impl.*.*(..))"/>
     <aop:advisor pointcut-ref="baseServiceMethod"  advice-ref="baseServiceAdvice"/>
    </aop:config>

      <!-- 创建SqlSessionFactory,同时指定数据源 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <property name="mapperLocations" value="classpath:com/guogong/weixin/dao/sqlmaps/*.xml"/>
    </bean>  

下面是我的session工厂java代码

@Autowired
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
this.sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
}

public SqlSessionTemplate getSqlSessionTemplate() {
return sqlSessionTemplate;
}

public void save(E entity) {
prepareObjectForSaveOrUpdate(entity);
@SuppressWarnings("unused")
int affectCount = getSqlSessionTemplate().insert(getInsertStatement(),
entity);
}

public void update(E entity) {
prepareObjectForSaveOrUpdate(entity);
@SuppressWarnings("unused")
int affectCount = getSqlSessionTemplate().update(getUpdateStatement(),
entity);
}

下面是我的DAO java代码

@Override
public void saveOrUpdate(TInfo entity) throws DataAccessException {
if(entity.getId() == null){
entity.setId(8888);
save(entity);
} else {
update(entity);
}
}

下面是我的service java代码

@Override
public void saveOrUpdate(TInfo entity) {
tInfoDaoM.saveOrUpdate(entity);
Integer.parseInt("qwe");
}


service中Integer.parseInt("qwe"); 会出错,配置的事务应该是出现Exception回滚的,但是依然保存成功没回滚.
------解决思路----------------------
@Override
public void saveOrUpdate(TInfo entity)  throws Exception{ 
  try{
      tInfoDaoM.saveOrUpdate(entity);   
    Integer.parseInt("qwe");
 } 
 }catch(Exception e){
   throw e;
}
   
------解决思路----------------------
你的SqlSessionTemplate bean在配置文件里创建多好
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">  
        <constructor-arg index="0" ref="sqlSessionFactory"/>  
   </bean>
------解决思路----------------------
@Transactional(rollbackFor=Exception.class) ,你在service里面的saveOrUpdate方法上加个这个注解试试
  相关解决方案