springMVC 整合 mybatis 事务出现Exception不回滚

J2EE 码拜 10年前 (2015-07-11) 1286次浏览 0个评论

service中Integer.parseInt(“qwe”); 会出错,配置的事务应该是出现Exception回滚的,但是依然保存成功没回滚.

下面是我的配置文件

    <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");
	}

30分

@Override
public void saveOrUpdate(TInfo entity) throws Exception{
try{
tInfoDaoM.saveOrUpdate(entity);
Integer.parseInt(“qwe”);
}
}catch(Exception e){
throw e;
}
 

这样不行

30分

你的SqlSessionTemplate bean在配置文件里创建多好
<bean id=”sqlSession” class=”org.mybatis.spring.SqlSessionTemplate”>
<constructor-arg index=”0″ ref=”sqlSessionFactory”/>
</bean>

40分

@Transactional(rollbackFor=Exception.class) ,你在service里面的saveOrUpdate方法上加个这个注解试试
 
解决了
springMVC 配置文件<context:component-scan 跟application中<context:component-scan 注解扫描配置有冲突
springMVC配置文件不扫描service application.xml配置文件扫描service就可以了, 代码已经贴出来
spring-mvc.xml

    <context:component-scan base-package="com.guogong.weixin">
    	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>

application.xml

    <context:component-scan base-package="com.guogong.weixin">
    	<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明springMVC 整合 mybatis 事务出现Exception不回滚
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!