/**
   * Opens new session or returns currently open session for this thread.
   *
   * @return session object.
   * @throws HibernateException if something with session creation goes wrong.
   */
  public static Session openSession() throws HibernateException {
    if (!inited) init();

    Session s = SESSION.get();
    if (s != null) {
      LOG.log(
          Level.WARNING,
          "Openning session more than once from the same thread!",
          new Exception("Dump"));

      s.clear();

      return s;
    } else {
      try {
        lock.lock();
      } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted waiting for session.");
      }

      s = sessionHandler.getSession();
      SESSION.set(s);
    }

    return s;
  }
  /**
   * Performs initialization.
   *
   * @throws HibernateException if problems with Hibernate occur.
   */
  private static synchronized void init() throws HibernateException {

    // Create the SessionFactory
    sessionHandler = SessionHandler.getInstance(System.getProperties());
    inited = true;
  }