简体   繁体   English

我没有在Tomcat中的Web应用程序的线程中获得Hibernate的会话

[英]I don't get session for Hibernate in thread for an web application in Tomcat

I have an web application with Tomcat. 我有一个使用Tomcat的Web应用程序。 I am using some Threads than execute in the Controller. 我正在使用一些线程,而不是在Controller中执行。 Inside this Thread I execute others Threads. 在此线程中,我执行其他线程。

This threads need to have a session with Hibernate , but it's not working when I execute some query, although I open a new session in the "run" method of each thread. 该线程需要与Hibernate进行会话,但是当我执行某些查询时,尽管我在每个线程的“运行”方法中打开了一个新会话,但该线程不起作用。

When I execute this code outsite of a Tomcat, it works, like a simple java main. 当我在Tomcat外部执行此代码时,它就像一个简单的Java main一样起作用。 So, it looks like it's something I do wrong with Tomcat. 因此,看起来我对Tomcat做了错。 I guess it could be because the OpenSessionInViewFilter. 我猜可能是因为OpenSessionInViewFilter。 I get an error about the current thread doesn't have a session. 我收到有关当前线程没有会话的错误。

The exception that I get is: 我得到的例外是:

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:941)

My web.xml 我的web.xml

<filter>
        <filter-name>sessionView</filter-name>
        <filter-class>
            org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>sessionView</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

My Controller 我的控制器

 @Override
    protected ModelAndView onSubmit(final HttpServletRequest request,
            final HttpServletResponse response, final Object command, final BindException errors)
            throws Exception {          
        MyThread thread = new ExecuteETLThread();
        thread.start();
        ...
    }

MyThread: 我的线程:

public final class MyThread extends AbstracThread {

 @Override
    public void runThread() {
        try {
            //I get an exception here. I just put this senstence here for checking.
            context.getConfigurationDAO().get("A");            
            //Normally, I open more threads here that they connect with hibernate and fail.
            //otherThread.start();
        } catch (final Exception e) {
            LOG.error(e);
        }
    }

AbstractThread: 抽象线程:

public abstract class AbstractThread extends Thread {
    @Override
    public final void run() {
        try {
            OpenSessionInView.openSession();
            runThread();
        } catch (final Exception e) {
            LOG.error("Error running BidoopThread", e);
        } finally {
            OpenSessionInView.closeSession();
        }
    }
    public abstract void runThread();
}

To open the session with Hibernate. 与Hibernate打开会话。

public final class OpenSessionInView {
    public static void openSession() {
        final SessionFactory sf = Factory.getSessionFactory();

        final Session session = sf.openSession();
        session.setFlushMode(FlushMode.COMMIT);
        TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
    }

    public static void closeSession() {
        final SessionFactory sf = Factory.getSessionFactory();
        final SessionHolder sessionHolder =
                (SessionHolder) TransactionSynchronizationManager.unbindResource(sf);
        SessionFactoryUtils.closeSession(sessionHolder.getSession());
    }

Both OpenSessionInViewFilter and spring's delegate for EntityManager use ThreadLocal to store real EntityManager implementation. EntityManager的OpenSessionInViewFilter和spring的委托都使用ThreadLocal来存储实际的EntityManager实现。 And they bind an EntityManager to this ThreadLocal each time request being initiated, in a thread of this request. 并且,每次在该请求的线程中发起请求时,它们都会将EntityManager绑定到此ThreadLocal。 So EM won't be bound to the thread you create manually. 因此,EM不会绑定到您手动创建的线程。 You should observe your stack trace and figure out where does spring look for EM (or session). 您应该观察堆栈跟踪并找出spring在哪里寻找EM(或会话)。 According to stack trace you provided, learn the SpringSessionContext to find a way to bind session manually. 根据您提供的堆栈跟踪,学习SpringSessionContext来找到一种手动绑定会话的方法。 Also you need to perform some work to clean up session as thread completes. 另外,您需要执行一些工作以在线程完成时清理会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tomcat Web应用程序线程转储 - Tomcat Web Application Thread Dump 在Debian上运行的Tomcat上部署Hibernate Web应用程序 - Deploying Hibernate Web Application on Tomcat running on Debian 在 jetty 和 tomcat 上运行 spring hibernate web 应用程序 - running a spring hibernate web application on jetty and tomcat Spring + Hibernate不会自动打开会话 - Spring + Hibernate don't open automatically the session 为什么我没有从这个简单的Java网络爬虫应用程序获得任何结果? - Why I don't get any results from this simple java web-crawler application? Web应用程序的Hibernate会话连接关系 - Hibernate session connection relationship for Web application 我根本没有获得线程同步 - I don't get thread synchronization much at all 当Tomcat无法启动Spring / Hibernate Web应用程序时,如何找到错误? - How can I find the error when Tomcat fails to start my Spring/Hibernate web application? 如何从运行生产Tomcat Web应用程序获取线程调试信息? - How to get thread debug information from running production Tomcat web application? 在Web应用程序中,我正在使用休眠模式。 这样我什么时候打开和关闭会话工厂 - In web application I am using hibernate. In that when do I Open and Close the Session factory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM