Skip to content

Instantly share code, notes, and snippets.

@vvakame
Created February 24, 2011 04:35
Show Gist options
  • Save vvakame/841759 to your computer and use it in GitHub Desktop.
Save vvakame/841759 to your computer and use it in GitHub Desktop.
package net.vvakame.appengine.test.controller.y2011.m02.d24;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.vvakame.appengine.test.model.y2011.m02.d24.Test20110224;
import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Transaction;
public class IndexController extends Controller {
static final Logger logger = Logger.getLogger(IndexController.class
.getName());
@Override
protected Navigation run() throws Exception {
long id = new Date().getTime();
Key key = Datastore.createKey(Test20110224.class, id);
Test20110224 test = new Test20110224();
test.setKey(key);
Datastore.put(test);
test.setText("none");
Datastore.put(test);
try {
sync(key);
logger.log(Level.FINEST, "sync");
} catch (Exception e) {
logger.log(Level.WARNING, "sync", e);
}
printData(key);
test.setText("none");
Datastore.put(test);
try {
syncAndAsyncCommit(key);
logger.log(Level.FINEST, "syncAndAsyncCommit");
} catch (Exception e) {
logger.log(Level.WARNING, "syncAndAsyncCommit", e);
}
printData(key);
test.setText("none");
Datastore.put(test);
try {
async(key);
logger.log(Level.FINEST, "async");
} catch (Exception e) {
logger.log(Level.WARNING, "async", e);
}
printData(key);
test.setText("none");
Datastore.put(test);
try {
asyncAndAsyncCommit(key);
logger.log(Level.FINEST, "asyncAndAsyncCommit");
} catch (Exception e) {
logger.log(Level.WARNING, "asyncAndAsyncCommit", e);
}
printData(key);
test.setText("none");
Datastore.put(test);
try {
asyncAndTouch(key);
logger.log(Level.FINEST, "asyncAndTouch");
} catch (Exception e) {
logger.log(Level.WARNING, "asyncAndTouch", e);
}
printData(key);
test.setText("none");
Datastore.put(test);
try {
asyncAndAsyncCommitTouch(key);
logger.log(Level.FINEST, "asyncAndAsyncCommitTouch");
} catch (Exception e) {
logger.log(Level.WARNING, "asyncAndAsyncCommitTouch", e);
}
printData(key);
response.getWriter().println("finish.");
response.flushBuffer();
return null;
}
void sync(Key key) {
Transaction tx1;
tx1 = Datastore.beginTransaction();
Test20110224 test1 = Datastore.get(tx1, Test20110224.class, key);
test1.setText("test1");
Transaction tx2;
tx2 = Datastore.beginTransaction();
Test20110224 test2 = Datastore.get(tx2, Test20110224.class, key);
test2.setText("test2");
Datastore.put(tx1, test1);
Datastore.put(tx2, test2);
tx1.commit();
tx2.commit();
}
void syncAndAsyncCommit(Key key) {
Transaction tx1;
tx1 = Datastore.beginTransaction();
Test20110224 test1 = Datastore.get(tx1, Test20110224.class, key);
test1.setText("test1");
Transaction tx2;
tx2 = Datastore.beginTransaction();
Test20110224 test2 = Datastore.get(tx2, Test20110224.class, key);
test2.setText("test2");
Datastore.put(tx1, test1);
Datastore.put(tx2, test2);
tx1.commit();
tx2.commitAsync();
}
void async(Key key) throws InterruptedException {
Transaction tx1;
tx1 = Datastore.beginTransaction();
Test20110224 test1 = Datastore.get(tx1, Test20110224.class, key);
test1.setText("test1");
Transaction tx2;
tx2 = Datastore.beginTransaction();
Test20110224 test2 = Datastore.get(tx2, Test20110224.class, key);
test2.setText("test2");
Datastore.put(tx1, test1);
Datastore.putAsync(tx2, test2);
tx1.commit();
Thread.sleep(2000);
tx2.commit();
}
void asyncAndAsyncCommit(Key key) throws InterruptedException {
Transaction tx1;
tx1 = Datastore.beginTransaction();
Test20110224 test1 = Datastore.get(tx1, Test20110224.class, key);
test1.setText("test1");
Transaction tx2;
tx2 = Datastore.beginTransaction();
Test20110224 test2 = Datastore.get(tx2, Test20110224.class, key);
test2.setText("test2");
Datastore.put(tx1, test1);
Datastore.putAsync(tx2, test2);
tx1.commit();
Thread.sleep(2000);
tx2.commitAsync();
}
void asyncAndTouch(Key key) throws InterruptedException, ExecutionException {
Transaction tx1;
tx1 = Datastore.beginTransaction();
Test20110224 test1 = Datastore.get(tx1, Test20110224.class, key);
test1.setText("test1");
Transaction tx2;
tx2 = Datastore.beginTransaction();
Test20110224 test2 = Datastore.get(tx2, Test20110224.class, key);
test2.setText("test2");
Datastore.put(tx1, test1);
Future<Key> future = Datastore.putAsync(tx2, test2);
tx1.commit();
Thread.sleep(2000);
tx2.commit();
future.get();
}
void asyncAndAsyncCommitTouch(Key key) throws InterruptedException,
ExecutionException {
Transaction tx1;
tx1 = Datastore.beginTransaction();
Test20110224 test1 = Datastore.get(tx1, Test20110224.class, key);
test1.setText("test1");
Transaction tx2;
tx2 = Datastore.beginTransaction();
Test20110224 test2 = Datastore.get(tx2, Test20110224.class, key);
test2.setText("test2");
Datastore.put(tx1, test1);
Future<Key> future = Datastore.putAsync(tx2, test2);
tx1.commit();
Thread.sleep(2000);
tx2.commitAsync();
future.get();
}
void printData(Key key) {
Test20110224 test = Datastore.get(Test20110224.class, key);
logger.log(Level.FINEST, test.getText());
}
}
おっけー
syncAndAsyncCommit
asyncAndAsyncCommit
asyncAndAsyncCommitTouch
だめ
sync
async
asyncAndTouch
値は全部"test1"になった。当たり前だけど。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment