本帖最后由 huyong3 于 2014-10-16 01:00:03 编辑

解决方案 »

  1.   

    <tx:method name="*" propagation="REQUIRED" read-only="true"/>
    Hibernate4已经是全部用了自己的事务处理框架,Spring集成事务层必须打开事务放行。
      

  2.   

    请问加上这段代码就行了么?在spring实物层(applicationContext2.xml)中添加就行?
      

  3.   

    尝试了一下配置事务
    <!-- 事务 -->
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
        <tx:advice id="txAdvice">
            <tx:attributes>
                <tx:method name="*" propagation="REQUIRED" read-only="true"/>
                <tx:method name="create*" propagation="REQUIRED" read-only="false" />
                <tx:method name="save*" propagation="REQUIRED" read-only="false" />
                <tx:method name="reg*" propagation="REQUIRED" read-only="false" />
                <tx:method name="update*" propagation="REQUIRED" read-only="false" />
                <tx:method name="delete*" propagation="REQUIRED" read-only="false" />
            </tx:attributes>
        </tx:advice>
         <aop:config>       
            <aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="execution(com.eai.serviceImpl.*.*(..))" 
            order="1"/>
        </aop:config>但是启动项目的时候报错,java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext。
    不知道我离成功越来越近还是越来越远了。
      

  4.   

    问题已经解决了。
    问题就如异常所述,是flushmode属性问题。
    对数据库的操作都属于事务的操作,而数据库默认是读已提交 read-only="true"形式
     当读写操作时会调用DAO方法,而操作DAO的核心业务是Service,即 Spring,然而Spring
    要对调用DAO的方法加以控制,所以就产生了切面(advice)
    新增配置如下:
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="*" propagation="REQUIRED" read-only="true"/>
                <tx:method name="create*" propagation="REQUIRED" read-only="false" />
                <tx:method name="save*" propagation="REQUIRED" read-only="false" />
                //实际只用到reg这个连接点。
                <tx:method name="reg*" propagation="REQUIRED" read-only="false" />
                <tx:method name="update*" propagation="REQUIRED" read-only="false" />
                <tx:method name="delete*" propagation="REQUIRED" read-only="false" />
            </tx:attributes>
        </tx:advice>
         <aop:config>       
            <aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="execution(* com.easi.serviceImpl.*.*(..)))" 
            order="1"/>
        </aop:config>报错需要导入aop的一些包,根据错误提示缺什么导什么就行了。虽然异常解决了,但是我觉得这种方式不好,其实根本原因就是readonly=fale所以不能写入数据源。只要单独设置数据源readonly属性即可,不需要引入切面。当然引入切面好处是以后逻辑业务多了,spring可以可以通过切面连接点很好的保护数据源。