|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.garret.consus.JDBCObjectRelationalAdapter
This class implements the Consus OO API on top of standard relational database systems.
The tables used by applications should be explicitly created by the programmer using the standard JDBC
protocol. This is the main difference with implementation of PersistentObjectStorage by
ConsusConnection. In the last case, tables are automatically generated from class definitions.
Below is the initialization sequence for a database to be used with JDBCRerlationalAdapter
TypeInfo
table with TID, TableName, ClassName
fields.
OidTable
. This table contains a single column ID
with
a single row and is used to generate new object identifiers.OidTable
table.long
value.VARBINARY
type. Non-persistent object fields
should be represented by their components concatenated with the
JDBCObjectRelationalAdapter.STRUCTURE_FIELDS_SEP
character (by default $
).
Mapping of other Java types to native RDBMS types is up to the programmer. The only requirement
is that JDBC driver will be able to extract/store fields of that object, using
getXXX
and putXXX
methods of java.sql.ResultSet
and
java.sql.PreparedStatament
, where XXX
is the Java type of the field.
Each table should have RID
and TID
fields which are used
to store record and type identifiers. Both identifiers are of the Java int
type.
The Object identifier (OID) is constructed by concatenation of type and record identifiers.
To check whether the database was initialized or not, the programmer should use the
PersistentObjectStorage.isInitialized()
method.
It should be called before opening and so the database should be initialized prior
to invocation of the PersistetnObjectStorage.open()
method.
These rules are illustrated by the following example:
Application classes:
class Address { String country; String city; String street; }; class Person extends Persistent { String name; Address address; Person mother; Person father; Person[] childs; }; Connection con = DriverManager.getConnection(databaseURL); PersistentObjectStorage storage = (con instanceof PersistentObjectStorage) ? (PersistentObjectStorage)con : new JDBCObjectRelationalAdapter(con); if (!storage.isInitialized()) { Statement stmt = con.createStatement(); stmt.executeUpdate("create table TypeInfo (TID int, TableName varchar, ClassName varchar)"); stmt.executeUpdate("create table OidTable (ID int)"); stmt.executeUpdate("insert into OidTable values (1)"); stmt.executeUpdate("create table Person (name varchar primary key," + "address$country varchar," + "address$city varchar," + "address$street varchar," + "mother bigint," + "father bigint," + "childs varbinary)"); stmt.executeUpdate("insert into TypeInfo values (1, 'Person', 'Person')"); } storage.open();Underlying RDBMS should have
Field Summary | |
---|---|
static java.lang.String |
OBJECT_RID_NAME
|
static java.lang.String |
OBJECT_TID_NAME
|
static java.lang.String |
OID_TABLE_COLUMN_NAME
|
static java.lang.String |
OID_TABLE_NAME
|
static java.lang.String |
STRUCTURE_FIELDS_SEP
|
static java.lang.String |
TYPE_CLASS_NAME_FIELD
|
static java.lang.String |
TYPE_ID_FIELD
|
static java.lang.String |
TYPE_TABLE_NAME
|
static java.lang.String |
TYPE_TABLE_NAME_FIELD
|
Constructor Summary | |
---|---|
JDBCObjectRelationalAdapter(java.sql.Connection con)
Constructor of the adapter class |
Method Summary | |
---|---|
void |
becomeObject(Persistent a,
Persistent b)
Exchange references to the objects in the database. |
void |
clearComponents(Persistent obj)
Clear all object fields (to make it possible for GC to collect unused persistent objects). |
void |
clearObjectCache()
Throw all objects from the object cache. |
void |
deleteObject(Persistent obj)
Remove object from the database. |
java.lang.Object |
fetchObject(java.sql.ResultSet cursor)
Fetch the current row of the cursor as a Java object |
java.lang.Object |
getObjectByOid(long oid)
Get object by object identifier. |
int |
getObjectCacheSize()
Poll object cache size |
TableIterator |
getTableIterator(java.lang.String tableName)
Get the iterator for the table. |
long |
insertObject(java.lang.Object obj)
Insert a new object into the database. |
boolean |
isInitialized()
Returns true if the database has already been initialized. |
void |
loadComponents(Persistent obj)
Loads the components of the object. |
void |
loadObject(Persistent obj)
Load stub object or reload object from the database. |
void |
lock()
Lock the database in exclusive mode. |
void |
open()
Opens the persistent object storage. |
void |
storeObject(Persistent obj)
Save object into the database. |
void |
updateObject(java.lang.Object obj,
java.sql.ResultSet cursor)
Fetch the current row of the cursor as a Java object |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static java.lang.String OBJECT_RID_NAME
public static java.lang.String OBJECT_TID_NAME
public static java.lang.String OID_TABLE_COLUMN_NAME
public static java.lang.String OID_TABLE_NAME
public static java.lang.String STRUCTURE_FIELDS_SEP
public static java.lang.String TYPE_CLASS_NAME_FIELD
public static java.lang.String TYPE_ID_FIELD
public static java.lang.String TYPE_TABLE_NAME
public static java.lang.String TYPE_TABLE_NAME_FIELD
Constructor Detail |
---|
public JDBCObjectRelationalAdapter(java.sql.Connection con)
con
- - opened JDBC connectionMethod Detail |
---|
public void becomeObject(Persistent a, Persistent b) throws java.sql.SQLException
becomeObject
in interface PersistentObjectStorage
a
- object which OID will be exchanged with b
objectb
- object which OID will be exchanged with a
object
java.sql.SQLException
public void clearComponents(Persistent obj) throws java.lang.Exception
clearComponents
in interface PersistentObjectStorage
obj
- persitent object which components are cleared
java.lang.Exception
public void clearObjectCache()
PersistentObjectStorage
getObjectCacheSize
or getUsedMemorySize
method) and
if it axceeds some threshold,
call clearObjectCache
method to remove all objects from cache.
Alternativly, progrtammer can call Persistent.unget
method to replace
object with a stub, but this approach is less efficient because usually it throws
away most recently used object.
clearObjectCache
in interface PersistentObjectStorage
public void deleteObject(Persistent obj) throws java.sql.SQLException
deleteObject
in interface PersistentObjectStorage
obj
- deleted persitent object
java.sql.SQLException
public java.lang.Object fetchObject(java.sql.ResultSet cursor) throws java.lang.Exception
fetchObject
in interface PersistentObjectStorage
cursor
- - result set with the current position corresponding to the fetched object
java.lang.Exception
public java.lang.Object getObjectByOid(long oid) throws java.lang.Exception
PersistentObjectStorage
getObjectByOid
in interface PersistentObjectStorage
oid
- - identifier of object to be fetched
java.lang.Exception
public int getObjectCacheSize()
PersistentObjectStorage
getObjectCacheSize
in interface PersistentObjectStorage
public TableIterator getTableIterator(java.lang.String tableName) throws java.sql.SQLException
getTableIterator
in interface PersistentObjectStorage
tableName
- name of th table through which iteration will be performed
java.sql.SQLException
public long insertObject(java.lang.Object obj) throws java.lang.Exception
java.lang.Exception
public boolean isInitialized() throws java.lang.Exception
isInitialized
in interface PersistentObjectStorage
true
if database was already initialized, false
otherwise
java.lang.Exception
public void loadComponents(Persistent obj) throws java.lang.Exception
loadComponents
in interface PersistentObjectStorage
obj
- persistent object which components are loaded
java.lang.Exception
public void loadObject(Persistent obj) throws java.lang.Exception
loadObject
in interface PersistentObjectStorage
obj
- loaded persistent object
java.lang.Exception
public void lock()
lock
in interface PersistentObjectStorage
public void open() throws java.lang.Exception
open
in interface PersistentObjectStorage
java.lang.Exception
public void storeObject(Persistent obj) throws java.lang.Exception
storeObject
in interface PersistentObjectStorage
obj
- stored persitent object
java.lang.Exception
public void updateObject(java.lang.Object obj, java.sql.ResultSet cursor) throws java.lang.Exception
updateObject
in interface PersistentObjectStorage
obj
- - object with the new valuescursor
- - result set with the current position pointing to the updated row
java.lang.Exception
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |