Hibernate Session Threading Hibernate Session Threading multithreading multithreading

Hibernate Session Threading


Make your Runnable a Spring bean and add @Transactional annotation over run. You must be warned thou that this asynchronous task won't run in the same transaction as your web request.

And please don't start new thread, use pooling/executor.


Here is a working example on how to use the Hibernate session inside a Runnable:

@Service@Transactionalpublic class ScheduleService {    @Autowired    private SessionFactory      sessionFactory;    @Autowired    private ThreadPoolTaskScheduler     scheduler;    public void doSomething() {        ScheduledFuture sf = scheduler.schedule(new Runnable() {            @Override            public void run() {                SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(scheduler);                final Session session = sessionFactory.openSession();                // Now you can use the session            }        }, new CronTrigger("25 8 * * * *"));    }}

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext() takes a reference to any Spring managed bean, so the scheduler itself is fine. Any other Spring managed bean would work as well.


Do I understand correctly, you want to perform some action in a completely dedicated background thread, right? In that case, I recommend you not accessing the Hibernates OpenSessionInViewFilter and further session logic for that thread at all, because it will, is you correctly noted, run in a decoupled thread and therefore information loaded in the original thread (i.e, the one that dealt with the initial HttpRequest). I think it would be wise to open and close the session yourself within that thread.

Otherwise, you might question why you are running that operation in a separated thread. May be it is sufficient to run the operation normally and present the user with some 'loading' screen in the meantime?