title: Java - Approaches to doing transactions on an object
body: <p>I have a simple Java object (not connected to any database) and I would like to have transactional semantics on it. Here is a contrived example:</p>
  
  <pre><code>class MySession {
       public User user;
       public int numLogins;
  }
  
  public void login(MySession session, String user) {
  
      loginToDatabase(user);
  
      session.beginTransaction();
      try {
  
           session.user = user;
  
           sendLoginAcknowledgement(); //Throws some exception if failed
  
           session.numLogins++;
  
           session.commit();
      } catch (Exception e) {
           // Should reset session.user to the previous value
           session.rollback();
           return;
      }
  }
  </code></pre>
  
  <p>What are my options for doing this in Java? Is JTA intended for this purpose? EJB? Is implementing a Memento pattern for the object the simplest option?</p>