Package com.jeck.microblogging.utils

Source Code of com.jeck.microblogging.utils.StoreServiceDAOT$Transact

package com.jeck.microblogging.utils;

import java.util.ConcurrentModificationException;
import java.util.logging.Logger;

import com.googlecode.objectify.ObjectifyOpts;

public class StoreServiceDAOT extends StoreServiceDAO {
  /** */
  private static final Logger log = Logger.getLogger(StoreServiceDAOT.class.getName());
 
 
  public StoreServiceDAOT(){
    super(new ObjectifyOpts().setSessionCache(true).setBeginTransaction(true));
  }
 
  public StoreServiceDAOT(ObjectifyOpts opts){
    super(opts.setBeginTransaction(true));
  }
 
  /** Alternate interface to Runnable for executing transactions */
  public static interface Transactable
  {
    void run(StoreServiceDAOT daot);
  }
 
  /**
   * Provides a place to put the result too.  Note that the result
   * is only valid if the transaction completes successfully; otherwise
   * it should be ignored because it is not necessarily valid.
   */
  abstract public static class Transact<T> implements Transactable
  {
    protected T result;
    public T getResult() { return this.result; }
  }
 
  /** Create a default DAOT and run the transaction through it */
  public static void runInTransaction(Transactable t)
  {
    StoreServiceDAOT daot = new StoreServiceDAOT();
    daot.doTransaction(t);
  }
 
  /**
   * Run this task through transactions until it succeeds without an optimistic
   * concurrency failure.
   */
  public static void repeatInTransaction(Transactable t)
  {
    while (true)
    {
      try
      {
        runInTransaction(t);
        break;
      }
      catch (ConcurrentModificationException ex)
      {
          log.warning("Optimistic concurrency failure for " + t + ": " + ex);
      }
    }
  }
 
  /**
   * Executes the task in the transactional context of this DAO/ofy.
   */
  public void doTransaction(final Runnable task)
  {
    this.doTransaction(new Transactable() {
      @Override
      public void run(StoreServiceDAOT daot)
      {
        task.run();
      }
    });
  }

  /**
   * Executes the task in the transactional context of this DAO/ofy.
   */
  public void doTransaction(Transactable task)
  {
    try
    {
      task.run(this);
      ofy().getTxn().commit();
    }
    finally
    {
      if (ofy().getTxn().isActive())
        ofy().getTxn().rollback();
    }
   }
}
TOP

Related Classes of com.jeck.microblogging.utils.StoreServiceDAOT$Transact

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.