Sample 1 - basic concepts [Home] [Previous page] [Next Page]
Before you create and manipulate persistent objects with ObjectStore PSE, you must first :
- create a session
- create or open a database
Creating a session
A session is the context in which PSE databases are created or opened and transactions can be executed. In a single Java VM proces PSE allows one session at a time. Separate Java VM can each run their own session at the same time. Only one transaction can exist at a time in a session. To create a session you call the Session constructor and specify the host and properties :
public static Session create(String host,java.util.Properties properties)PSE ignores the first parameter, you can specify null. The second parameter specifies null or property list (for example - a weak reference to an object that does not prevent the object being garbage collected by the Java VM's garbage collector, to control debugging output and the level, to specify how many newly created strings PSE maintain in the string pool for the current session ,... ) A session remains active until your application or PSE terminates it. After a session is terminated, it is never used again. You can however create a new session.
At any given time, an active session has zero or more associated threads. Any number can join a session. But each thread can belong to only one session at a time. To explicitly join a thread to a session, you can call the following method:
Session.join()Example :
/* The active session for this Sample1 */ private static Session session;/* Create a session and join this thread to the new session. */ session = Session.create(null, null); session.join();
Creating and opening a database
Before you begin creating persistent objects, you must create a database to hold the objects. Then you open the database each time you read or modify the objects. To create a database, you call the static Create method on the Database class and specify the database name and an access mode:
public static Database create(String name, int fileMode)The first parameter specifies the path name of a file. In PSE you must end this name with extension .odb. The second parameter specifies the access mode for the database. For each database you create, PSE creates three files :
- the first file has the path name you specify. Path name must be unique and must end with .odb.
- the second one has the same name but extension .odt.
- the third file also has the same name but extension .odf.
The .odb, .odf and .odt files together make up the database.
To open an existing database , you can call the static Open method:
public static Database open(String name, int openMode)The meanings of the parameters are the same as for the creating the database.
PSE provides constants that you can specify for the openMode parameter:
- ObjectStore.UPDATE to read and modify a database
- ObjectStore.READONLY to read but no modify a database
A PSE application that uses one thread in one session can have only one database opened at a time. The application must close the database before it can open another one.
Example :
/** Create or open the database.*/ public static void open(String dbName) { dbName = dbName + ".odb"; /* Open the database or create a new one if necessary. */ try { db = Database.open(dbName,ObjectStore.OPEN_UPDATE); System.out.println(" Database " + dbNameGlobal + " was opened."); OpenedDatabase = dbNameGlobal; }catch (DatabaseNotFoundException e) { db = Database.create(dbName, ObjectStore.ALL_READ | ObjectStore.ALL_WRITE); System.out.println(" Database " + dbNameGlobal + " was created."); OpenedDatabase = dbNameGlobal; } catch (DatabaseLockedException e) { System.out.println(" The database is locked by another user."); return; } catch (DatabaseException e) { System.out.println(" Not possible create or open more then one database."); } }To close a database, call the Close method on the instance of the Database:
public void close(boolean retainAsTransient)Example :
/** Close the database. */ public static void close() { try { db.close(); System.out.println(" Database " + OpenedDatabase + " was closed."); } catch (DatabaseNotOpenException e) { System.out.println(" There is not opened any database."); } catch (NullPointerException e) { System.out.println(" There is not opened any database."); } return; }If you do not close a database, PSE closes it when you shutdown PSE.
When your application opens a database, PSE creates a directory that functions as a lock against other Java VM processes. The name of the directory is database_name.odx. PSE maintains this directory in the same directory as your database. When your application closes the database or terminates the session that opened the database, PSE deletes the .odx directory, which releases the lock.
The application Sample1 is a simply example of creating a session, creating, opening and closing the database.
[Home] [Previous page] [Next Page]