1 21 package com.db4o.test; 22 23 import java.io.*; 24 25 import com.db4o.*; 26 import com.db4o.tools.*; 27 28 public class ObjectNotStorable implements Runnable { 29 30 private static final String FILE = "notStorable.yap"; 31 private static boolean throwException = false; 32 33 private String name; 34 35 private ObjectNotStorable(String name){ 36 if(throwException){ 37 throw new RuntimeException (); 38 } 39 this.name = name; 40 } 41 42 public static void main(String [] args) { 43 throwException = false; 44 new ObjectNotStorable(null).run(); 45 } 46 47 public void run(){ 48 new File(FILE).delete(); 49 Db4o.configure().exceptionsOnNotStorable(true); 50 run1(); 51 } 52 53 private void run1(){ 54 try{ 55 setExc(); 56 }catch(Exception e){ 57 e.printStackTrace(); 58 } 59 try{ 60 getExc(); 61 }catch(Exception e){ 62 e.printStackTrace(); 63 } 64 } 65 66 private static void setOK(){ 67 throwException = false; 68 ObjectContainer con = Db4o.openFile(FILE); 69 ObjectNotStorable ons = new ObjectNotStorable("setOK"); 70 con.set(ons); 71 con.close(); 72 } 73 74 private static void setExc(){ 75 ObjectContainer con = Db4o.openFile(FILE); 76 throwException = false; 77 ObjectNotStorable ons = new ObjectNotStorable("setExc"); 78 throwException = true; 79 con.set(ons); 80 con.close(); 81 } 82 83 private static void getOK(){ 84 throwException = false; 85 ObjectContainer con = Db4o.openFile(FILE); 86 ObjectSet set = con.get(new ObjectNotStorable(null)); 87 while(set.hasNext()){ 88 Logger.log(con, set.next()); 89 } 90 con.close(); 91 } 92 93 private static void getExc(){ 94 throwException = true; 95 ObjectContainer con = Db4o.openFile(FILE); 96 ObjectSet set = con.get(null); 97 while(set.hasNext()){ 98 Logger.log(con, set.next()); 99 } 100 con.close(); 101 102 } 103 } 104 | Popular Tags |