1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title>Managing Databases in Environments</title> 7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 8 <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" /> 9 <link rel="home" href="index.html" title="Getting Started with Berkeley DB" /> 10 <link rel="up" href="DB.html" title="Chapter��7.��Databases" /> 11 <link rel="previous" href="dbErrorReporting.html" title="Error Reporting Functions" /> 12 <link rel="next" href="CoreJavaUsage.html" title="Database Example" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Managing Databases in Environments</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="dbErrorReporting.html">Prev</a>��</td> 22 <th width="60%" align="center">Chapter��7.��Databases</th> 23 <td width="20%" align="right">��<a accesskey="n" href="CoreJavaUsage.html">Next</a></td> 24 </tr> 25 </table> 26 <hr /> 27 </div> 28 <div class="sect1" lang="en" xml:lang="en"> 29 <div class="titlepage"> 30 <div> 31 <div> 32 <h2 class="title" style="clear: both"><a id="CoreEnvUsage"></a>Managing Databases in Environments</h2> 33 </div> 34 </div> 35 <div></div> 36 </div> 37 <p> 38 In 39 40 <span> 41 <a href="Env.html">Database Environments</a>, 42 </span> 43 44 we introduced 45 environments. While environments are not used in the example built in this book, 46 they are so commonly used for a wide class of DB applications that it is 47 necessary to show their basic usage, if only from a completeness perspective. 48 </p> 49 <p> 50 To use an environment, you must first 51 52 open it. At open time, you must identify the directory in 53 which it resides. This directory must exist prior to the open attempt. 54 You can also identify open properties, such as whether the environment can be 55 created if it does not already exist. 56 </p> 57 <p> 58 You will also need to initialize the in-memory cache when you open your environment. 59 </p> 60 <p> 61 For example, to 62 <span>create an environment handle and</span> 63 open an environment: 64 </p> 65 <a id="java_env1"></a> 66 <pre class="programlisting">package db.GettingStarted; 67 68import com.sleepycat.db.DatabaseException; 69import com.sleepycat.db.Environment; 70import com.sleepycat.db.EnvironmentConfig; 71 72import java.io.File; 73import java.io.FileNotFoundException; 74 75... 76 77Environment myEnv = null; 78File envHome = new File("/export1/testEnv"); 79try { 80 EnvironmentConfig envConf = new EnvironmentConfig(); 81 envConf.setAllowCreate(true); // If the environment does not 82 // exist, create it. 83 envConf.setInitializeCache(true); // Initialize the in-memory 84 // cache. 85 86 myEnv = new Environment(envHome, envConf); 87} catch (DatabaseException de) { 88 // Exception handling goes here 89} catch (FileNotFoundException fnfe) { 90 // Exception handling goes here 91} </pre> 92 <p> 93 Once an environment is opened, you can open databases in it. Note that by default databases 94 are stored in the environment's home directory, or relative to that directory if you 95 provide any sort of a path in the database's file name: 96 </p> 97 <a id="java_env2"></a> 98 <pre class="programlisting">package db.GettingStarted; 99 100<b class="userinput"><tt>import com.sleepycat.db.Database; 101import com.sleepycat.db.DatabaseConfig; 102import com.sleepycat.db.DatabaseType;</tt></b> 103import com.sleepycat.db.DatabaseException; 104import com.sleepycat.db.Environment; 105import com.sleepycat.db.EnvironmentConfig; 106 107import java.io.File; 108import java.io.FileNotFoundException; 109 110... 111 112Environment myEnv = null; 113Database myDb = null; 114File envHome = new File("/export1/testEnv"); 115String dbFileName = new String("mydb.db", "UTF-8"); 116 117try { 118 EnvironmentConfig envConf = new EnvironmentConfig(); 119 envConf.setAllowCreate(true); 120 DatabaseConfig dbConfig = new DatabaseConfig(); 121 <b class="userinput"><tt>dbConfig.setAllowCreate(true); 122 dbConfig.setType(DatabaseType.BTREE);</tt></b> 123 124 myEnv = new Environment(envHome, envConf); 125 <b class="userinput"><tt>myDb = myEnv.openDatabase(null, dbFileName, null, dbConfig);</tt></b> 126} catch (DatabaseException de) { 127 // Exception handling goes here 128} catch (FileNotFoundException fnfe) { 129 // Exception handling goes here 130} </pre> 131 <p> 132 When you are done with an environment, you must close it. Before you close an environment, 133 make sure you close any opened databases. 134 </p> 135 <a id="java_env3"></a> 136 <pre class="programlisting">finally { 137 try { 138 if (myDb != null) { 139 myDb.close(); 140 } 141 142 if (myEnv != null) { 143 myEnv.close(); 144 } 145 } catch (DatabaseException de) { 146 // Exception handling goes here 147 } 148} </pre> 149 </div> 150 <div class="navfooter"> 151 <hr /> 152 <table width="100%" summary="Navigation footer"> 153 <tr> 154 <td width="40%" align="left"><a accesskey="p" href="dbErrorReporting.html">Prev</a>��</td> 155 <td width="20%" align="center"> 156 <a accesskey="u" href="DB.html">Up</a> 157 </td> 158 <td width="40%" align="right">��<a accesskey="n" href="CoreJavaUsage.html">Next</a></td> 159 </tr> 160 <tr> 161 <td width="40%" align="left" valign="top">Error Reporting Functions��</td> 162 <td width="20%" align="center"> 163 <a accesskey="h" href="index.html">Home</a> 164 </td> 165 <td width="40%" align="right" valign="top">��Database Example</td> 166 </tr> 167 </table> 168 </div> 169 </body> 170</html> 171