Spring + Hibernate:没有为当前线程找到会话
Posted
技术标签:
【中文标题】Spring + Hibernate:没有为当前线程找到会话【英文标题】:Spring + Hibernate : No Session Found For Current Thread 【发布时间】:2015-05-28 11:45:30 【问题描述】:我在解决这个问题时遇到了困难:
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:993)
at com.veram.dao.UsuariosDaoImp.findByUserName(UsuariosDaoImp.java:23)
at com.veram.servicos.ServicosUsuario.loadUserByUsername(ServicosUsuario.java:31)
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:101)
at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:132)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:177)
at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:211)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
类:
应用配置
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.ver.*")
@Import( SecurityConfig.class, DataBaseConfig.class )
public class AppConfig extends WebMvcConfigurerAdapter
//Adiciona a pasta resources ao dispatcher do MVC
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
configurer.enable();
//Localização das views da minha aplicação
@Bean
public InternalResourceViewResolver viewResolver()
InternalResourceViewResolver viewResolver
= new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
数据库配置:
@Configuration
@Import( SecurityConfig.class )
public class DataBaseConfig
@Bean
public SessionFactory sessionFactory()
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource());
builder.scanPackages("com.ver.entid").addProperties(getHibernateProperties());
return builder.buildSessionFactory();
private Properties getHibernateProperties()
Properties prop = new Properties();
prop.put("hibernate.format_sql", "true");
prop.put("hibernate.show_sql", "true");
prop.put("hibernate.dialect", "org.hibernate.dialect.mysql5Dialect");
return prop;
@Bean(name = "dataSource")
public BasicDataSource dataSource()
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/veram_prod");
ds.setUsername("root");
ds.setPassword("root");
return ds;
@Bean
public HibernateTransactionManager txManager()
return new HibernateTransactionManager(sessionFactory());
用户服务:
@Service("userDetailsService")
public class ServicosUsuario implements UserDetailsService
@Autowired
private UsuariosDao userDao;
@Transactional(readOnly=true)
@Override
public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException
com.veram.entidades.Usuarios user = userDao.findByUserName(username);
UserDaoImp
@Repository
public class UsuariosDaoImp implements UsuariosDao
@Autowired
private SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
public Usuarios findByUserName(String username)
List<Usuarios> users = new ArrayList<Usuarios>();
users = sessionFactory.getCurrentSession()
.createQuery("from usuarios where usuario=?")
.setParameter(0, username)
.list();
if (users.size() > 0)
return users.get(0);
else
return null;
我正在尝试了解 Spring 和 Hibernate 的配置。每次我尝试访问当前会话时,我都会在控制台中收到未找到当前线程错误的会话(不会停止应用程序)。 如果有人可以提供帮助,我将不胜感激!
【问题讨论】:
Spring and hibernate: No Session found for current thread 的可能重复项 【参考方案1】:尝试将@EnableTransactionManagement
添加到DataBaseConfig
【讨论】:
以上是关于Spring + Hibernate:没有为当前线程找到会话的主要内容,如果未能解决你的问题,请参考以下文章
我的 spring+hibernate 应用程序没有关闭 jdbc 连接
Spring Hibernate - 无法为当前线程获取事务同步会话
spring / hibernate - 按当前用户 ID 过滤