最近做项目,被一个问题烦恼了很久。使用Spring MVC+Spring +Hibernate开发项目,在使用注解配置事务管理,刚开始发现无论如何数据库都无法更新,但是可以从数据库查询到数据。怀疑是配置文件的问题,但是反复确认,配置文件没有问题。后来将注解配置事务变更为注解配置事务,同样无法入库。

后来在dao层方法里面添加了session.flush()数据可以入库,但是不能回滚。正常的情况下,配置事务成功的话,事务会自动提交,session会自动flush。于是怀疑配置的事务根本就没有起到作用。在网上查找资料,终于发现问题症结所在。

引起问题原因:spring初始化时优先component-scan bean,注入Web Controller 的Service直接拿了上下文中的@Service("someService"),这个时候@Transactional (readOnly=false, isolation = Isolation.READ_COMMITTED) 还没有被处理.所以Web Controller 的Service是SomeServiceImpl而不是AOP的$ProxyNO.

解决途径:不让spring扫描到ServiceImpl类,直接写在xml文件,其它的配置和annotation照用,这样事务就起作用了.

按照上述配置,发现事务起作用了,自动提交,并且在出现异常的时候自动回滚。

工程具体配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>SMS</display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:school-spring-*.xml</param-value></context-param><listener><!-- 负责加载spring的配置文件 --><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 直接使用spring的encodingFilter--><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>*</url-pattern></filter-mapping><!-- 集成使用Spring MVC --><servlet><servlet-name>defaultDispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><!-- Spring XML 文件 --><param-value>classpath*:school-springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>defaultDispatcher</servlet-name><url-pattern>/school/*</url-pattern></servlet-mapping><!-- 验证码生成 --><servlet><servlet-name>image_code</servlet-name><servlet-class>cn.com.yinuo.common.web.util.RandomCode</servlet-class></servlet><servlet-mapping><servlet-name>image_code</servlet-name><url-pattern>/imgCode</url-pattern></servlet-mapping><!-- 解决hibernate延迟加载问题 --><!-- <filter><filter-name>openSession</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping><filter-name>openSession</filter-name><url-pattern>/*</url-pattern></filter-mapping>--><!-- session超时定义,单位为分钟 --><session-config><session-timeout>30</session-timeout></session-config><!-- 欢迎界面 --><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>

web.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:aop="http://www.springframework.org/schema/aop"xmlns:jee="http://www.springframework.org/schema/jee"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd"><!-- Spring管理配置文件 --><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath*:school_config.properties</value></list></property><!-- 可注入中文 --><property name="fileEncoding" value="UTF-8"/></bean><!-- 注解扫描包  会扫描school包下面所有的类文件以及包  待确认--><context:component-scan base-package="cn.com.yinuo.school"><context:exclude-filter type="regex" expression="cn.com.yinuo.*.web.*"/><context:exclude-filter type="regex" expression=".*ServiceImpl$" /></context:component-scan><!-- 配置multipartResolver处理器 --><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="defaultEncoding" value="utf-8" /><property name="maxUploadSize" value="10485760000" /><property name="maxInMemorySize" value="40960" /></bean><bean name="informationService" class="cn.com.yinuo.school.InformationMaintenance.biz.service.impl.InformationServiceImpl"></bean>
</beans>

school-spring-core.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"><!-- 注解扫描包  会扫描school包下面所有的类文件以及包  待确认--><context:component-scan base-package="cn.com.yinuo.school"><!-- <context:include-filter type="regex" expression="cn.com.yinuo.*.web.*"/> --></context:component-scan><!-- 开启注解 --><mvc:annotation-driven/><!-- 上面开启注解的方式是下面开启注解方式的优化,下面的方式在3之后就过时了 --><!--  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /><bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>--><!-- 访问静态资源   --><mvc:resources location="/js/" mapping="/js/**"/><mvc:resources location="/img/" mapping="/img/**"/><mvc:resources location="/css/" mapping="/css/**"/><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean>
</beans>

school-springmvc.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:aop="http://www.springframework.org/schema/aop"xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"><!-- Spring管理 Hibernate --><!-- 配置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="${initialSize}"/><property name="maxActive" value="${maxActive}"/><property name="maxIdle" value="${maxIdle}"/><property name="minIdle" value="${minIdle}"/></bean><bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource"/><property name="packagesToScan"><list><value>cn.com.yinuo.school.model.domain</value></list></property><property name="mappingResources"><list><value>../resources/${resource.mapping.sql}</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop><prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop><prop key="hibernate.format_sql">${hibernate.format_sql}</prop><prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop><prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop><!-- <prop key="hibernate.connection.autocommit">${hibernate.connection.autocommit}</prop>-->  <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop><prop key="hibernate.cache.use_second_level_cache">true</prop><prop key="Hibernate.current_session_context_class">thread</prop><prop key="hibernate.cache.use_query_cache">false</prop><prop key="hibernate.connection.release_mode">after_transaction</prop></props></property></bean><bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"/></bean><context:annotation-config/><aop:aspectj-autoproxy proxy-target-class="true"/> <aop:config proxy-target-class="true">  </aop:config> <!--  注解配置事务--><tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /><!-- <tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="add*" propagation="REQUIRED" read-only="false" rollback-for="Exception" /><tx:method name="update*" propagation="REQUIRED" read-only="false"/><tx:method name="insert*" propagation="REQUIRED" read-only="false"/><tx:method name="modify*" propagation="REQUIRED" read-only="false"/><tx:method name="delete*" propagation="REQUIRED" read-only="false"/><tx:method name="get*" propagation="REQUIRED" /><tx:method name="save*" propagation="REQUIRED" read-only="false"/></tx:attributes></tx:advice><aop:config><aop:pointcut id="interceptorPointCuts"expression="execution(* cn.com.yinuo.school.InformationMaintenance.biz.service.impl.InformationServiceImpl.*(..))" /><aop:advisor advice-ref="txAdvice"pointcut-ref="interceptorPointCuts" />       </aop:config>      -->
</beans>

school-spring-hibernate.xml

package cn.com.yinuo.school.InformationMaintenance.web;import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import net.sf.json.JSONObject;import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import cn.com.yinuo.school.InformationMaintenance.biz.service.InformationService;
import cn.com.yinuo.school.model.domain.Student;
import cn.com.yinuo.school.util.Json;@Controller
@RequestMapping("/information")
public class InformationController {@Autowiredprivate InformationService informationService;private static Logger logger=Logger.getLogger(InformationController.class);@RequestMapping("/toRegister")public String toRegister(){return "JSP/imformation/register";}@RequestMapping("/addUser")public Json addUser(HttpServletRequest request,HttpServletResponse response){Student student=new Student();student.setName("阿享");student.setClassId("001001");student.setCreditId("4365671235678931");student.setMailBox("591137758@qq.com");student.setGender(0);student.setPassword("janeyloveachris");student.setType(1);informationService.addUser(student);Json json=new Json();json.setSuccess(true);json.setMsg("新增成功");response.setCharacterEncoding("UTF-8");response.setContentType("application/json; charset=utf-8");PrintWriter pw=null;JSONObject jsonObject =JSONObject.fromObject(json);try {pw=response.getWriter();pw.append(jsonObject.toString());logger.debug("返回到前台的对象是"+jsonObject.toString());} catch (IOException e) {e.printStackTrace();}finally{if(pw!=null){pw.close();}}return null;}
}

InformationController.java

package cn.com.yinuo.school.InformationMaintenance.biz.service.impl;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import cn.com.yinuo.school.InformationMaintenance.biz.dao.InformationDao;
import cn.com.yinuo.school.InformationMaintenance.biz.service.InformationService;@Transactional
@Service
public class InformationServiceImpl implements InformationService {@Autowiredprivate InformationDao informationDao;@Transactional(propagation=Propagation.REQUIRED,rollbackFor=java.lang.Exception.class,readOnly=false)@Overridepublic void addUser(Object object) {informationDao.addUser(object);}@Overridepublic Object getAllUsers() {return informationDao.getAllUsers();}}

InformationServiceImpl.java

package cn.com.yinuo.school.InformationMaintenance.biz.dao.impl;import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;import cn.com.yinuo.school.InformationMaintenance.biz.dao.InformationDao;
import cn.com.yinuo.school.model.domain.Student;
import cn.com.yinuo.school.model.domain.Teacher;@Repository
public class InformationDaoImpl /*extends HibernateDaoSupport */implements InformationDao {@Autowiredprivate SessionFactory sessionFactory;private Logger logger=LoggerFactory.getLogger(InformationDaoImpl.class);@Overridepublic void addUser(Object object) {/*Session session=sessionFactory.openSession();Transaction tc=session.beginTransaction();*/Session session=sessionFactory.getCurrentSession();//tc.begin();if(object instanceof Student){//getHibernateTemplate().save((Student)object);//sessionFactory.getCurrentSession().save((Student)object);
            session.saveOrUpdate((Student)object);}else if(object instanceof Teacher){//getHibernateTemplate().save((Teacher)object);
            session.save((Teacher)object);}//tc.commit();//session.flush();System.out.println((1/0));}
@Overridepublic Object getAllUsers() {//super.getHibernateTemplate().find("");Student st=new Student();Query query=sessionFactory.openSession().createQuery("from "+st.getClass().getName());logger.debug(query.getQueryString());return query.list();}}

InformationDaoImpl.java

后来师父找到了我的问题所在:在springmvc.xml在扫描包的时候缺少了:use-default-filters="false"

<context:component-scan base-package="cn.com.yinuo.school" use-default-filters="false" ><context:include-filter type="regex" expression="cn.com.yinuo.*.web.*Controller$"/> </context:component-scan>

添加之后,去掉spring-core.xml中的

<context:component-scan base-package="cn.com.yinuo.school"><context:exclude-filter type="regex" expression="cn.com.yinuo.*.web.*Controller$"/><!-- <context:exclude-filter type="regex" expression=".*ServiceImpl$" /> --></context:component-scan>

以及service的配置执行发现事务同样起到作用了。

去掉我的其他配置,同时不加use-default-filters="false",会报错 session没有绑定。

师父参照的博客文章地址:http://blog.csdn.net/shuwei003/article/details/7281792

转载于:https://www.cnblogs.com/loveyixiang/p/4446615.html

Spring MVC+Spring +Hibernate配置事务,但是事务不起作用相关推荐

  1. Spring JDBC-使用注解配置声明式事务

    系列 概述 使用Transactional注解 txannotation-driven其他属性 关于Transaction的属性 在何处标注Transactional注解 在方法处使用注解 使用不同的 ...

  2. Spring JDBC-使用XML配置声明式事务

    系列 概述 基于aop/tx命名空间的配置 示例 tx:method元素属性 系列 Spring对事务管理的支持概述以及 编程式的事务管理 Spring JDBC-使用XML配置声明式事务 Sprin ...

  3. (转)为Spring集成的Hibernate配置二级缓存

    http://blog.csdn.net/yerenyuan_pku/article/details/52896195 前面我们已经集成了Spring4.2.5+Hibernate4.3.11+Str ...

  4. Spring MVC 无XML配置入门示例

    Spring MVC 无XML(纯 Java)配置入门示例 本示例是从<Spring in Action, Fourth Edition>一书而来,涉及的是书中5.1节部分内容,书中其实说 ...

  5. Spring MVC + Spring + Hibernate + mysql 注册登陆入门实例

    Spring MVC + Spring + Hibernate + mysql 注册登陆入门实例 (1) 结构 (说明目的是要做在线聊天室的,也包含登陆注册部分,先用这部分做个例子)开发环境用的是ST ...

  6. Spring MVC原理及配置详解

    转载自 http://blog.csdn.net/jianyuerensheng/article/details/51258942 [Spring]Spring MVC原理及配置 1.Spring M ...

  7. 【Spring】Spring MVC原理及配置详解

    [Spring]Spring MVC原理及配置 1.Spring MVC概述: Spring MVC是Spring提供的一个强大而灵活的web框架.借助于注解,Spring MVC提供了几乎是POJO ...

  8. 超详细Spring MVC的环境配置:IDEA环境下创建Maven WebAp

    Spring MVC的环境配置: 1. IDEA环境下创建Maven WebApp 配置示例如下: 第一步,创建maven工程 第二步,配置web功能: 第三步,配合Tomcat 运行web文件 2. ...

  9. Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建

    目录 Spring MVC+Spring+Mybatis+MySQL(IDEA)入门框架搭建 0.项目准备 1.数据持久层Mybatis+MySQL 1.1 MySQL数据准备 1.2 Mybatis ...

最新文章

  1. 英伟达验证图片加载不出来_让大卫雕塑跳舞、蒙娜丽莎说话,英伟达视频合成有如此多「骚操作」...
  2. C语言 使用指针对两个变量的数值进行互换
  3. QTP自动化测试视频系列
  4. GVRP:GARP VLAN 注册协议 - 802.1P、802.1Q (GARP VLAN Registration Protocol - 802.1P、802.1Q)...
  5. finishBeanFactoryInitialization 处理预实例化Bean
  6. html5 内嵌网页_如何分析并优化网页的性能?新梦想软件测试
  7. Android位置服务介绍,并介绍如何通过LocationManager对象获取位置信息
  8. 技术实操丨HBase 2.X版本的元数据修复及一种数据迁移方式
  9. 保护你的眼睛,把电脑屏幕由白色改为淡绿
  10. 10. Browser 对象 - Location 对象(2)
  11. 如何证明永动机不可能制造出来
  12. vue+axios 拦截器实现统一token
  13. PR视频编辑软件Premiere软件安装包下载地址及安装教程
  14. 从零开始实现一个颜色选择器(原生JavaScript实现)
  15. Windows如何对硬盘进行分区?
  16. CBv92_GSHI 使用技巧、电脑输CBC码、金手指分区数据复制和备份
  17. C语言 逻辑运算符及其优先次序(一)
  18. Matlab滤波器设计与滤波器特性分析(sptool、filterdesigner)
  19. 前端启动本地服务的四种方法,看完不会你锤我
  20. 反编译008神器,修改手机型号与android版本号信息

热门文章

  1. how to add one row in the dataframe?
  2. advanced search at idiscover
  3. 在食堂吃饭是最好的解
  4. 其实我们不需要那么大的房子
  5. 在线抠图工具:亲测有效
  6. DM 源码阅读系列文章(二)整体架构介绍
  7. SpringBoot实现Java高并发秒杀系统之DAO层开发(一)
  8. maven+tomcat8.0+eclipse远程部署项目
  9. [firefox] Scrapbook Plus的改进版Scrapbook X
  10. 不要在覆写的方法中用super