title: Hibernate deadlock
body: <p>Having a deadlock in webservice + hibernate... No idea why.</p>
  
  <p>I have the following webservice code (core code of the function)</p>
  
  <pre><code>public String createImageRecipe(...)
  {
      Session session = ICDBHibernateUtil.getSessionFactory().getCurrentSession();
      try 
      {
          session.beginTransaction();
  
          User user = (User) session.load(User.class, userid);
  
          // Create the recipe
          Recipe recipe = new Recipe();
          ...
          ...
          ...
  
          session.save(recipe);
          session.save(user);
  
          ...
          ...
          ...
  
          session.update( recipe );
  
          // Add new entry to activity log
          Activitylog activityLog = new Activitylog(  user, 
                                                      (byte) ActivityTypeEnum.USER_SHARED_RECIPE.ordinal(), 
                                                      new Date() );
          activityLog.setRecipe(recipe);
          TimelineProcessor.saveTimeline( activityLog, null, true );
  
          session.getTransaction().commit();
  
          endMeasurement();
  
          return recipe.getRecipeid() + "," + recipe.getImageRecipeUrl();
      } 
      catch (RuntimeException e) 
      {
          ICDBHibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
          throw e;
      }
  }
  </code></pre>
  
  <p>Now the timeline thread (TimelineProcessor) does the following (after getting the message):</p>
  
  <pre><code>    Session session = ICDBHibernateUtil.getTimelineSessionFactory().openSession();
      try {
          session.getTransaction().begin();
  
          ...
          ...
          ...
  
          TimelineId tlId = new TimelineId();
          tlId.setUserId(userId);
  
          User u = new User();
          u.setUserid(userId);
  
          Timeline tl = new Timeline(tlId, timelineData.getActivitylog(), u);
          session.save(tl);
  
          session.getTransaction().commit();
          session.close();
      } catch (Exception e) {
          logger.error("Error while processing timeline :: ", e);
          session.getTransaction().rollback();
      }
  }
  </code></pre>
  
  <p>Exceptions from log:
  Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)</p>
  
  <p>org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
      at org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:435)</p>
  
  <p>ERROR com.icdb.TimelineProcessor  - processTimeline - Error while processing timeline :: 
   org.hibernate.exception.LockAcquisitionException: could not insert: [com.icdb.data.Activitylog]</p>
  
  <p>Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)</p>
  
  <p>ERROR com.icdb.TimelineProcessor  - processTimeline - Error while processing timeline :: 
   org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions</p>
  
  <p>Will really appriciate help...
  Thanks a lot</p>