01: package com.db4o.f1.chapter5;
02:
03: import java.io.*;
04: import com.db4o.*;
05: import com.db4o.f1.*;
06:
07: public class TransactionExample extends Util {
08: public static void main(String[] args) {
09: new File(Util.DB4OFILENAME).delete();
10: ObjectContainer db = Db4o.openFile(Util.DB4OFILENAME);
11: try {
12: storeCarCommit(db);
13: db.close();
14: db = Db4o.openFile(Util.DB4OFILENAME);
15: listAllCars(db);
16: storeCarRollback(db);
17: db.close();
18: db = Db4o.openFile(Util.DB4OFILENAME);
19: listAllCars(db);
20: carSnapshotRollback(db);
21: carSnapshotRollbackRefresh(db);
22: } finally {
23: db.close();
24: }
25: }
26:
27: public static void storeCarCommit(ObjectContainer db) {
28: Pilot pilot = new Pilot("Rubens Barrichello", 99);
29: Car car = new Car("BMW");
30: car.setPilot(pilot);
31: db.set(car);
32: db.commit();
33: }
34:
35: public static void listAllCars(ObjectContainer db) {
36: ObjectSet result = db.get(Car.class);
37: listResult(result);
38: }
39:
40: public static void storeCarRollback(ObjectContainer db) {
41: Pilot pilot = new Pilot("Michael Schumacher", 100);
42: Car car = new Car("Ferrari");
43: car.setPilot(pilot);
44: db.set(car);
45: db.rollback();
46: }
47:
48: public static void carSnapshotRollback(ObjectContainer db) {
49: ObjectSet result = db.get(new Car("BMW"));
50: Car car = (Car) result.next();
51: car.snapshot();
52: db.set(car);
53: db.rollback();
54: System.out.println(car);
55: }
56:
57: public static void carSnapshotRollbackRefresh(ObjectContainer db) {
58: ObjectSet result = db.get(new Car("BMW"));
59: Car car = (Car) result.next();
60: car.snapshot();
61: db.set(car);
62: db.rollback();
63: db.ext().refresh(car, Integer.MAX_VALUE);
64: System.out.println(car);
65: }
66: }
|