spring 启动时加载数据字典 Could not obtain transaction-synchronized Session for current t

洛洛洛丽塔 2016-07-07 05:17:00
spring4+hibernate4 想在spring启动时加载数据字典到缓存 然后出现一下问题

<bean class="com.crf.core.web.listener.DictionaryCacheListener">

</bean>



package com.crf.core.web.listener;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.crf.model.TestModel;
import com.crf.service.TestService;

public class DictionaryCacheListener implements ApplicationContextAware {

@Override
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
TestService testService = (TestService)arg0.getBean("testService");
System.out.println("-----------------启动时执行此方法1111111------------------------");
TestModel testModel = testService.getTestModel(1);
System.out.println(testModel.getName());

}
}

baseDao

@Resource(name="sessionFactory")
private SessionFactory sessionFactory;

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

public SessionFactory getSessionFactory() {
return sessionFactory;
}


public Session getSession() {
return sessionFactory.getCurrentSession();
}

出现一下异常

Caused by: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
at com.crf.base.BaseDao.getSession(BaseDao.java:51)
at com.crf.base.BaseDao.load(BaseDao.java:82)
at com.crf.service.TestService.getTestModel(TestService.java:41)
at com.crf.core.web.listener.DictionaryCacheListener.setApplicationContext(DictionaryCacheListener.java:49)
at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:119)
at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:94)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)


上网搜加入hibernate.current_session_context_class=thread

org.hibernate.HibernateException: load is not valid without active transaction


getCurrentSession()到底怎么样才能在spring启动时获取到session 搞了两天
我试过opensession是可以的 是不是getCurrentSession没办法实现呢
...全文
290 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_27443123 2017-09-15
  • 打赏
  • 举报
回复
1.查看你的aop切点, 2..再看你注入下面这个属性的地方。 @Autowired Sessionfactory sessionfactory; 3.对比切点和注入地方的位置是否相同,如果相同则可以用,也就是可以获取session。(本人可以用,但是额外情况另算。) 4.在3不满足的情况下,那么在看看切点是否在注入的上一层,也就是:如果你在dao层注入,你把切点写在了service层, 那么这个注入同样有效,当然在外层我就不知道了(一般不回去切controller层吧!!),

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧