zoukankan      html  css  js  c++  java
  • hibernate添加spring 事务管理注意问题记录

    今天弄了一天的hibernate添加事务的问题

    首先,建立的是一个java工程,把hibernate添加进工程里,很容易就可以写一个增删改查的方法。索性就多加点东西,把接口,抽象类也加到里面,自己看着也舒服点,然后写的时候了,想把spring的功能加进来,想利用spring的aop配置事务,做到事务层配置可控制到service层,这样的话,就可以专注业务实现,一个业务方法就是一个事务。

    首先看配置好的application.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:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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
           ">


        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        </bean>

       
       
        <bean id="iStudentDAO" class="com.wxw.model.StudentDAO">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
       
       
        <bean id="studentServiceImpl" class="com.wxw.service.StudentServiceImpl">
            <property name="iStudentDao" ref="iStudentDAO"></property>
        </bean>
       

       
        
        
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>

       
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="*" propagation="REQUIRED"/>
            </tx:attributes>
        </tx:advice>

        
      

         <!-- 事务控制在service层 -->
         <aop:config>
             <aop:pointcut id="interceptorPointCuts" expression="execution(* com.wxw.service.*.*(..))" />
             <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />       
         </aop:config>     
       
       


    </beans>

    注意点:

    • 自己定义的bean一定要是接口的形式,为什么了,因为spring的配置针对的接口,如果bean配置的实体类,会出现异常
      1. 自己定义的bean也不能是abstract类,否则一样是报错。也不能在abstract类里注入接口等一些操作了
      2. 添加spring aop需要添加对应的jar包外,还需要在xml头部加一些xsd信息,否则编译器会报错,这个是常识性的问题,但却很隐蔽,还有别把头信息版本搞错了,虽然有时候3.0和3.1的版本中有细微的差别,一旦出问题,那可是致命的,找都无法找。
      3. 定义aop:config里的expression的信息时,一定要写对execution()这里面的表达式,这个是有讲究的,自己好好注意一下
      4. 然后是sessionFactory的问题,只能定义一个sessionFactory,别初始化一次,然后又注入一次,两个不同的session事务就不执行了,我测试了一下,确实是不能成功,都生成了sql语句了,但是事务却没有提交成功,而且执行起来,两条sql时间间格差很多,有2秒钟左右,我写的保存语句就是连着写的,本来应该是同一时间出现的。所以,这点也要有明确的思维image
      5. 然后就是保存时候的问题,自己加transation加在代码里,能够提交成功,但配置的事务就是没起作用,肯定是配置写的不对,仔细检查。然后就是一直报save is not valid without active transaction这个错误,我试了好多办法,终于在一个论坛找到了,把hibernate.cxf.xml文件里的一个<property name="hibernate.current_session_context_class">thread</property>注释掉便可,前提是你其他的方的配置写正确了。这个地方原来是hibernate原因网上说的是跟spring接管事务的起冲突了,按照我的理解就是spring接管了hibernate的所有数据访问配置
      6. 事务对RuntimeException默认是会回滚的,而且还不能把这个异常用try捕获,否则事务先提交的一样会成功,后提交的也一样会成功,只有throw了之后就事务就会回滚
      7. 最后的最重要的一点就是,遇到错误不要慌,虽然我也花了整整一天时间,最少有7-8个小时才把这个问题解决,一定要沉住气,哪里出错,先想想,一些明显的错误,上网一查,基本上都能解决,有些问题肯定是你需要花时间去想的,好了,问题解决,花了这么多时间,难得,感觉不错!
  • 相关阅读:
    【vijos】【优先队列】合并果子
    【vijos】【二叉树】FBI树
    【NOIp复习】数据结构之栈、队列和二叉树
    【Leetcode】53. Maximum Subarray
    PHP json_encode转换空数组为对象
    206. Reverse Linked List
    151. Reverse Words in a String
    74. Search a 2D Matrix
    557. Reverse Words in a String III
    【Leetcode】79. Word Search
  • 原文地址:https://www.cnblogs.com/wxwall/p/3176809.html
Copyright © 2011-2022 走看看