title: Calling @Transactional methods from another thread (Runnable)
body: <p>Is there any simple solution to save data into database using JPA in a new thread?</p>
  
  <p>My Spring based web application allows user to manage scheduled tasks. On runtime he can create and start new instances of predefined tasks. I am using spring's TaskScheduler and everything works well.</p>
  
  <p>But I need to save boolean result of every fired task into database. How can I do this?</p>
  
  <p>EDIT:
  I have to generalize my question: I need to call a method on my @Service class from tasks. Because the task result has to be "processed" before saving into database.</p>
  
  <p>EDIT 2:
  Simplified version of my problematic code comes here. When saveTaskResult() is called from scheduler, message is printed out but nothing is saved into db. But whenever I call saveTaskResult() from controller, record is correctly saved into database.</p>
  
  <pre><code>@Service
  public class DemoService {
  
      @Autowired
      private TaskResultDao taskResultDao;
  
      @Autowired
      private TaskScheduler scheduler;
  
      public void scheduleNewTask() {
          scheduler.scheduleWithFixedDelay(new Runnable() {
  
              public void run() {
                  // do some action here
                  saveTaskResult(new TaskResult("result"));
              }
  
          }, 1000L);
      }
  
      @Transactional
      public void saveTaskResult(TaskResult result) {
          System.out.println("saving task result");
          taskResultDao.persist(result);
      }
  
  }
  </code></pre>