xiaotqc0000 2014-06-30 11:04 采纳率: 0%
浏览 284
已采纳

spring mvc 事务失效问题

同时向两个表插入数据,第一条成功,第二条数据插入失败,第一条未回滚,具体配置如下

 

applicationContext.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:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

 <!--将@Controller的注解排除掉 -->
 <context:component-scan base-package="com">
  <context:exclude-filter type="annotation"
   expression="org.springframework.stereotype.Controller" />
 </context:component-scan>

 <!-- 读取配置文件 -->
 <util:properties id="settings"
  location="classpath:config/global.properties" />

 <!-- 读取数据库配置文件 -->
 <bean id="configProperties"
  class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="locations">
   <list>
    <value>classpath:config/jdbc.properties</value>
   </list>
  </property>
 </bean>
 <bean id="propertyConfigurer"
  class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
  <property name="properties" ref="configProperties" />
 </bean>
 <mvc:annotation-driven />

 <!-- 数据源 -->
 <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
  <property name="driver" value="${driver}" />
  <property name="driverUrl" value="${driverUrl}" />
  <property name="user" value="${user}" />
  <property name="password" value="${password}" />
  <property name="alias" value="proxool.aidecenter" />
  <property name="maximumActiveTime" value="300000" />
  <property name="prototypeCount" value="0" />
  <property name="maximumConnectionCount" value="${maximumConnectionCount}" />
  <property name="minimumConnectionCount" value="${minimumConnectionCount}" />
  <property name="simultaneousBuildThrottle" value="50" />
  <property name="houseKeepingTestSql" value="select form CURRENT_DATE" />
 </bean>
 <!-- JDBC模版 -->
 <bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="dataSource" />
 </bean>
 <bean
  class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
  <property name="staticMethod" value="com.common.database.SpringDB.setJdbcTemplate" />
  <property name="arguments" ref="jdbc" />
 </bean>
 <!-- 事务管理器 -->
 <bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource">
   <ref local="dataSource" />
  </property>
 </bean>
 <!-- 事务模板 -->
 <bean id="transactionTemplate"
  class="org.springframework.transaction.support.TransactionTemplate">
  <property name="transactionManager">
   <ref local="transactionManager" />
  </property>
 </bean>
 <aop:config>
  <aop:pointcut id="transactionPointcut"
   expression="execution(* com.service..*.*(..))" />
  <aop:advisor pointcut-ref="transactionPointcut"
   advice-ref="advice" />
 </aop:config>
 <tx:advice id="advice" transaction-manager="transactionManager">
  <tx:attributes>
   <!-- 读取数据方法,一般采用只读事务 -->
   <tx:method name="find*" read-only="true" />
   <!--以下方法,如save,update,delete等对数据库进行写入操作的方法,当产生Exception时进行回滚 -->
   <tx:method name="save*" propagation="REQUIRED" />
   <tx:method name="update*" />
   <tx:method name="delete*" />
  </tx:attributes>
 </tx:advice>
 <tx:annotation-driven transaction-manager="transactionManager"
  proxy-target-class="true" />
</beans>

 

dispatcher-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:context="http://www.springframework.org/schema/context"
 xmlns:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 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.1.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-3.1.xsd 
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
            http://www.springframework.org/schema/jdbc 
            http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
            http://www.springframework.org/schema/cache
            http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
            http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"
 default-autowire="byName">

 <!-- 将@Service注解给去掉 -->
 <context:component-scan base-package="com.controller">
  <context:include-filter type="annotation"
   expression="org.springframework.stereotype.Controller" />
  <context:exclude-filter type="annotation"
   expression="org.springframework.stereotype.Service" />
 </context:component-scan>

 <bean
  class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
  <property name="useSuffixPatternMatch" value="true" />
  <property name="interceptors">
   <list>
    <ref bean="sessionInterceptor"></ref>
   </list>
  </property>
 </bean>

 <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
 <bean
  class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  <property name="messageConverters">
   <list>
    <ref bean="mappingJacksonHttpMessageConverter" />
    <ref bean="stringHttpMessageConverter" />
   </list>
  </property>
 </bean>
 <!-- 负责读写字符串格式的数据 -->
 <bean id="stringHttpMessageConverter"
  class="org.springframework.http.converter.StringHttpMessageConverter">
  <property name="supportedMediaTypes">
   <list>
    <value>text/plain;charset=UTF-8</value>
   </list>
  </property>
 </bean>
 <!-- 负责读写入json格式的数据 -->
 <bean id="mappingJacksonHttpMessageConverter"
  class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
  <property name="supportedMediaTypes">
   <list>
    <value>application/json;charset=UTF-8</value>
   </list>
  </property>
 </bean>

 <!-- 拦截器 -->
 <bean id="sessionInterceptor" class="com.interceptor.SessionInterceptor"></bean>

 <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
 <bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass"
   value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="" />
  <property name="suffix" value=".jsp" />
 </bean>

 <!-- 启用缓存注解功能,该注解一定要声明在Spring主配置文件中才会生效 -->
 <cache:annotation-driven cache-manager="cacheManager" />
 <bean id="cacheManager"
  class="org.springframework.cache.concurrent.ConcurrentMapCacheManager" />

 <!-- 资源文件加载 -->
 <mvc:resources mapping="/upload/**" location="/upload/"
  cache-period="31556926" />
 <mvc:resources mapping="/libs/**" location="/libs/"
  cache-period="31556926" />
</beans>
上传的图片为包结构

 

Service的注解在接口实现类中

@Service
@Transactional
public class SiteServiceImpl implements SiteService {

.......

}

 

请问是哪里配置错了么

  • 写回答

4条回答 默认 最新

  • 征服.刘华强 2014-07-01 14:14
    关注

    这个错误多明显啊,你事物定义到service包上面,然后你在controller包里调用了2个service去操作两张表。这俩个service的事物已经不在一个上下文里了。

    Spring的事物是需要这样的。

    @tran.....
    public void doxx() {
    dao.save(A表)
    dao.delete(B表)
    }

    这样才能生效啊。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问