📄 client.java
字号:
import org.ozoneDB.ExternalDatabase;/** * $Id: Client.java,v 1.3.2.1 2003/12/21 16:01:22 per_nyfelt Exp $ */public class Client { private Foo foo; private Bar bar; private Delegate delegate; public static void main(String[] args) throws Exception { String url= "ozonedb:remote://localhost:3333"; if (args.length > 0) { url = args[0]; } Client client = new Client(); client.runSample(url); } public void runSample(String url) throws Exception { ExternalDatabase db = ExternalDatabase.openDatabase(url); foo = (Foo)db.objectForName("foo"); if (foo == null) { foo = (Foo)db.createObject(FooImpl.class); } bar = (Bar)db.objectForName("bar"); if (bar == null) { bar = (Bar)db.createObject(BarImpl.class); } delegate = (Delegate) db.objectForName("delegate"); if (delegate == null) { delegate = (Delegate)db.createObject( DelegateImpl.class, new Class[] {Foo.class, Bar.class}, new Object[] {foo, bar} ); } // fisrt a simple test, we expect no issues here foo.setA("hej"); foo.setB("bla"); bar.setC(56); if (checkValues("hej", "bla", 56)) { System.out.println("OK, Simple test, All values set correctly"); } else { System.out.println("Error, Failed to set simple values"); } printValues(); // now lets try to use the delegate for a build update of foo and bar // again no issues are expected delegate.updateFooBar("qwe", "rty", 123); if (checkValues("qwe", "rty", 123)) { System.out.println("OK, Bulk update test, All values set correctly"); } else { System.out.println("Error, Failed to set values using delegate"); } printValues(); // now lets try use values that will cause an exception and roll back try { delegate.updateFooBar("asd", "dfg", 23); } catch (Exception e) { System.out.println("OK, updateFooBarFailed"); } // this would be bad, the roll back failed if (checkValues("asd", "dfg", 23)) { System.out.println("Error, Failed to roll back properly!!!"); // the expected outcome } else if (checkValues("qwe", "rty", 123)){ System.out.println("OK, Bulk update test, All values rolled back correctly"); // something else wierd has happened } else { System.out.println("Error, Failed to roll back properly"); } printValues(); // do it again on updateFooBar2 which does not execute as a transactional unit delegate.updateFooBar2("asd", "dfg", 23); if (checkValues("asd", "dfg", 123)) { System.out.println("OK, only partial roll back (as expected)"); } else if (checkValues("qwe", "rty", 123)){ System.out.println("Error, Bulk update test2, All values rolled back"); } else { System.out.println("Error, unexpected result"); } printValues(); db.close(); } private void printValues() { System.out.println("\t aVal = " + foo.getA() + ", bVal = " + foo.getB() + ", cVal = " + bar.getC()); } private boolean checkValues(String aVal, String bVal, int cVal) { if (aVal.equals((foo.getA())) && (bVal.equals(foo.getB())) && (cVal == bar.getC())) { return true; } else { return false; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -