大家好,这两天在用ssh框架写个项目。我在sring的配置文件中配置了aop的事物声明。但是单元测试时一直报
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:695)
at com.zgh.shop.dao.impl.BaseDaoImpl.saveEntity(BaseDaoImpl.java:37)
at com.zgh.shop.service.impl.BaseServiceImpl.saveEntity(BaseServiceImpl.java:69)
at com.zgh.shop.test.TestDao.save(TestDao.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
不知道是什么问题?我的beans.xml的配置文件为:
 <!-- 配置扫描配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 扫描组件 -->
    <context:component-scan base-package="com.zgh.shop.dao,com.zgh.shop.service,com.zgh.shop.struts.action" />
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driverclass}" />
    <property name="jdbcUrl" value="${jdbc.url}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}" /> <property name="maxPoolSize" value="${c3p0.pool.size.max}" />
<property name="minPoolSize" value="${c3p0.pool.size.min}" />
<property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
<property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
    </bean>
    
    <!-- 配置本地会话工厂bean,hibernate的核心入口 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- hibernate自身属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
<!-- 映射目录位置集 -->
<property name="mappingDirectoryLocations">
<list>
 <value>classpath:com/zgh/shop/domain</value>
</list>
</property>
</bean>

<!-- hibernate事物管理器,在service层面上实现事物管理 -->
<bean id="hibernateTransactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
     <!-- 事务通知 -->
    <tx:advice id="txAdvice" transaction-manager="hibernateTransactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT"/>
            
            <tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
            <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
            <tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
            
            <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- aop配置 -->
    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* *..*Service.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>以前我就是这样配置的,在其他项目中就能运行成功。但是现在一直报错,请帮我找找原因,谢谢了!!!

解决方案 »

  1.   

    你的配置没错,可能是service没有交spring管理,你仔细看看代码看写错了吧,再看看你的service里的方法名在不在配置文件中声明
      

  2.   

    No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one heresession会话没有绑定到线程,不能创建事物 。 你是不是没加载配置文件啊。
      

  3.   

    什么意思?单元测试时加载了。
    public class TestC3p0 {
    private static ApplicationContext ac = null ;
    @BeforeClass
    public static void iniAc(){
    try {
    ac = new ClassPathXmlApplicationContext("beans.xml");
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
      

  4.   

    我也是菜鸟,你最好耐心的看看原理,看看Spring的配置原理,都是做什么用的,一知半解终究不会成为大牛,你给我看看你的项目,我改对了你还是不会的,自己耐下心来解决,不要怕麻烦,这才是最重要的!心里不要骂我啊!!
    你的service里的方法配置事务是在配置文件里配置,你把你方法的名字写在配置文件里,初步认定是这里出错了。
    再告诉你个方法:1.下载个“有道” 把错误信息翻译一下,你就会恍然大悟
    2.找错一般从下开始找,我说的是后台报的错啊,从最低下的CauseBy开始找,一条条解决
      

  5.   

    servie方法的事物是在配置文件中配置的,在service层的实现类中加@service注解了。能扫描到这个类。我上一个项目就是按照这种方式配置的,那个项目就没错。我把两次的日志信息对比了一下也没发现有什么异常。而且这次创建的项目,映射的表已经在数据库何总生成了。项目配置的应该没有什么问题。我是在是想不出到底是哪出错了。
      

  6.   

    应该是说的是,你的service里面注入的sessionFactory.getCurrentSession();
    不能获取到值,是因为他不能配置在没有注入的事务环境中,
    也就是说,应该是你的service的方法没有配置到相应的事务上
      

  7.   

    我在service上配置事物了。
     <!-- aop配置 -->
        <aop:config>
            <aop:pointcut id="txPointcut" expression="execution(* *..*Service.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
        </aop:config>
    而且在service层的实现类上也加了@service
      

  8.   

    你把你的service里需要配置事务的方法改改名字,然后再在配置文件中声明,这样试试看行吧