关于Hibernate Could not obtain transaction-synchronized Session for current thread

版权声明:转载请说明来源 https://blog.csdn.net/flyjiangs/article/details/51537381

最近几年一直再搞android,最近闲下来了,顺便玩一下web。

整了个最新版本的SSH(hibernate5.1.0+spring4.2.6+struts-2.5)

在写dao实现类的时候碰到一个问题,

@Repository
public class UserDaoImpl implements IUserDao {

	@Autowired
	private SessionFactory sessionFactory;

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



用了sessionFactory.getCurrentSession()这个之后,会提示

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread 这个异常。(据说hibernate 4以上的版本才会有)

这里可以用Session session = sessionFactory.openSession(),然后代码中去关闭 session.close.当然为了偷懒的原则

必须不自己去管理session。让Spring容器自己去处理。

研究了一下。发现 只要在

applicationContext.xml 中追加

 <!-- 配置事务管理器 --> 
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 
        <property name="sessionFactory" ref="sessionFactory"></property> 
    </bean>
     
    <tx:annotation-driven transaction-manager="transactionManager"/>


然后再在实现类加上@Transactional



@Repository
@Transactional
public class UserDaoImpl implements IUserDao {

	@Autowired
	private SessionFactory sessionFactory;

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


这样问题就完美解决了。


大神都来看看吧!!!-Could not obtain transaction-synchronized Session for current thread

01-15

异常情况:rnException in thread "taskExecutor-1" org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current threadrn at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)rn at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)rn at cn.uihome.hibernate.dao.HibernateGenericDao.getCurrentSession(HibernateGenericDao.java:73)rn at cn.uihome.hibernate.dao.HibernateGenericDao.save(HibernateGenericDao.java:106)rn at cn.uihome.hibernate.basedao.BaseDao.save(BaseDao.java:187)rn at cn.uihome.service.noification.NotificationCommentManager.addNotificationComment(NotificationCommentManager.java:45)rn at cn.com.guju.util.notification.NotificationCommentUtil.updateNotification(NotificationCommentUtil.java:123)rn at cn.com.guju.util.notification.NotificationCommentUtil.addNotificationForCommented(NotificationCommentUtil.java:101)rn at cn.com.guju.aop.task.activity.AsyncActivityTask.run(AsyncActivityTask.java:32)rn at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)rn at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)rn at java.lang.Thread.run(Thread.java:745)rnrn我是想在action里 开启一个新的线程,但这个线程涉及到Hibernate数据库的操作,所以报这个问题,求救大神给解决啊!!!!!!

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

07-07

spring4+hibernate4 想在spring启动时加载数据字典到缓存 然后出现一下问题rn[code=java]rn rnrn rn[/code]rnrn[code=java]rnpackage com.crf.core.web.listener;rnrnimport org.springframework.beans.BeansException;rnimport org.springframework.context.ApplicationContext;rnimport org.springframework.context.ApplicationContextAware;rnrnimport com.crf.model.TestModel;rnimport com.crf.service.TestService;rnrnpublic class DictionaryCacheListener implements ApplicationContextAware rn rn @Overridern public void setApplicationContext(ApplicationContext arg0)rn throws BeansException rn TestService testService = (TestService)arg0.getBean("testService");rn System.out.println("-----------------启动时执行此方法1111111------------------------");rn TestModel testModel = testService.getTestModel(1);rn System.out.println(testModel.getName());rn rn rnrn[/code]rnbaseDaorn[code=java]rn@Resource(name="sessionFactory")rn private SessionFactory sessionFactory;rn rn public void setSessionFactory(SessionFactory sessionFactory) rn this.sessionFactory = sessionFactory;rn rn rn public SessionFactory getSessionFactory() rn return sessionFactory;rn rn rn rn public Session getSession() rn return sessionFactory.getCurrentSession();rn rn[/code]rn出现一下异常 rn[code=java]rnCaused by: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current threadrn at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)rn at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)rn at com.crf.base.BaseDao.getSession(BaseDao.java:51)rn at com.crf.base.BaseDao.load(BaseDao.java:82)rn at com.crf.service.TestService.getTestModel(TestService.java:41)rn at com.crf.core.web.listener.DictionaryCacheListener.setApplicationContext(DictionaryCacheListener.java:49)rn at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:119)rn at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:94)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)rn at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)rn[/code]rnrn上网搜加入hibernate.current_session_context_class=threadrn[code=java]rn org.hibernate.HibernateException: load is not valid without active transactionrn[/code]rnrngetCurrentSession()到底怎么样才能在spring启动时获取到session 搞了两天rn我试过opensession是可以的 是不是getCurrentSession没办法实现呢

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试