No Hibernate Session bound to thread纠结了很久

这个问题我在网上看了好多资料,大概就是没有配置transaction,而我已经配置了

<?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-2.5.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
      http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-2.5.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  
  <context:component-scan base-package="com.yuxuan.crm"/>
 <context:annotation-config/>   
  <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/crm"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
  </bean>
 <bean id="mySessionFactory1" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    
        <property name="mappingResources">
      <list>
        <value>com/yuxuan/crm/domain/Department.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
      <value>
        hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
       hibernate.show_sql=true
       hibernate.hbm2ddl.auto=update
      </value>
    </property>
  </bean> 
  
   <!-- similarly, don't forget the PlatformTransactionManager -->
  <bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="mySessionFactory1" />

  </bean>

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

  <aop:config>
    <aop:pointcut id="productServiceMethods" expression="execution(public * com.yuxuan.crm.domain.TestConfiguration.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods"/>
  </aop:config>

  
</beans>


 

@Component
public class TestConfiguration {
	private SessionFactory mySessionFactory;

	
	public SessionFactory getMySessionFactory() {
			return mySessionFactory;
		}
	@Resource(name="mySessionFactory1")

		public void setMySessionFactory(SessionFactory mySessionFactory) {
			this.mySessionFactory = mySessionFactory;
		}	
	
@Test
public void testTransactionManager(){
	Department department = new Department();
	department.setName("吼吼吼");
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
	TestConfiguration tConfiguration =  (TestConfiguration) applicationContext.getBean("testConfiguration");
	tConfiguration.getMySessionFactory().getCurrentSession().save(department);
}
}

但是就是一直出No Hibernate Session bound to thread错误。。。

后来我发现<aop:pointcut id="productServiceMethods" expression="execution(public * com.yuxuan.crm.domain.TestConfiguration.*(..))"/>定义的是运行TestConfiguration里面的方法才自动管理实务,而我是junit testTransactionManager()来测试的,是不是这样测试并没有经过spring的先getBean,在执行相应execution方法的流程?

我有加了一个方法

@Test
public void testTransactionManager(){
	Department department = new Department();
	department.setName("吼吼吼");
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
	TestConfiguration tConfiguration =  (TestConfiguration) applicationContext.getBean("testConfiguration");
	tConfiguration.getMySessionFactory().getCurrentSession().save(department);
}
@Test
public void test3(){
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
	TestConfiguration tConfiguration =  (TestConfiguration) applicationContext.getBean("testConfiguration");
	tConfiguration.testTransactionManager();
}

junit test3()这次运行正确了!那么我的推断就是对的,必须用getBean得出的对象来执行execution的方法,而最开始我

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
	TestConfiguration tConfiguration =  (TestConfiguration) applicationContext.getBean("testConfiguration");
	tConfiguration.getMySessionFactory().getCurrentSession().save(department);

getbean之后是执行的save方法,并不是execution(public * com.yuxuan.crm.domain.TestConfiguration.*(..))这定义的方法,而用Junit测试该方法和spring并没有关系(没有开启事务管理没有创建bean)!

总结:出现No Hibernate Session bound to thread情况,一种是事务声明管理没有配置或者配置错误

还有一种情况是执行的方法并没有设置在<aop:pointcut execution="">中!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值