1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 1997,2008 Oracle. All rights reserved. 5 * 6 * $Id: RPCExample.java,v 12.7 2008/01/08 20:58:32 bostic Exp $ 7 */ 8 9package db; 10 11import com.sleepycat.db.*; 12import java.io.File; 13import java.io.FileNotFoundException; 14import java.io.OutputStream; 15 16/* 17 * An example of a program configuring a database environment as an RPC client. 18 */ 19public class RPCExample { 20 private static final String progname = "RPCExample"; 21 private static final File DATABASE_HOME = new File("TESTDIR"); 22 23 private static void runApplication(Environment dbenv) 24 throws DatabaseException, FileNotFoundException { 25 26 // Do something interesting... 27 // Your application goes here. 28 DatabaseConfig config = new DatabaseConfig(); 29 config.setAllowCreate(true); 30 config.setType(DatabaseType.BTREE); 31 Database db = dbenv.openDatabase(null, "test.db", null, config); 32 db.close(); 33 } 34 35 private static void setupEnvironment(File home, 36 OutputStream errs) 37 throws DatabaseException, FileNotFoundException { 38 39 // Create an environment object and initialize it for error reporting. 40 EnvironmentConfig config = new EnvironmentConfig(); 41 config.setErrorStream(errs); 42 config.setErrorPrefix(progname); 43 44 // 45 // We want to specify the shared memory buffer pool cachesize, 46 // but everything else is the default. 47 // 48 config.setCacheSize(64 * 1024); 49 50 // Open the environment with full transactional support. 51 config.setAllowCreate(true); 52 config.setInitializeCache(true); 53 config.setTransactional(true); 54 config.setInitializeLocking(true); 55 56 config.setRPCServer("localhost", 0, 0); 57 58 // 59 // open is declared to throw a FileNotFoundException, which normally 60 // shouldn't occur when allowCreate is set. 61 // 62 Environment dbenv = new Environment(home, config); 63 64 try { 65 // Start your application. 66 runApplication(dbenv); 67 } finally { 68 // Close the environment. Doing this in the finally block ensures 69 // it is done, even if an error is thrown. 70 dbenv.close(); 71 } 72 } 73 74 private static void teardownEnvironment(File home, 75 OutputStream errs) 76 throws DatabaseException, FileNotFoundException { 77 78 // Remove the shared database regions. 79 EnvironmentConfig config = new EnvironmentConfig(); 80 81 config.setErrorStream(errs); 82 config.setErrorPrefix(progname); 83 config.setRPCServer("localhost", 0, 0); 84 Environment.remove(home, true, config); 85 } 86 87 public static void main(String[] args) { 88 File home = DATABASE_HOME; 89 90 try { 91 System.out.println("Setup env"); 92 setupEnvironment(home, System.err); 93 94 System.out.println("Teardown env"); 95 teardownEnvironment(home, System.err); 96 } catch (DatabaseException dbe) { 97 System.err.println(progname + ": environment open: " + dbe.toString()); 98 dbe.printStackTrace(System.err); 99 System.exit (1); 100 } catch (FileNotFoundException fnfe) { 101 System.err.println(progname + ": unexpected open environment error " + fnfe); 102 System.exit (1); 103 } 104 } 105 106} 107