Could not obtain transaction-synchronized Session for current thread 异常处理方式

String4版本 在biz层做多线程处理集合任务报错:

Could not obtain transaction-synchronized Session for current thread


这种错误暂时没有发现是什么原因,按照网上的配置方法改wms.xml或者在方法加@Transactional都不行

今天不甘心,把代码移动到非biz层去做就没问题了。

Spring配置:

<!-- 配置事务切面 -->
<aop:config>
<aop:pointcut id="serviceOperation"
expression="execution(* com.oa..biz.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="serviceOperation" />
</aop:config>


<!-- 自动加载构建bean -->
<context:component-scan base-package="com.oa" />
<task:annotation-driven />


我是在job里面做定时任务的,把处理的多线程逻辑就放到job那里,然后取调用biz层的代码就没有发现任何问题。


大神都来看看吧!!!-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没办法实现呢 论坛

Hibernate4 No Session found for current thread异常

10-07

标题:Hibernate4 No Session found for current thread与createQuery is not valid without active transaction异常n描述:n在项目程序运行时遇到了Hibernate4 No Session found for current thread异常,nn ![图片说明](https://img-ask.csdn.net/upload/201610/07/1475829970_705784.png)n百度了一下n在hibernate的配置文件中加入了threadn配置并且处理业务逻辑的类上也加入了注解如下nn package com.xiaonei.baseService;nnimport java.util.List;nnimport javax.annotation.Resource;nnimport org.hibernate.Query;nimport org.hibernate.Session;nimport org.hibernate.SessionFactory;nimport org.springframework.transaction.annotation.Transactional;nn@Transactionalnpublic class BaseServiceImpl implements BaseService nn @Resourcen private SessionFactory sessionFactory;n npublic SessionFactory getSessionFactory() n return sessionFactory;n nn public void setSessionFactory(SessionFactory sessionFactory) n this.sessionFactory = sessionFactory;n nn/**n * @author sunlongn * @param hql:传入的hql,可以带参数?n * @param args: 问号对应的参数数组n */n @Overriden public List getResult(String hql, Object[] args) nn Session session = sessionFactory.getCurrentSession();n System.out.println("执行到创建Session");n Query query = session.createQuery(hql);n System.out.println("执行到Query");n if(args!= null && args.length > 0)n for(int i = 0;ithreadn配置删除或者把值thread改成org.springframework.orm.hibernate3.SpringSessionContextn再次运行却又出现了org.springframework.orm.hibernate3.SpringSessionContext异常整了好久都没搞明白,这是为什么呀?n nnSpring 的配置文件n1.applicationContext.xml nn nn n n n n n n n n n n n n n n n n n nn n n n n nnn n n n n n n n n n n n n n nn n n n nn2.applicationContextbeans.xmlnnn n n n n n n n n n n n n n n n n n n nbaseServiceImpl.javanpackage com.xiaonei.service.baseService;nnimport java.util.List;nnimport javax.annotation.Resource;nnimport org.hibernate.Query;nimport org.hibernate.SessionFactory;nimport org.springframework.stereotype.Service;nimport org.springframework.transaction.annotation.Transactional;nn@Servicenpublic abstract class BaseServiceImpl implements BaseService nn @Resourcen private SessionFactory sessionFactory;n npublic SessionFactory getSessionFactory() n return sessionFactory;n nn public void setSessionFactory(SessionFactory sessionFactory) n this.sessionFactory = sessionFactory;n nn/**n * @author sunlongn * @param hql:传入的hql,可以带参数?n * @param args: 问号对应的参数数组n */n n n @Transactionaln public List getResult(String hql, Object[] args) nSystem.out.println("执行了getResult方法");n Query query = sessionFactory.getCurrentSession().createQuery(hql);n System.out.println("执行到createQuery方法");n if(args!= null && args.length > 0)n for(int i = 0;i calzz, Integer id) n // TODO Auto-generated method stubn return null;n nnnnnnnnnnnn 问答

No Session found for current thread

02-09

在spring3.1整合hibernate4的时候出现No Session found for current thread,求大神看看是什么原因?rn[code=html]HTTP Status 500 - No Session found for current threadrnrntype Exception reportrnrnmessage No Session found for current threadrnrndescription The server encountered an internal error that prevented it from fulfilling this request.rnrnexceptionrnrnorg.hibernate.HibernateException: No Session found for current threadrn org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)rn org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1024)rn com.hrmsys.dao.impl.BaseDaoImpl.getSession(BaseDaoImpl.java:20)rn com.hrmsys.dao.impl.BaseDaoImpl.findByProperty(BaseDaoImpl.java:117)rn com.hrmsys.dao.impl.UserDaoImpl.findByUsernameAndPassword(UserDaoImpl.java:16)rn com.hrmsys.service.impl.UserServiceImpl.validateUser(UserServiceImpl.java:24)rn sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)rn sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)rn java.lang.reflect.Method.invoke(Unknown Source)rn org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)rn org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)rn org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)rn org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)rn org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)rn org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)rn org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)rn org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)rn $Proxy15.validateUser(Unknown Source)rn com.hrmsys.action.UserAction.login(UserAction.java:30)rn sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)rn sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)rn java.lang.reflect.Method.invoke(Unknown Source)rn com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)rn com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)rn org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)rn org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)rn org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)rn org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511)rn org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)rn org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)rn[/code]rn这是applicationContext.xml里的sessionFactory里的代码rn[code=html]rn rn rn rn rn rn rn falsern org.hibernate.dialect.SQLServerDialectrn truern falsern org.springframework.orm.hibernate4.SpringSessionContextrn rn rn rn rn [/code] 论坛

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