spring mvc+mybatis+mysql 事务失效

夏V风 2016-01-06 08:09:29
如题,配置文件如下
spring_applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 自动扫描web包 ,将带有注解的类 纳入spring容器管理 -->
<context:component-scan base-package="com.web">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 引入jdbc配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/config/jdbc.properties" />
</bean>
<!-- dataSource 配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" />

<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />

<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />

<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'X' from dual" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
</bean>
<!-- mybatis文件配置,扫描所有mapper文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/web/dao/mapping/*SqlMap.xml"></property>
</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.web.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 以 @Transactional 标注来定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />

<!-- transaction manager, use DataSourceTransactionManager -->
<!-- <aop:config>
<aop:pointcut id="fooServiceMethods"
expression="execution(* com.web.*.service.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceMethods" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" read-only="true" />
<tx:method name="load*" read-only="true" />
<tx:method name="*" rollback-for="CustomException" />
</tx:attributes>
</tx:advice> -->
</beans>


springMvc_servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" >
<!-- 约定优于配置,约定优于配置 -->
<!-- 使注解生效 -->
<mvc:annotation-driven />
<!-- 扫描所有的controller -->
<!--扫描web包,应用Spring的注解 -->
<context:component-scan base-package="com.web">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd -->
<mvc:resources mapping="/img/**" location="/static/img/" />
<mvc:resources mapping="/js/**" location="/static/js/"/>
<mvc:resources mapping="/css/**" location="/static/css/"/>
<mvc:resources mapping="/easyUi/**" location="/static/easyUi/**" />
<mvc:resources mapping="/zTree/**" location="/static/zTree/**" />
<!-- InternalResourceViewResolver默认的就是JstlView所以这里就不用配置viewClass了 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/html/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.web.utils.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>

<!-- 启用基于注解的处理器映射,添加拦截器,类级别的处理器映射 -->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<bean class="com.fsj.spring.util.MyHandlerInterceptor"/>
</list>
</property>
</bean> -->

<!--
配置一个基于注解的定制的WebBindingInitializer,解决日期转换问题,方法级别的处理器映射,
-->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="cacheSeconds" value="0" />
<property name="webBindingInitializer">
<bean class="com.web.utils.MyWebBinding" />
</property>
</bean> -->

</beans>


CaptchaServiceImpl
package com.web.service.impl;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import net.sf.json.JSONObject;

import com.web.bean.CaptchaBean;
import com.web.dao.CaptchaDao;
import com.web.service.CaptchaService;
import com.web.utils.MyUtils;

@Service
@Transactional
public class CaptchaServiceImpl implements CaptchaService {
@Resource
private CaptchaDao cDao;

@Override
public boolean insertCaptcha(HttpServletRequest request) {
//String param=MyUtils.getParam(request);
String param="{\"mobile\":\"15333333333\"}";
JSONObject pjs = JSONObject.fromObject(param);
String code=MyUtils.createCaptcha();
String mobile=pjs.getString("mobile");
//将前面发送的验证码失效
cDao.updateCaptchaByMoblie(mobile);
CaptchaBean cb=new CaptchaBean();
cb.setCaptcha(code);
//cb.setMoblieNumber(mobile);此处故意让insert报错。但是update没有回滚
cb.setCreateTime(new Date());
int i=cDao.insertCaptcha(cb);
if(i>0){
return true;
}
return false;
}

}
...全文
185 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wushen333 2016-01-07
  • 打赏
  • 举报
回复
@Transactional默认的是RuntimeException才会回滚吧。你抛出的异常是RuntimeException吗?或者直接在最后写一句 throw new RuntimeException() 来测试一下
夏V风 2016-01-07
  • 打赏
  • 举报
回复
引用 2 楼 wushen333 的回复:
@Transactional默认的是RuntimeException才会回滚吧。你抛出的异常是RuntimeException吗?或者直接在最后写一句 throw new RuntimeException() 来测试一下
2楼正解。改了注解方式就搞定了。@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
azulwang 2016-01-07
  • 打赏
  • 举报
回复
事务控制在接口层,不是实现类 <!-- 配置事务切面 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="batchUpdate*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="aopPointCut" expression="execution(* com.stengg..*Service.*(..))" /> <aop:advisor pointcut-ref="aopPointCut" advice-ref="txAdvice" /> </aop:config>
一叶飞舟 2016-01-06
  • 打赏
  • 举报
回复
1、expression="execution(* com.web.*.service.impl.*.*(..))" 2、com.web.service.impl.CaptchaServiceImpl  这两点能解释下吗?

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧