title: Getting two open session when working with thread in Java
body: <p>I'm working with thread to stored some data in parallel in the database with hibernate. I need to do that because before saving the information to the database I need to run some task that takes several minutes. So I implemented threads.</p>
  
  <p>So, I have to instance of the same class, let say Students, so each student has a unique Code (not the ID) that I generate randomly and in order to make sure that it is unique, before saving that student I check that there is not other student with that code.</p>
  
  <p>The issue is that when I want to check for the second student if the code is unique (the first one was stored correctly) I get </p>
  
  <pre><code>HibernateException: illegally attempted to associate a proxy with two open Sessions
  </code></pre>
  
  <p>when I'm doing the select...
  I'm using Autowired class to instaciate the other classes (Services, Dao, etc) and Runnable for the Thread.</p>
  
  <p>Below you have some code:
  StudentServiceImpl:</p>
  
  <pre><code>@Autowired
  RunSomeTaskService runSomeTaskService;
  @Override
  Transactional
  public Response save(StudentBO[] students, String username) throws Exception {
  ...
  for (StudentBO student : students) {
      Student s = new Student();
      String code = UUID.randomUUID().toString();
      while(this.studentDao.getByCode(code) != null)
          code = UUID.randomUUID().toString();
      s.setCode(code);
      ....
      this.studentDao.save(s);
      runSomeTaskService.setData(s);
      Thread t = new Thread(runSomeTaskService);
      t.start();
  }
  </code></pre>
  
  <p>RunSomeTaskService:</p>
  
  <pre><code>public interface RunSomeTaskService extends Runnable{
      void setData(Student student);
      Response getResponse();
  }
  </code></pre>
  
  <p>RunSomeTaskServiceImpl:</p>
  
  <pre><code>public class RunSomeTaskServiceImpl extends CommonService implements RunSomeTaskService{
  
  Response response;
  Student student;
  @Override
      public void setData(Student student) {
            this.response = new Response();
            this.student = student;
         }
  }
  
  @Override
      public Response getResponse() {
          return this.response;
      }
  
  @Override
      @Transactional
      public void run() {
  //do some process that takes minutes
  this.studentDao.add(this.student);
  this.response = response;
  }
  </code></pre>
  
  <p>StudentDaoImpl:</p>
  
  <pre><code>@Override
      public Object addOrUpdate(Object item) throws Exception {
          sessionFactory.getCurrentSession().saveOrUpdate(item);
          return item;
      }
  
  @Override
      public Object getByCode(String code) throws Exception {
          Query q = sessionFactory.getCurrentSession().createQuery("FROM Student as e WHERE e.code = '" + code + "'");
          return q.uniqueResult();
      }
  </code></pre>
  
  <p>So my questions are:</p>
  
  <p>1 - Is it ok to instantiate RunSomeTaskService with Autowired? I need this becase if I use <code>new RunSomeTaskService()</code>, sessionFactory is null in the dao layer. But maybe in this way is it wrong because I use just one instance for all the students</p>
  
  <p>2 - In order to avoid <code>the proxy with two open Sessions</code> error, I cannot consult the student table inside the <code>for (StudentBO student : students)</code>. Is that ok or is the issue is different?</p>
  
  <p>BTW it's my first time working with thread.</p>