Package com.db4o.internal
Class ObjectContainerSession
- java.lang.Object
-
- com.db4o.internal.ObjectContainerSession
-
- All Implemented Interfaces:
ExtObjectContainer,InternalObjectContainer,ObjectContainerSpec,ObjectContainer,TransientClass
public class ObjectContainerSession extends java.lang.Object implements InternalObjectContainer, TransientClass, ObjectContainerSpec
-
-
Field Summary
Fields Modifier and Type Field Description protected ObjectContainerBase_serverprotected Transaction_transaction
-
Constructor Summary
Constructors Constructor Description ObjectContainerSession(ObjectContainerBase server)ObjectContainerSession(ObjectContainerBase server, Transaction trans)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidactivate(java.lang.Object obj)activates an object with the current activation strategy.voidactivate(java.lang.Object obj, int depth)Activates all members on a stored object to the specified depth.voidbackup(Storage storage, java.lang.String path)backs up a database file of an open ObjectContainer.voidbackup(java.lang.String path)backs up a database file of an open ObjectContainer.voidbackupSync(Storage storage, java.lang.String path)backs up a database file of an open ObjectContainer.voidbackupSync(java.lang.String path)backs up a database file of an open ObjectContainer.voidbind(java.lang.Object obj, long id)binds an object to an internal object ID.Callbackscallbacks()voidcallbacks(Callbacks cb)ClassMetadataclassMetadataForID(int id)ClassMetadataclassMetadataForName(java.lang.String name)ClassMetadataclassMetadataForReflectClass(ReflectClass reflectClass)booleanclose()Closes the ObjectContainer.voidcommit()Commits the running transaction.Config4ImplconfigImpl()Configurationconfigure()returns the Configuration context for this ObjectContainer.ObjectContainerBasecontainer()voiddeactivate(java.lang.Object obj)deactivates an object.voiddeactivate(java.lang.Object obj, int depth)Deactivates a stored object by setting all members to null.voiddelete(java.lang.Object obj)Deletes a stored object permanently from the database.java.lang.Objectdescend(java.lang.Object obj, java.lang.String[] path)returns a member at the specific path without activating intermediate objects.ExtObjectContainerext()Returns an ObjectContainer with extended functionality.<T> QLin<T>from(java.lang.Class<T> clazz)java.lang.ObjectgetByID(long id)returns the stored object for an internal ID.java.lang.ObjectgetByUUID(Db4oUUID uuid)returns a stored object for aDb4oUUID.longgetID(java.lang.Object obj)returns the internal unique object ID.NativeQueryHandlergetNativeQueryHandler()ObjectInfogetObjectInfo(java.lang.Object obj)returns theObjectInfofor a stored object.HandlerRegistryhandlers()Db4oDatabaseidentity()returns the Db4oDatabase object for this ObjectContainer.booleaninCallback()intinstanceCount(ClassMetadata clazz, Transaction trans)booleanisActive(java.lang.Object obj)tests if an object is activated.booleanisCached(long id)tests if an object with this ID is currently cached.booleanisClient()booleanisClosed()tests if this ObjectContainer is closed.booleanisStored(java.lang.Object obj)tests if an object is stored in this ObjectContainer.ReflectClass[]knownClasses()returns all class representations that are known to this ObjectContainer because they have been used or stored.java.lang.Objectlock()returns the main synchronization lock.EventRegistryImplnewEventRegistry()ObjectContaineropenSession()opens a new ObjectContainer on top of this ObjectContainer.java.lang.ObjectpeekPersisted(java.lang.Object object, int depth, boolean committed)returns a transient copy of a persistent object with all members set to the values that are currently stored to the database.voidpurge()unloads all clean indices from memory and frees unused objects.voidpurge(java.lang.Object obj)unloads a specific object from the db4o reference mechanism.Queryquery()Creates a new S.O.D.A.ObjectSetquery(Predicate predicate)Native Query Interface.
Make sure that you include the db4o-nqopt-java.jar and bloat.jar in your classpath when using native queries.<T> ObjectSet<T>query(Predicate<T> predicate, QueryComparator<T> comparator)Native Query Interface.ObjectSetquery(Predicate predicate, java.util.Comparator comparator)Native Query Interface.ObjectSetquery(java.lang.Class clazz)Queries for all instances of a class.ObjectSetqueryByExample(java.lang.Object template)Query-By-Example interface to retrieve objects.GenericReflectorreflector()Return the reflector currently being used by db4objects.voidrefresh(java.lang.Object obj, int depth)refreshs all members on a stored object to the specified depth.voidreleaseSemaphore(java.lang.String name)releases a semaphore, if the calling transaction is the owner.voidrollback()Rolls back the running transaction.booleansetSemaphore(java.lang.String name, int waitForAvailability)attempts to set a semaphore.voidstore(java.lang.Object obj)Stores objects or updates stored objects.voidstore(java.lang.Object obj, int depth)deep update interface to store or update objects.voidstoreAll(Transaction transaction, Iterator4 objects)StoredClassstoredClass(java.lang.Object clazz)returns aStoredClassmeta information object.StoredClass[]storedClasses()returns an array of allStoredClassmeta information objects.java.lang.ObjectsyncExec(Closure4 block)SystemInfosystemInfo()returns theSystemInfofor this ObjectContainer.Transactiontransaction()UpdateDepthProviderupdateDepthProvider()longversion()returns the current transaction serial number.
-
-
-
Field Detail
-
_server
protected final ObjectContainerBase _server
-
_transaction
protected final Transaction _transaction
-
-
Constructor Detail
-
ObjectContainerSession
public ObjectContainerSession(ObjectContainerBase server, Transaction trans)
-
ObjectContainerSession
public ObjectContainerSession(ObjectContainerBase server)
-
-
Method Detail
-
backup
public void backup(java.lang.String path) throws Db4oIOException, DatabaseClosedException, NotSupportedExceptionDescription copied from interface:ExtObjectContainerbacks up a database file of an open ObjectContainer.
While the backup is running, the ObjectContainer can continue to be used. Changes that are made while the backup is in progress, will be applied to the open ObjectContainer and to the backup.
While the backup is running, the ObjectContainer should not be closed.
Note that this is designed to run as a background process: To avoid harming performance of concurrently active database operations, it tries to leave room for other threads as much as possible and thus will be orders of magnitude slower than a regular file copy. If you are looking for a faster backup (that will block all other database operations), useExtObjectContainer.backupSync(Storage, String)instead.
If a file already exists at the specified path, it will be overwritten.
TheStorageused for backup is the one configured for this container.- Specified by:
backupin interfaceExtObjectContainer- Parameters:
path-- Throws:
Db4oIOException- I/O operation failed or was unexpectedly interrupted.DatabaseClosedException- db4o database file was closed or failed to open.NotSupportedException- is thrown when the operation is not supported in current configuration/environment
-
backup
public void backup(Storage storage, java.lang.String path) throws Db4oIOException, DatabaseClosedException, NotSupportedException
Description copied from interface:ExtObjectContainerbacks up a database file of an open ObjectContainer.
While the backup is running, the ObjectContainer can continue to be used. Changes that are made while the backup is in progress, will be applied to the open ObjectContainer and to the backup.
While the backup is running, the ObjectContainer should not be closed.
Note that this is designed to run as a background process: To avoid harming performance of concurrently active database operations, it tries to leave room for other threads as much as possible and thus will be orders of magnitude slower than a regular file copy. If you are looking for a faster backup (that will block all other database operations), useExtObjectContainer.backupSync(Storage, String)instead.
If a file already exists at the specified path, it will be overwritten.
This method is intended for cross-storage backups, i.e. backup from an in-memory database to a file.- Specified by:
backupin interfaceExtObjectContainer- Parameters:
storage- theStorageto be used for backuppath- a fully qualified path- Throws:
Db4oIOException- I/O operation failed or was unexpectedly interrupted.DatabaseClosedException- db4o database file was closed or failed to open.NotSupportedException- is thrown when the operation is not supported in current configuration/environment
-
backupSync
public void backupSync(java.lang.String path) throws Db4oIOException, DatabaseClosedException, NotSupportedExceptionDescription copied from interface:ExtObjectContainerbacks up a database file of an open ObjectContainer. While the backup is running, the ObjectContainer should not be closed.
Note that this blocks all other concurrently active database operations for the full duration of the backup. If you intend to run a backup as a less intrusive background process concurrently with other database operations, useExtObjectContainer.backup(Storage, String)instead.
If a file already exists at the specified path, it will be overwritten.
TheStorageused for backup is the one configured for this container.- Specified by:
backupSyncin interfaceExtObjectContainer- Parameters:
path- a fully qualified path- Throws:
Db4oIOException- I/O operation failed or was unexpectedly interrupted.DatabaseClosedException- db4o database file was closed or failed to open.NotSupportedException- is thrown when the operation is not supported in current configuration/environment
-
backupSync
public void backupSync(Storage storage, java.lang.String path) throws Db4oIOException, DatabaseClosedException, NotSupportedException
Description copied from interface:ExtObjectContainerbacks up a database file of an open ObjectContainer. While the backup is running, the ObjectContainer should not be closed.
Note that this blocks all other concurrently active database operations for the full duration of the backup. If you intend to run a backup as a less intrusive background process concurrently with other database operations, useExtObjectContainer.backup(Storage, String)instead.
If a file already exists at the specified path, it will be overwritten.
This method can be used for cross-storage backups, i.e. backup from an in-memory database to a file.- Specified by:
backupSyncin interfaceExtObjectContainer- Parameters:
storage- theStorageto be used for backuppath- a fully qualified path- Throws:
Db4oIOException- I/O operation failed or was unexpectedly interrupted.DatabaseClosedException- db4o database file was closed or failed to open.NotSupportedException- is thrown when the operation is not supported in current configuration/environment
-
bind
public void bind(java.lang.Object obj, long id) throws InvalidIDException, DatabaseClosedExceptionDescription copied from interface:ExtObjectContainerbinds an object to an internal object ID.
This method uses the ID parameter to load the corresponding stored object into memory and replaces this memory reference with the object parameter. The method may be used to replace objects or to reassociate an object with it's stored instance after closing and opening a database file. A subsequent call tostore(Object)is necessary to update the stored object.
Requirements:
- The ID needs to be a valid internal object ID, previously retrieved withgetID(Object).
- The object parameter needs to be of the same class as the stored object.- Specified by:
bindin interfaceExtObjectContainer- Parameters:
obj- the object that is to be boundid- the internal id the object is to be bound to- Throws:
InvalidIDException- when the provided id is outside the scope of the database IDs.DatabaseClosedException- db4o database file was closed or failed to open.- See Also:
ExtObjectContainer.getID(java.lang.Object)
-
configImpl
public Config4Impl configImpl()
- Specified by:
configImplin interfaceInternalObjectContainer
-
configure
public Configuration configure()
Description copied from interface:ExtObjectContainerreturns the Configuration context for this ObjectContainer.- Specified by:
configurein interfaceExtObjectContainer- Returns:
Configurationthe Configuration context for this ObjectContainer
-
descend
public java.lang.Object descend(java.lang.Object obj, java.lang.String[] path)Description copied from interface:ExtObjectContainerreturns a member at the specific path without activating intermediate objects.
This method allows navigating from a persistent object to it's members in a performant way without activating or instantiating intermediate objects.- Specified by:
descendin interfaceExtObjectContainer- Parameters:
obj- the parent object that is to be used as the starting point.path- an array of field names to navigate by- Returns:
- the object at the specified path or null if no object is found
-
getByID
public java.lang.Object getByID(long id) throws DatabaseClosedException, InvalidIDExceptionDescription copied from interface:ExtObjectContainerreturns the stored object for an internal ID.
This is the fastest method for direct access to objects. Internal IDs can be obtained withgetID(Object). Objects will not be activated by this method. They will be returned in the activation state they are currently in, in the local cache.- Specified by:
getByIDin interfaceExtObjectContainer- Parameters:
id- the internal ID- Returns:
- the object associated with the passed ID or null, if no object is associated with this ID in this ObjectContainer.
- Throws:
DatabaseClosedException- db4o database file was closed or failed to open.InvalidIDException- when an invalid id is passed- See Also:
Why activation?
-
getByUUID
public java.lang.Object getByUUID(Db4oUUID uuid) throws DatabaseClosedException, Db4oIOException
Description copied from interface:ExtObjectContainerreturns a stored object for aDb4oUUID.
This method is intended for replication and for long-term external references to objects. To get aDb4oUUIDfor an object useExtObjectContainer.getObjectInfo(Object)andObjectInfo.getUUID().
Objects will not be activated by this method. They will be returned in the activation state they are currently in, in the local cache.- Specified by:
getByUUIDin interfaceExtObjectContainer- Parameters:
uuid- the UUID- Returns:
- the object for the UUID
- Throws:
DatabaseClosedException- db4o database file was closed or failed to open.Db4oIOException- I/O operation failed or was unexpectedly interrupted.- See Also:
Why activation?
-
getID
public long getID(java.lang.Object obj)
Description copied from interface:ExtObjectContainerreturns the internal unique object ID.
db4o assigns an internal ID to every object that is stored. IDs are guaranteed to be unique within one ObjectContainer. An object carries the same ID in every db4o session. Internal IDs can be used to look up objects with the very fastgetByIDmethod.
Internal IDs will change when a database is defragmented. UseExtObjectContainer.getObjectInfo(Object),ObjectInfo.getUUID()andExtObjectContainer.getByUUID(Db4oUUID)for long-term external references to objects.- Specified by:
getIDin interfaceExtObjectContainer- Parameters:
obj- any object- Returns:
- the associated internal ID or
0, if the passed object is not stored in this ObjectContainer.
-
getObjectInfo
public ObjectInfo getObjectInfo(java.lang.Object obj)
Description copied from interface:ExtObjectContainerreturns theObjectInfofor a stored object.
This method will return null, if the passed object is not stored to this ObjectContainer.- Specified by:
getObjectInfoin interfaceExtObjectContainer- Parameters:
obj- the stored object- Returns:
- the
ObjectInfo
-
identity
public Db4oDatabase identity()
Description copied from interface:ExtObjectContainerreturns the Db4oDatabase object for this ObjectContainer.- Specified by:
identityin interfaceExtObjectContainer- Returns:
- the Db4oDatabase identity object for this ObjectContainer.
-
isActive
public boolean isActive(java.lang.Object obj)
Description copied from interface:ExtObjectContainertests if an object is activated.
isActive returns false if an object is not stored within the ObjectContainer.- Specified by:
isActivein interfaceExtObjectContainer- Parameters:
obj- to be tested- Returns:
- true if the passed object is active.
-
isCached
public boolean isCached(long id)
Description copied from interface:ExtObjectContainertests if an object with this ID is currently cached.- Specified by:
isCachedin interfaceExtObjectContainer- Parameters:
id- the internal ID
-
isClosed
public boolean isClosed()
Description copied from interface:ExtObjectContainertests if this ObjectContainer is closed.- Specified by:
isClosedin interfaceExtObjectContainer- Returns:
- true if this ObjectContainer is closed.
-
isStored
public boolean isStored(java.lang.Object obj) throws DatabaseClosedExceptionDescription copied from interface:ExtObjectContainertests if an object is stored in this ObjectContainer.- Specified by:
isStoredin interfaceExtObjectContainer- Parameters:
obj- to be tested- Returns:
- true if the passed object is stored.
- Throws:
DatabaseClosedException- db4o database file was closed or failed to open.
-
knownClasses
public ReflectClass[] knownClasses()
Description copied from interface:ExtObjectContainerreturns all class representations that are known to this ObjectContainer because they have been used or stored.- Specified by:
knownClassesin interfaceExtObjectContainer- Returns:
- all class representations that are known to this ObjectContainer because they have been used or stored.
-
lock
public java.lang.Object lock()
Description copied from interface:ExtObjectContainerreturns the main synchronization lock.
Synchronize over this object to ensure exclusive access to the ObjectContainer.
Handle the use of this functionality with extreme care, since deadlocks can be produced with just two lines of code.- Specified by:
lockin interfaceExtObjectContainer- Returns:
- Object the ObjectContainer lock object
-
peekPersisted
public java.lang.Object peekPersisted(java.lang.Object object, int depth, boolean committed)Description copied from interface:ExtObjectContainerreturns a transient copy of a persistent object with all members set to the values that are currently stored to the database.
The returned objects have no connection to the database.
With the committed parameter it is possible to specify, whether the desired object should contain the committed values or the values that were set by the running transaction withObjectContainer.store(java.lang.Object).
A possible use case for this feature:
An application might want to check all changes applied to an object by the running transaction.- Specified by:
peekPersistedin interfaceExtObjectContainer- Parameters:
object- the object that is to be cloneddepth- the member depth to which the object is to be instantiatedcommitted- whether committed or set values are to be returned- Returns:
- the object
-
purge
public void purge()
Description copied from interface:ExtObjectContainerunloads all clean indices from memory and frees unused objects.
This method can have a negative impact on performance since indices will have to be reread before further inserts, updates or queries can take place.- Specified by:
purgein interfaceExtObjectContainer
-
purge
public void purge(java.lang.Object obj)
Description copied from interface:ExtObjectContainerunloads a specific object from the db4o reference mechanism.
db4o keeps references to all newly stored and instantiated objects in memory, to be able to manage object identities.
With calls to this method it is possible to remove an object from the reference mechanism.
An object removed with purge(Object) is not "known" to the ObjectContainer afterwards, so this method may also be used to create multiple copies of objects.
purge(Object) has no influence on the persistence state of objects. "Purged" objects can be reretrieved with queries.- Specified by:
purgein interfaceExtObjectContainer- Parameters:
obj- the object to be removed from the reference mechanism.
-
reflector
public GenericReflector reflector()
Description copied from interface:ExtObjectContainerReturn the reflector currently being used by db4objects.- Specified by:
reflectorin interfaceExtObjectContainer- Returns:
- the current Reflector.
-
refresh
public void refresh(java.lang.Object obj, int depth)Description copied from interface:ExtObjectContainerrefreshs all members on a stored object to the specified depth.
If a member object is not activated, it will be activated by this method.
The isolation used is READ COMMITTED. This method will read all objects and values that have been committed by other transactions.- Specified by:
refreshin interfaceExtObjectContainer- Parameters:
obj- the object to be refreshed.depth- the memberdepthto which refresh is to cascade.
-
releaseSemaphore
public void releaseSemaphore(java.lang.String name)
Description copied from interface:ExtObjectContainerreleases a semaphore, if the calling transaction is the owner.- Specified by:
releaseSemaphorein interfaceExtObjectContainer- Parameters:
name- the name of the semaphore to be released.
-
store
public void store(java.lang.Object obj, int depth)Description copied from interface:ExtObjectContainerdeep update interface to store or update objects.
In addition to the normal storage interface,ObjectContainer#store(Object), this method allows a manual specification of the depth, the passed object is to be updated.- Specified by:
storein interfaceExtObjectContainer- Parameters:
obj- the object to be stored or updated.depth- the depth to which the object is to be updated- See Also:
ObjectContainer.store(java.lang.Object)
-
setSemaphore
public boolean setSemaphore(java.lang.String name, int waitForAvailability)Description copied from interface:ExtObjectContainerattempts to set a semaphore.
Semaphores are transient multi-purpose named flags forObjectContainers.
A transaction that successfully sets a semaphore becomes the owner of the semaphore. Semaphores can only be owned by a single transaction at one point in time.
This method returns true, if the transaction already owned the semaphore before the method call or if it successfully acquires ownership of the semaphore.
The waitForAvailability parameter allows to specify a time in milliseconds to wait for other transactions to release the semaphore, in case the semaphore is already owned by another transaction.
Semaphores are released by the first occurrence of one of the following:
- the transaction releases the semaphore withExtObjectContainer.releaseSemaphore(java.lang.String)
- the transaction is closed withObjectContainer.close()
- C/S only: the correspondingObjectServeris closed.
- C/S only: the clientObjectContainerlooses the connection and is timed out.
Semaphores are set immediately. They are independant of callingObjectContainer.commit()orObjectContainer.rollback().
Possible use cases for semaphores:
- prevent other clients from inserting a singleton at the same time. A suggested name for the semaphore: "SINGLETON_" + Object#getClass().getName().
- lock objects. A suggested name: "LOCK_" +getID(Object)
- generate a unique client ID. A suggested name: "CLIENT_" + System.currentTimeMillis().- Specified by:
setSemaphorein interfaceExtObjectContainer- Parameters:
name- the name of the semaphore to be setwaitForAvailability- the time in milliseconds to wait for other transactions to release the semaphore. The parameter may be zero, if the method is to return immediately.- Returns:
- boolean flag
true, if the semaphore could be set or if the calling transaction already owned the semaphore.
false, if the semaphore is owned by another transaction.
-
storedClass
public StoredClass storedClass(java.lang.Object clazz)
Description copied from interface:ExtObjectContainerreturns aStoredClassmeta information object.
There are three options how to use this method.
Any of the following parameters are possible:
- a fully qualified class name.
- a Class object.
- any object to be used as a template.- Specified by:
storedClassin interfaceExtObjectContainer- Parameters:
clazz- class name, Class object, or example object.- Returns:
- an instance of an
StoredClassmeta information object.
-
storedClasses
public StoredClass[] storedClasses()
Description copied from interface:ExtObjectContainerreturns an array of allStoredClassmeta information objects.- Specified by:
storedClassesin interfaceExtObjectContainer
-
systemInfo
public SystemInfo systemInfo()
Description copied from interface:ExtObjectContainerreturns theSystemInfofor this ObjectContainer.
TheSystemInfosupplies methods that provide information about system state and system settings of this ObjectContainer.- Specified by:
systemInfoin interfaceExtObjectContainer- Returns:
- the
SystemInfofor this ObjectContainer.
-
version
public long version()
Description copied from interface:ExtObjectContainerreturns the current transaction serial number.
This serial number can be used to query for modified objects and for replication purposes.- Specified by:
versionin interfaceExtObjectContainer- Returns:
- the current transaction serial number.
-
activate
public void activate(java.lang.Object obj) throws Db4oIOException, DatabaseClosedExceptionDescription copied from interface:ExtObjectContaineractivates an object with the current activation strategy. In regular activation mode the object will be activated to the global activation depth, ( seeCommonConfiguration.activationDepth()) and all configured settings forObjectClass.maximumActivationDepth(int)andObjectClass.maximumActivationDepth(int)will be respected.
In Transparent Activation Mode ( seeTransparentActivationSupport) the parameter object will only be activated, if it does not implementActivatable. All referenced members that do not implementActivatablewill also be activated. AnyActivatableobjects along the referenced graph will break cascading activation.- Specified by:
activatein interfaceExtObjectContainer- Throws:
Db4oIOExceptionDatabaseClosedException
-
activate
public void activate(java.lang.Object obj, int depth) throws Db4oIOException, DatabaseClosedExceptionDescription copied from interface:ObjectContainerActivates all members on a stored object to the specified depth.
See"Why activation"for an explanation why activation is necessary.
Calling this method activates a graph of persistent objects in memory. Only deactivated objects in the graph will be touched: Their fields will be loaded from the database. When called it starts from the given object, traverses all member objects and activates them up to the given depth. The depth parameter is the distance in "field hops" (object.field.field) away from the root object. The nodes at 'depth' level away from the root (for a depth of 3: object.member.member) will be instantiated but not populated with data. Its fields will be null. The activation depth of individual classes can be overruled with the methodsmaximumActivationDepth()andminimumActivationDepth()in theObjectClass interface.- Specified by:
activatein interfaceObjectContainer- Parameters:
obj- the objects to be activated.depth- the object-graphdepthup to which object are activated- Throws:
Db4oIOException- I/O operation failed or was unexpectedly interrupted.DatabaseClosedException- db4o database file was closed or failed to open.- See Also:
Why activation?,Using callbacks
-
close
public boolean close() throws Db4oIOExceptionDescription copied from interface:ObjectContainer- Specified by:
closein interfaceObjectContainer- Returns:
- success - true denotes that the object container was closed, false if it was already closed
- Throws:
Db4oIOException- I/O operation failed or was unexpectedly interrupted.
-
commit
public void commit() throws Db4oIOException, DatabaseClosedException, DatabaseReadOnlyException, UniqueFieldValueConstraintViolationExceptionDescription copied from interface:ObjectContainerCommits the running transaction.
Transactions are back-to-back. A call to commit will start a new transaction immediately.- Specified by:
commitin interfaceObjectContainer- Throws:
Db4oIOException- I/O operation failed or was unexpectedly interrupted.DatabaseClosedException- db4o database file was closed or failed to open.DatabaseReadOnlyException- database was configured as read-only.UniqueFieldValueConstraintViolationException
-
deactivate
public void deactivate(java.lang.Object obj, int depth) throws DatabaseClosedExceptionDescription copied from interface:ObjectContainerDeactivates a stored object by setting all members to null.
Primitive types will be set to their default values. The method has no effect, if the passed object is not stored in the object container.
Be aware that calling may have side effects, which assume that a object is filled with data.
In general you should not deactivate objects, since it makes you application more complex and confusing. To control the scope of objects you should use session containers for your unit of work. UseObjectContainer.ext()openSession()to create a new session.- Specified by:
deactivatein interfaceObjectContainer- Parameters:
obj- the object to be deactivated.depth- the object-graph depth up to which object are deactivated- Throws:
DatabaseClosedException- db4o database file was closed or failed to open.- See Also:
Using callbacks,Why activation?
-
deactivate
public void deactivate(java.lang.Object obj) throws DatabaseClosedExceptionDescription copied from interface:ExtObjectContainerdeactivates an object. Only the passed object will be deactivated, i.e, no object referenced by this object will be deactivated.- Specified by:
deactivatein interfaceExtObjectContainer- Parameters:
obj- the object to be deactivated.- Throws:
DatabaseClosedException
-
delete
public void delete(java.lang.Object obj) throws Db4oIOException, DatabaseClosedException, DatabaseReadOnlyExceptionDescription copied from interface:ObjectContainerDeletes a stored object permanently from the database.
Note that this method has to be called for every single object individually. Delete does not recurs to object members. Primitives, strings and array member types are deleted.
Referenced objects of the passed object remain untouched, unless cascaded deletes areconfigured for the classorfor member fields.
The method has no effect, if the passed object is not stored in the object container.
A subsequent call toObjectContainer.store(Object)with the same object stores the object again in the database.- Specified by:
deletein interfaceObjectContainer- Parameters:
obj- the object to be deleted from the object container- Throws:
Db4oIOException- I/O operation failed or was unexpectedly interrupted.DatabaseClosedException- db4o database file was closed or failed to open.DatabaseReadOnlyException- database was configured as read-only.- See Also:
ObjectClass.cascadeOnDelete(boolean),ObjectField.cascadeOnDelete(boolean),Using callbacks
-
ext
public ExtObjectContainer ext()
Description copied from interface:ObjectContainerReturns an ObjectContainer with extended functionality.
Every ObjectContainer that db4o provides can be casted to an ExtObjectContainer. This method is supplied for your convenience to work without a cast.
The ObjectContainer functionality is split to two interfaces to allow newcomers to focus on the essential methods.- Specified by:
extin interfaceObjectContainer- Returns:
- this, casted to ExtObjectContainer
-
queryByExample
public ObjectSet queryByExample(java.lang.Object template) throws Db4oIOException, DatabaseClosedException
Description copied from interface:ObjectContainerQuery-By-Example interface to retrieve objects.
queryByExample() creates anObjectSetcontaining all objects in the database that match the passed template object.
Calling queryByExample(NULL) returns all objects stored in the database.
Query Evaluation:- All non-null members of the template object are compared against all stored objects of the same class.
- Primitive type members are ignored if they are 0 or false respectively.
- Arrays and collections are evaluated for containment. Differences in length/size() are ignored.
- Specified by:
queryByExamplein interfaceObjectContainer- Parameters:
template- object to be used as an example to find all matching objects.- Returns:
ObjectSetcontaining all found objects.- Throws:
Db4oIOException- I/O operation failed or was unexpectedly interrupted.DatabaseClosedException- db4o database file was closed or failed to open.- See Also:
Why activation?
-
query
public Query query() throws DatabaseClosedException
Description copied from interface:ObjectContainerCreates a new S.O.D.A.Query. NOTE: Soda queries silently ignore invalid specified fields, constraints etc.
Native queriesare the recommended main db4o query interface.- Specified by:
queryin interfaceObjectContainer- Returns:
- a new Query object
- Throws:
DatabaseClosedException- db4o database file was closed or failed to open.
-
query
public ObjectSet query(java.lang.Class clazz) throws Db4oIOException, DatabaseClosedException
Description copied from interface:ObjectContainerQueries for all instances of a class.- Specified by:
queryin interfaceObjectContainer- Parameters:
clazz- the class to query for.- Returns:
- all instances of the given class
- Throws:
Db4oIOException- I/O operation failed or was unexpectedly interrupted.DatabaseClosedException- db4o database file was closed or failed to open.
-
query
public ObjectSet query(Predicate predicate) throws Db4oIOException, DatabaseClosedException
Description copied from interface:ObjectContainerNative Query Interface.
Make sure that you include the db4o-nqopt-java.jar and bloat.jar in your classpath when using native queries. Unless you are using the db4o-all-java5.jar
Native Queries allows typesafe, compile-time checked and refactorable queries, following object-oriented principles. A Native Query expression should return true to include that object in the result and false otherwise.
db4o will attempt to optimize native query expressions and execute them against indexes and without instantiating actual objects. Otherwise db4o falls back and instantiates objects to run them against the given predicate. That is an order of magnitude slower than a optimized native query.
List<Cat> cats = db.query(new Predicate<Cat>() { public boolean match(Cat cat) { return cat.getName().equals("Occam"); } });Summing up the above:
In order to execute a Native Query, you can extend the Predicate class
A class that extends Predicate is required to implement the #match() method, following the native query conventions:
- The name of the method is "#match()".
- The method must be public.
- The method returns a boolean.
- The method takes one parameter.
- The Class (Java) of the parameter specifies the extent.
- The query expression should return true to include a object. False otherwise.- Specified by:
queryin interfaceObjectContainer- Parameters:
predicate- thePredicatecontaining the native query expression.- Returns:
- the query result
- Throws:
Db4oIOException- I/O operation failed or was unexpectedly interrupted.DatabaseClosedException- db4o database file was closed or failed to open.
-
query
public <T> ObjectSet<T> query(Predicate<T> predicate, QueryComparator<T> comparator) throws Db4oIOException, DatabaseClosedException
Description copied from interface:ObjectContainerNative Query Interface. Queries as withObjectContainer.query(com.db4o.query.Predicate), but will sort the result according to the given comperator.- Specified by:
queryin interfaceObjectContainer- Parameters:
predicate- thePredicatecontaining the native query expression.comparator- theQueryComparatorspecifying the sort order of the result- Returns:
- the query result
- Throws:
Db4oIOException- I/O operation failed or was unexpectedly interrupted.DatabaseClosedException- db4o database file was closed or failed to open.
-
rollback
public void rollback() throws Db4oIOException, DatabaseClosedException, DatabaseReadOnlyExceptionDescription copied from interface:ObjectContainerRolls back the running transaction. This only rolls back the changes in the database, but not the state of in memory objects.
Dealing with stale state of in memory objects after a rollback:
- Since in memory objects are not rolled back you probably want start with a clean state.
The easiest way to do this is by creating a new object container:
ObjectContainer.ext().openSession(). - Alternatively you can deactivate objects or
refreshthem to get back to the state in the database. - In case you are using transparent persistence you can use a
rollback strategyto rollback the in memory objects as well.
- Specified by:
rollbackin interfaceObjectContainer- Throws:
Db4oIOException- I/O operation failed or was unexpectedly interrupted.DatabaseClosedException- db4o database file was closed or failed to open.DatabaseReadOnlyException- database was configured as read-only.
- Since in memory objects are not rolled back you probably want start with a clean state.
The easiest way to do this is by creating a new object container:
-
store
public void store(java.lang.Object obj) throws DatabaseClosedException, DatabaseReadOnlyExceptionDescription copied from interface:ObjectContainerStores objects or updates stored objects.
An object not yet stored in the database will be stored. An object already stored in database will be updated.
Updates:- Will update all primitive types, strings and arrays of a object
- References to other object that are already stored will be updated.
- New object members will be stored.
- Referenced object members that are already stored are not updated
themselves. Every object member needs to be updated individually with a
call to store(). Unless a deeper update depth has been configured with on of these options:
Global- orclass-specific update depth,cascde on update for typeorfield.
- Specified by:
storein interfaceObjectContainer- Parameters:
obj- the object to be stored or updated.- Throws:
DatabaseClosedException- db4o database file was closed or failed to open.DatabaseReadOnlyException- database was configured as read-only.- See Also:
ExtObjectContainer#set(object, depth),CommonConfiguration.updateDepth(int),ObjectClass.updateDepth(int),ObjectClass.cascadeOnUpdate(boolean),ObjectField.cascadeOnUpdate(boolean)
-
container
public ObjectContainerBase container()
- Specified by:
containerin interfaceInternalObjectContainer
-
transaction
public Transaction transaction()
- Specified by:
transactionin interfaceInternalObjectContainer
-
callbacks
public void callbacks(Callbacks cb)
- Specified by:
callbacksin interfaceInternalObjectContainer
-
callbacks
public Callbacks callbacks()
- Specified by:
callbacksin interfaceInternalObjectContainer
-
getNativeQueryHandler
public final NativeQueryHandler getNativeQueryHandler()
- Specified by:
getNativeQueryHandlerin interfaceInternalObjectContainer
-
classMetadataForReflectClass
public ClassMetadata classMetadataForReflectClass(ReflectClass reflectClass)
- Specified by:
classMetadataForReflectClassin interfaceInternalObjectContainer
-
classMetadataForName
public ClassMetadata classMetadataForName(java.lang.String name)
- Specified by:
classMetadataForNamein interfaceInternalObjectContainer
-
classMetadataForID
public ClassMetadata classMetadataForID(int id)
- Specified by:
classMetadataForIDin interfaceInternalObjectContainer
-
handlers
public HandlerRegistry handlers()
- Specified by:
handlersin interfaceInternalObjectContainer
-
syncExec
public java.lang.Object syncExec(Closure4 block)
- Specified by:
syncExecin interfaceInternalObjectContainer
-
instanceCount
public int instanceCount(ClassMetadata clazz, Transaction trans)
- Specified by:
instanceCountin interfaceInternalObjectContainer
-
query
public ObjectSet query(Predicate predicate, java.util.Comparator comparator) throws Db4oIOException, DatabaseClosedException
Description copied from interface:ObjectContainerNative Query Interface. Queries as withObjectContainer.query(com.db4o.query.Predicate), but will sort the resultingObjectSetaccording to the givenComparator.- Specified by:
queryin interfaceObjectContainer- Parameters:
predicate- thePredicatecontaining the native query expression.comparator- the java.util.Comparator specifying the sort order of the result- Returns:
- the
ObjectSetreturned by the query. - Throws:
Db4oIOException- I/O operation failed or was unexpectedly interrupted.DatabaseClosedException- db4o database file was closed or failed to open.
-
isClient
public boolean isClient()
- Specified by:
isClientin interfaceInternalObjectContainer
-
storeAll
public void storeAll(Transaction transaction, Iterator4 objects)
- Specified by:
storeAllin interfaceInternalObjectContainer
-
updateDepthProvider
public UpdateDepthProvider updateDepthProvider()
- Specified by:
updateDepthProviderin interfaceInternalObjectContainer
-
openSession
public ObjectContainer openSession()
Description copied from interface:ExtObjectContaineropens a new ObjectContainer on top of this ObjectContainer. The ObjectContainer will have it's own transaction and it's own reference system.- Specified by:
openSessionin interfaceExtObjectContainer- Returns:
- the new ObjectContainer session.
-
newEventRegistry
public EventRegistryImpl newEventRegistry()
- Specified by:
newEventRegistryin interfaceInternalObjectContainer
-
from
public <T> QLin<T> from(java.lang.Class<T> clazz)
-
inCallback
public boolean inCallback()
- Specified by:
inCallbackin interfaceInternalObjectContainer
-
-