title: Spring - Provide Hibernate session for a new Thread using OpenSessionInViewFilter
body: <p>I have a project based on Spring 4.0 and Hibernate 4, specifically, Spring MVC. </p>
  
  <p>Hibernate's session is being created by<code>OpenSessionInViewFilter</code> for each request in controller. </p>
  
  <p>Now, I'm trying to start a new Thread inside controller's method (to do a long process). Apparently, OpenSessionInViewFilter is closing the session after request finishes. Then, when my Thread starts, there isn't session anymore and I get this error: </p>
  
  <p><code>org.hibernate.HibernateException: No Session found for current thread</code></p>
  
  <p>Here is the basis structure of classes, from Controller to my Callable component. IReportService extends Callable.</p>
  
  <p>OBS: I've been tried to use spring's <code>@Async</code> annotation, but it stills not working. I put <code>REQUIRES_NEW</code> on Service trying to get a new transaction, but it failed even changed to NESTED.</p>
  
  <pre><code>@Controller
  @RequestMapping(value = "/action/report")
  @Transactional(propagation = Propagation.REQUIRED)
  public class ReportController {
  
      @Autowired
      private IReportService service;
      private final Map&lt;Long, Future&gt; tasks = new HashMap();
  
      @RequestMapping(value = "/build")
      public String build(@RequestParam Long id) {
  
          ExecutorService executor = Executors.newSingleThreadExecutor();
          Future&lt;StatusProcesso&gt; future = executor.submit(service);
  
          tasks.put(id, future);
  
          return "wait-view";
      }
  
      @RequestMapping(value = "/check", method = RequestMethod.GET)
      public @ResponseBody Map&lt;String, Object&gt; check(@RequestParam Long id) {
  
          String status = null;
  
          try {
              Future foo = this.processos.get(id);
              status = foo.isDone() ? "complete" : "building";
          } catch (Exception e) {
              status = "failed";
          }
  
          return new ResponseBuilder()
                  .add("status", status)
                  .toSuccessResponse();
      }
  
      // Another operations...
  }
  
  
  @Service
  @Transactional(propagation = Propagation.REQUIRES_NEW)
  public class ReportService implements IReportService {
  
      @Autowired
      private IReportDAO dao;
  
      @Override
      public Status call() {
  
          Status status = new Status();
  
          // do long operation and use DAO...
  
          return status;
      }
  }
  </code></pre>