springMVC 调整 mybatis 事务不回滚
springMVC 整合 mybatis 事务不回滚
下面是我的配置文件
下面是我的session工厂java代码
下面是我的DAO java代码
下面是我的service java代码
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方法上加个这个注解试试
下面是我的配置文件
<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方法上加个这个注解试试