title: Hibernate Delayed Write
body: <p>I am wondering if there is a possibility of hibernate delaying its writes to the DB. I have hibernate configured for mysql. Scenarios I hope to support are <code>80%</code> reads and <code>20%</code> writes. So I do not want to optimize my writes, I rather have the client wait until the DB has been written to, than to return a bit earlier. My tests currently have 100 client in parallel, the cpu does  sometimes max out. I need this flush method to write to DB immediately and return only when the data is written.</p>
  
  <p>On my client side, I send a write request and then a read request, but the read request sometimes returns null. I suspect hibernate is not writing to db immediately.</p>
  
  <pre><code>public final ThreadLocal    session = new ThreadLocal();
  
  public Session currentSession() {
      Session s = (Session) session.get();
      // Open a new Session, if this thread has none yet
      if (s == null || !s.isOpen()) {
          s = sessionFactory.openSession();
          // Store it in the ThreadLocal variable
          session.set(s);
      }
      return s;
  }
  public synchronized void flush(Object dataStore) throws DidNotSaveRequestSomeRandomError {
      Transaction txD;
      Session session;
      session = currentSession();
      txD = session.beginTransaction();
      session.save(dataStore);
      try {
          txD.commit();
      } catch (ConstraintViolationException e) {
          e.printStackTrace();
          throw new DidNotSaveRequestSomeRandomError(dataStore, feedbackManager);
      } catch (TransactionException e) {
          log.debug("txD state isActive" + txD.isActive() + " txD is participating" + txD.isParticipating());
          log.debug(e);
      } finally {
          // session.flush();
          txD = null;
          session.close();
      }
      // mySession.clear();
  }
  </code></pre>