package com.db4o.samples;

import com.db4o.*;
import com.db4o.lib.Logger;

public class S20_Better_Not
{
	public static void main(String[] args)
	{
		Logger.log("*** S20 Things you don't do");
		ObjectContainer db = Db4o.openFile("samples.yap");
		
		
		// Don't open a database file a second time.
		ObjectContainer concurrent = Db4o.openFile("samples.yap");
		concurrent.close();
		
		
		// Classes need a 
		// public default constructor (no parameters)
		// to be stored.
		NoPublicDefaultConstructor noConstructor = null;
		try{
			noConstructor = new NoPublicDefaultConstructor(true);
			db.set(noConstructor);
		}
		catch(Exception e){
			Logger.log("No public default Constructor on class NoPublicDefaultConstructor");
		}
		
		
		db.close();
		// Note we are shutting down the database here.
		
		
		try{
			
			
			db.get(new Company());
			// Don't access the database after it is down.
			
			
		}
		catch(Exception e){
			Logger.log("If you access the database after calling down():");
			Logger.log(e.getClass().getName());
			Logger.log(e.getMessage());
		}
		
		
		db = Db4o.openFile("samples.yap");
		// Note that the database is up again.
		
		
		Logger.log("If you try to get objects with no public constructor:");
		Logger.log(db, db.get(noConstructor).next());
		// We had to continue this example after down() and up()
		// otherwise the object loads fine from the cache.
		
		
		Logger.log("If you forget to call db.down() before you loose the reference to your Db4o:");
		
		// Don't forget to call db.down()
		db = null;
		System.gc();
		
		
		// More suggestions would be appreciated.
		// Please mail them to betternot@db4o.com
	}
}