0 0

org.hibernate.HibernateException: No Session found for current thread5

我是在做spring3.2.1与hibernate4.2Final整合的时候出的这个bug,在网上找了很多资料仍然解决不了

我的这个项目只是想测试在Service层上加上一事务,当向数据库存入一个User的时候,在log里面加上记录

下面给出我的相关代码,实体类User、Logo以及相应的接口类UserDAO与LogDAO就不给出了,对于逻辑没有什么影响:

用于处理User的DAO层的类UserDAOImpl:

package com.edu.hpu.impl;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.edu.hpu.dao.UserDAO;
import com.edu.hpu.model.User;

@Component(value="u")
public class UserDAOImpl implements UserDAO {
	
	private SessionFactory sessionFactory;

	public void save(User user) {
		Session s = sessionFactory.getCurrentSession();
		s.save(user);
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Resource(name="sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}

 用于处理Log的DAO层的类LogDAOImpl:

package com.edu.hpu.impl;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.edu.hpu.dao.LogDAO;
import com.edu.hpu.model.Log;

@Component(value="logDAO")
public class LogDAOImpl implements LogDAO {
	
	private SessionFactory sessionFactory;

	@Override
	public void save(Log log) {
		Session s = sessionFactory.getCurrentSession();
		s.save(log);
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}

 用于联合处理User与Log的Service层次的类UserService类:

package com.edu.hpu.service;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.edu.hpu.dao.LogDAO;
import com.edu.hpu.dao.UserDAO;
import com.edu.hpu.model.Log;
import com.edu.hpu.model.User;

@Component(value="userService")
@Scope(value="singleton")
public class UserService {
	
	private UserDAO userDAO;
	private LogDAO logDAO;
	
	@Transactional
	public void save(User user) {
		userDAO.save(user);
		Log log = new Log();
		log.setMsg("a user been add into database");
		logDAO.save(log);
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}
	
	@Resource(name="u")
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public LogDAO getLogDAO() {
		return logDAO;
	}

	@Resource(name="logDAO")
	public void setLogDAO(LogDAO logDAO) {
		this.logDAO = logDAO;
	}
}

 beans.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:context="http://www.springframework.org/schema/context"
	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.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-3.0.xsd
           http://www.springframework.org/schema/tx
  		   http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
	<context:annotation-config />
	<context:component-scan base-package="com.edu.hpu" />
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	<context:property-placeholder location="jdbc.properties" />

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<!-- 声明可以使用Annotation,而不是使用LocalSessionFactoryBean -->
		
		<property name="dataSource" ref="dataSource" />
		<!-- 指定数据库的连接信息,即指定数据连接池 -->
		
		<property name="annotatedClasses">
		<!-- 指定数据库中的实体 -->
			<list>
				<value>com.edu.hpu.model.User</value>
				<value>com.edu.hpu.model.Log</value>
			</list>
		</property>
		
		<property name="hibernateProperties">
		<!-- 指定方言等属性 -->
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>
		
	</bean>

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

 用于测试的Junit类:

package com.edu.hpu.sevice;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.edu.hpu.model.User;
import com.edu.hpu.service.UserService;

public class TestUser {

	@Test
	public void testSave() {
		ApplicationContext acx = new ClassPathXmlApplicationContext("beans.xml");
		
		UserService service = (UserService)acx.getBean("userService");
		
		service.save(new User());
		
	}
	
}

 出现的问题情况如下,bug信息:

org.hibernate.HibernateException: No Session found for current thread
	at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
	at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:980)
	at com.edu.hpu.impl.UserDAOImpl.save(UserDAOImpl.java:18)
	at com.edu.hpu.service.UserService.save(UserService.java:23)
	at com.edu.hpu.sevice.TestUser.testSave(TestUser.java:18)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	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)

 出现bug的地点,是在getCurrentSession()这个地方。。求指教。。。

2013年5月06日 12:01

1个答案 按时间排序 按投票排序

0 0

采纳的答案

  at com.edu.hpu.impl.UserDAOImpl.save(UserDAOImpl.java:18) 
    at com.edu.hpu.service.UserService.save(UserService.java:23) 
    at com.edu.hpu.sevice.TestUser.testSave(TestUser.java:18) 


UserService没有被事务切面代理

加上
<aop:aspectj-autoproxy />

而且需要cglib包

2013年5月06日 12:12

相关推荐

    org.hibernate.HibernateException: No Hibernate Session bound to thread

    然而,在使用 Hibernate 进行数据库操作时,经常会遇到 "No Hibernate Session bound to thread" 的错误信息。本文将详细介绍该错误的解决方案。 错误原因 "No Hibernate Session bound to thread" 错误信息通常是...

    解决SpringDataJPA报错:org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null w

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/...

    Spring4.0+Hibernate4.0+Struts2.3整合案例

    2、报错:org.hibernate.HibernateException: No Session found for current thread 意思是必须在transcation.isActive()条件下才能执行, 可以解决办法是:当方法不需要事务支持的时候,使用 Session ...

    HIbernate4.3.6整合c3p0所需jar

    Caused by: org.hibernate.HibernateException: Could not instantiate connection provider [org.hibernate.connection.C3P0ConnectionProvider] at org.hibernate.engine.jdbc.connections.internal....

    jdbc+hibernate的jar包

    3. `org.hibernate.HibernateException`:Hibernate抛出的异常类。 4. `org.hibernate.Query`和`org.hibernate Criteria`:用于执行HQL(Hibernate Query Language)和Criteria API查询,比直接使用SQL更加灵活。 ...

    com.microsoft.sqlserver.jdbc.SQLServerException: 只进结果集不支持请求的操作 解决方案

    query.setHint("org.hibernate.fetchSize", 20); // 设置查询结果集大小 query.setFetchSize(20); List&lt;Game&gt; games = query.list(); tx.commit(); } catch (HibernateException e) { if (tx != null) { tx....

    Hibernate+Proxool配置

    由于项目需求的需要,我们引入了连接池。...我们采用了Hibernate,所以可以考虑hibernate自带的连接池机制,但是发现效率不高,而且Hibernate也推荐使用c3p0或Proxool连接池,在我们的项目中采用了Proxool

    Hibernate配置常见错误

    错误表现:在Session关闭后尝试访问懒加载属性,抛出“org.hibernate.LazyInitializationException: could not initialize proxy - no Session”异常。 解决方案:理解并合理使用Open Session in View(OSIV)模式...

    hibernate测试时遇到的几个异常及解决方法汇总

    3. org.hibernate.HibernateException: No CurrentSessionContext configured! 该异常的解决方法是添加 hibernate-jpa-2.0-api-1.0.0.Final.jar。这是因为 Hibernate 需要配置 CurrentSessionContext,以便正确地...

    J2EE利用Hibernate采用B/S架构网页设计

    import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Configuration; /** * Configures and provides access to Hibernate sessions, tied to the * current ...

    spring_MVC源码

    18. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; 19. &lt;property name="dataSource" ref="dataSource" /&gt; 20. &lt;property name="hibernateProperties"&gt; 21. &lt;props&gt; 22...

    Hibernate源代码分析

    public org.hibernate.classic.Session getCurrentSession() throws HibernateException { if (currentSessionContext == null) { throw new HibernateException("No CurrentSessionContext configured!"); } ...

    Hibernate开发必备版

    import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; ``` 2. **类声明:** ```java public class HibernateUtil { ``` 3. **静态...

    学习hibernate第一章内容

    5. 异常体系:理解Hibernate的异常体系,如HibernateException、ConstraintViolationException等。 6. Session和Transaction:理解Session的生命周期和事务管理,以及它们在实际应用中的最佳实践。 本章作为...

    JAVA错误文档.pdf

    4. Hibernate框架错误:文档还包含了“org.hibernate.HibernateException: No Hibernate Session bound to thread”等错误信息,这是Hibernate框架常见的异常,表明在当前线程上没有绑定Hibernate Session。...

    hibernate错误汇总

    错误八:Exception in thread "main" org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: org.model.User 错误原因:这个错误表示在尝试获取主键为1的实体...

    Jbuilder里配置Hibernate

    &lt;property name="current_session_context_class"&gt;thread &lt;!-- Echo all executed SQL to stdout --&gt; &lt;property name="show_sql"&gt;true &lt;!-- Drop and re-create the database schema on startup --&gt; ...

    hibernate-release-5.4.25.Final_Hibernate5.4.25_hibernate所需jar包_源

    5. 异常处理:Hibernate将SQL异常转换为特定的HibernateException,便于理解和处理。 总之,Hibernate 5.4.25为Java开发者提供了强大的数据持久化解决方案,通过合理的jar包配置和应用,可以极大地提升开发效率,...

Global site tag (gtag.js) - Google Analytics