错误是这样的:
当我单独测试dao的时候,会报错:No Session found for current thread
但是把到加入到service时,再测试,就没问题
配置文件如下:
<?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"       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       ">
    <context:annotation-config/>
    <context:component-scan base-package="com.zq"/>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
        <property name="initialSize" value="10"></property>
        <property name="maxActive" value="1000"></property>
        <property name="maxIdle" value="300"></property>
        <property name="minIdle" value="10"></property>
        <property name="maxWait" value="10000"></property>
        <!--<property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="60"/>
        <property name="logAbandoned" value="true" />
        <property name="testOnBorrow" value="true"/>
        <property name="testWhileIdle" value="true"/>
        <property name="validationQuery" value="select getdate()"/>-->
    </bean>
    <!--整合hibernate-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>-->
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.current_session_context_class">
                    org.springframework.orm.hibernate4.SpringSessionContext
                </prop>
                
            </props>
        </property>
        <property name="packagesToScan">
            <value>com.zq.po</value>
        </property>
    </bean>    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>    </bean>    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 对get/find/search开头的方法要求只读事务 -->
            <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="search*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="read*" propagation="REQUIRED" read-only="true"/>
           
            <tx:method name="get*" propagation="REQUIRED"/>
            <tx:method name="count*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
            <tx:method name="list*" propagation="REQUIRED" read-only="true"/>
            <!-- 对其它方法要求事务 -->
            <tx:method name="*" propagation="REQUIRED"/>        </tx:attributes>
    </tx:advice>
    <!--声明一个切面-->
    <aop:config>
        <aop:pointcut id="daoMethods" expression="execution(* com.zq.service..*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods"/>
    </aop:config>   
</beans>
请问这是怎么回事呢,再有就是我这个配置写的规范吗?为什么不能回滚事务呢?hibernatespring

解决方案 »

  1.   

    有一个<tx:method name="*" propagation="REQUIRED"/>这个表示的是其他方法,这个不是只读的。
      

  2.   

    有一个<tx:method name="*" propagation="REQUIRED"/>这个表示的是其他方法,这个不是只读的。
      

  3.   

    试试给你的aop配置,添加roll-back属性,指定异常回滚
      

  4.   

    是不是这样<tx:advice id="txAdvice">
      <tx:attributes>
      <tx:method name="*" rollback-for="Throwable" no-rollback-for="InstrumentNotFoundException"/>
      </tx:attributes>
    </tx:advice>楼主先试试
      

  5.   

    只读事务也可以回滚<tx:advice id="txAdvice" transaction-manager="txManager">
      <tx:attributes>
      <tx:method name="get*" read-only="true" rollback-for="NoProductInStockException"/>
      <tx:method name="*"/>
      </tx:attributes>
    </tx:advice>