title: Hibernate multiple Sessions with different Users
body: <p>I am building my first Java Application with Hibernate and have a bit of a problem with Hibernate Sessions.</p>
  
  <p>My Problem:
  When a second User logs into the application he overrides the session for the first user -> both are working on the second session now. Although both Users create a new Session when logging in. </p>
  
  <p>My Code:</p>
  
  <p>first I get the current User when the user logs in (LoginDetail.java):</p>
  
  <pre><code>userBean = UserProxy.getInstance().getElementByUser(userBean.getUser(), userBean.getPassword());
  </code></pre>
  
  <p>UserProxy.java:</p>
  
  <pre><code>public static synchronized UserProxy getInstance() {
  
      if (instance == null) {
          instance = new UserProxy();
      }
  
      return instance;
  }
  public UserBean getElementByUser(String user, String password) {
  
      try {
          Iterator&lt;SUsers&gt; iter = s.createQuery("from SUsers where user = '" + user + "' and password = '" + password + "'").list().iterator();
          while (iter.hasNext()) {
              userDB = iter.next();
              currentUser = convertClassToBean(userDB);
              log.debug("aktuell ausgewaehlter char: " + userDB.getId());
          }
  
      } catch (Exception e) {
          log.error(e);
          e.printStackTrace();
      }
  
      currentUser = convertClassToBean(userDB);
  
      return currentUser;
  }
  </code></pre>
  
  <p>My HibernateUtil.java:</p>
  
  <pre><code>public static final ThreadLocal&lt;Session&gt;    session = new ThreadLocal&lt;Session&gt;();
  public static Session currentSession() {
      Session s = session.get();
      // Open a new Session, if this Thread has none yet
      if (s == null) {
          s = sessionFactory.openSession();
  
          session.set(s);
      }
      return s;
  }
  </code></pre>
  
  <p>and the hibernate.cfg.xml:</p>
  
  <pre><code>    &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
      &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
      &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
      &lt;property name="cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/property&gt;
      &lt;property name="show_sql"&gt;true&lt;/property&gt;
      &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;
      &lt;property name="hibernate.connection.isolation"&gt;2&lt;/property&gt;
  </code></pre>
  
  <p>Can anyone give me a hint how to fix this problem?</p>