openSession出错:蓝萝卜blu Hibernate4中使用getCurrentSession报Could not obtain transaction-synchronized Sessio

架个spring4+hibernate4的demo,dao层直接注入的sessionFactory,然后用getCurrentSession方法获取session,然后问题来了,直接报错:

Could not obtain transaction-synchronized Session for current thread

提示无法获取当前线程的事务同步session,略微奇怪,这和事务有什么关系..然后百度一下有人说改成用openSession方法就好了,那我又百度了一下这2个方法的区别:

(1)openSession每次打开都是新的Session,所以多次获取的Session实例是不同的,并且需要人为的调用close方法进行Session关闭。
(2)getCurrentSession是从当前上下文中获取Session并且会绑定到当前线程,第一次调用时会创建一个Session实例,如果该Session未关闭,后续多次获取的是同一个Session实例;事务提交或者回滚时会自动关闭Sesison,无需人工关闭。

看起来这个getCurrentSession方法的确和事务有点关系.然后我加上事务:

复制代码
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
解决的方式是打开session在web.xml中进行配置:
<!-- 解决 org.hibernate.HibernateException:
             Could not obtain transaction-synchronized Session for current 出错问题,主要问题是获取当前线程的问题获取不到 -->
<filter>
    <filter-name>SpringOpenSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SpringOpenSessionInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
为什么会在测试的时候出现这个问题
因为我们在写代码的时候逻辑的线程是在serviceImpl,现在是在DaoImpl中,ServiceImpl和DaoImpl是2个不同的线程在getCurrentSession()中,
getCurrentSession会从上下文中获取session,并且会绑定在当前线程,第一调用的时候会创建一个Session实例,如果Sesssion未关闭的时候,后续
多次获取的时同一个Session,当事物开启关闭或者回滚的时候就会自动关闭Session,无需手动关闭。
使用openSessionView需要手动关闭。
阅读更多
换一批

Could not obtain transaction-synchronized Session

01-25

excel数据导入数据库的小逻辑,断点调试发现走了dao层的save方法,但是并没有sql语句输出,而且数据库也没有数据。用spring-test测试dao层的save方法报错org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current threadn at org.springframework.orm.hibernate5.SpringSessionContext.currentSession(SpringSessionContext.java:132)n at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:692)n at com.balaiduo.tcm.dao.impl.HibernateDao.getCurrentSession(HibernateDao.java:13)n at com.balaiduo.tcm.test.DaoTest.addEmploy(DaoTest.java:19)n at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)n at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)n at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)n at java.lang.reflect.Method.invoke(Method.java:606)n at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)n at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)n at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)n at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)n at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)n at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)n at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)n at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)n at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)n at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)n at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)n at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)n at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)n at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)n at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)n at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)n at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)n at org.junit.runners.ParentRunner.run(ParentRunner.java:309)n at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)n at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)n at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)n at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)n at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)n at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)n at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

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

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