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
_server
protected 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 void
activate(java.lang.Object obj)
activates an object with the current activation strategy.void
activate(java.lang.Object obj, int depth)
Activates all members on a stored object to the specified depth.void
backup(Storage storage, java.lang.String path)
backs up a database file of an open ObjectContainer.void
backup(java.lang.String path)
backs up a database file of an open ObjectContainer.void
backupSync(Storage storage, java.lang.String path)
backs up a database file of an open ObjectContainer.void
backupSync(java.lang.String path)
backs up a database file of an open ObjectContainer.void
bind(java.lang.Object obj, long id)
binds an object to an internal object ID.Callbacks
callbacks()
void
callbacks(Callbacks cb)
ClassMetadata
classMetadataForID(int id)
ClassMetadata
classMetadataForName(java.lang.String name)
ClassMetadata
classMetadataForReflectClass(ReflectClass reflectClass)
boolean
close()
Closes the ObjectContainer.void
commit()
Commits the running transaction.Config4Impl
configImpl()
Configuration
configure()
returns the Configuration context for this ObjectContainer.ObjectContainerBase
container()
void
deactivate(java.lang.Object obj)
deactivates an object.void
deactivate(java.lang.Object obj, int depth)
Deactivates a stored object by setting all members to null.void
delete(java.lang.Object obj)
Deletes a stored object permanently from the database.java.lang.Object
descend(java.lang.Object obj, java.lang.String[] path)
returns a member at the specific path without activating intermediate objects.ExtObjectContainer
ext()
Returns an ObjectContainer with extended functionality.<T> QLin<T>
from(java.lang.Class<T> clazz)
java.lang.Object
getByID(long id)
returns the stored object for an internal ID.java.lang.Object
getByUUID(Db4oUUID uuid)
returns a stored object for aDb4oUUID
.long
getID(java.lang.Object obj)
returns the internal unique object ID.NativeQueryHandler
getNativeQueryHandler()
ObjectInfo
getObjectInfo(java.lang.Object obj)
returns theObjectInfo
for a stored object.HandlerRegistry
handlers()
Db4oDatabase
identity()
returns the Db4oDatabase object for this ObjectContainer.boolean
inCallback()
int
instanceCount(ClassMetadata clazz, Transaction trans)
boolean
isActive(java.lang.Object obj)
tests if an object is activated.boolean
isCached(long id)
tests if an object with this ID is currently cached.boolean
isClient()
boolean
isClosed()
tests if this ObjectContainer is closed.boolean
isStored(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.Object
lock()
returns the main synchronization lock.EventRegistryImpl
newEventRegistry()
ObjectContainer
openSession()
opens a new ObjectContainer on top of this ObjectContainer.java.lang.Object
peekPersisted(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.void
purge()
unloads all clean indices from memory and frees unused objects.void
purge(java.lang.Object obj)
unloads a specific object from the db4o reference mechanism.Query
query()
Creates a new S.O.D.A.ObjectSet
query(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.ObjectSet
query(Predicate predicate, java.util.Comparator comparator)
Native Query Interface.ObjectSet
query(java.lang.Class clazz)
Queries for all instances of a class.ObjectSet
queryByExample(java.lang.Object template)
Query-By-Example interface to retrieve objects.GenericReflector
reflector()
Return the reflector currently being used by db4objects.void
refresh(java.lang.Object obj, int depth)
refreshs all members on a stored object to the specified depth.void
releaseSemaphore(java.lang.String name)
releases a semaphore, if the calling transaction is the owner.void
rollback()
Rolls back the running transaction.boolean
setSemaphore(java.lang.String name, int waitForAvailability)
attempts to set a semaphore.void
store(java.lang.Object obj)
Stores objects or updates stored objects.void
store(java.lang.Object obj, int depth)
deep update interface to store or update objects.void
storeAll(Transaction transaction, Iterator4 objects)
StoredClass
storedClass(java.lang.Object clazz)
returns aStoredClass
meta information object.StoredClass[]
storedClasses()
returns an array of allStoredClass
meta information objects.java.lang.Object
syncExec(Closure4 block)
SystemInfo
systemInfo()
returns theSystemInfo
for this ObjectContainer.Transaction
transaction()
UpdateDepthProvider
updateDepthProvider()
long
version()
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, NotSupportedException
Description copied from interface:ExtObjectContainer
backs 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.
TheStorage
used for backup is the one configured for this container.- Specified by:
backup
in 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:ExtObjectContainer
backs 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:
backup
in interfaceExtObjectContainer
- Parameters:
storage
- theStorage
to 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, NotSupportedException
Description copied from interface:ExtObjectContainer
backs 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.
TheStorage
used for backup is the one configured for this container.- Specified by:
backupSync
in 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:ExtObjectContainer
backs 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:
backupSync
in interfaceExtObjectContainer
- Parameters:
storage
- theStorage
to 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, DatabaseClosedException
Description copied from interface:ExtObjectContainer
binds 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:
bind
in 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:
configImpl
in interfaceInternalObjectContainer
-
configure
public Configuration configure()
Description copied from interface:ExtObjectContainer
returns the Configuration context for this ObjectContainer.- Specified by:
configure
in interfaceExtObjectContainer
- Returns:
Configuration
the Configuration context for this ObjectContainer
-
descend
public java.lang.Object descend(java.lang.Object obj, java.lang.String[] path)
Description copied from interface:ExtObjectContainer
returns 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:
descend
in 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, InvalidIDException
Description copied from interface:ExtObjectContainer
returns 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:
getByID
in 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:ExtObjectContainer
returns a stored object for aDb4oUUID
.
This method is intended for replication and for long-term external references to objects. To get aDb4oUUID
for 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:
getByUUID
in 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:ExtObjectContainer
returns 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 fastgetByID
method.
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:
getID
in 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:ExtObjectContainer
returns theObjectInfo
for a stored object.
This method will return null, if the passed object is not stored to this ObjectContainer.- Specified by:
getObjectInfo
in interfaceExtObjectContainer
- Parameters:
obj
- the stored object- Returns:
- the
ObjectInfo
-
identity
public Db4oDatabase identity()
Description copied from interface:ExtObjectContainer
returns the Db4oDatabase object for this ObjectContainer.- Specified by:
identity
in interfaceExtObjectContainer
- Returns:
- the Db4oDatabase identity object for this ObjectContainer.
-
isActive
public boolean isActive(java.lang.Object obj)
Description copied from interface:ExtObjectContainer
tests if an object is activated.
isActive returns false if an object is not stored within the ObjectContainer.- Specified by:
isActive
in interfaceExtObjectContainer
- Parameters:
obj
- to be tested- Returns:
- true if the passed object is active.
-
isCached
public boolean isCached(long id)
Description copied from interface:ExtObjectContainer
tests if an object with this ID is currently cached.- Specified by:
isCached
in interfaceExtObjectContainer
- Parameters:
id
- the internal ID
-
isClosed
public boolean isClosed()
Description copied from interface:ExtObjectContainer
tests if this ObjectContainer is closed.- Specified by:
isClosed
in interfaceExtObjectContainer
- Returns:
- true if this ObjectContainer is closed.
-
isStored
public boolean isStored(java.lang.Object obj) throws DatabaseClosedException
Description copied from interface:ExtObjectContainer
tests if an object is stored in this ObjectContainer.- Specified by:
isStored
in 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:ExtObjectContainer
returns all class representations that are known to this ObjectContainer because they have been used or stored.- Specified by:
knownClasses
in 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:ExtObjectContainer
returns 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:
lock
in 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:ExtObjectContainer
returns 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:
peekPersisted
in 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:ExtObjectContainer
unloads 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:
purge
in interfaceExtObjectContainer
-
purge
public void purge(java.lang.Object obj)
Description copied from interface:ExtObjectContainer
unloads 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:
purge
in interfaceExtObjectContainer
- Parameters:
obj
- the object to be removed from the reference mechanism.
-
reflector
public GenericReflector reflector()
Description copied from interface:ExtObjectContainer
Return the reflector currently being used by db4objects.- Specified by:
reflector
in interfaceExtObjectContainer
- Returns:
- the current Reflector.
-
refresh
public void refresh(java.lang.Object obj, int depth)
Description copied from interface:ExtObjectContainer
refreshs 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:
refresh
in interfaceExtObjectContainer
- Parameters:
obj
- the object to be refreshed.depth
- the memberdepth
to which refresh is to cascade.
-
releaseSemaphore
public void releaseSemaphore(java.lang.String name)
Description copied from interface:ExtObjectContainer
releases a semaphore, if the calling transaction is the owner.- Specified by:
releaseSemaphore
in 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:ExtObjectContainer
deep 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:
store
in 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:ExtObjectContainer
attempts 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 correspondingObjectServer
is closed.
- C/S only: the clientObjectContainer
looses 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:
setSemaphore
in 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:ExtObjectContainer
returns aStoredClass
meta 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:
storedClass
in interfaceExtObjectContainer
- Parameters:
clazz
- class name, Class object, or example object.- Returns:
- an instance of an
StoredClass
meta information object.
-
storedClasses
public StoredClass[] storedClasses()
Description copied from interface:ExtObjectContainer
returns an array of allStoredClass
meta information objects.- Specified by:
storedClasses
in interfaceExtObjectContainer
-
systemInfo
public SystemInfo systemInfo()
Description copied from interface:ExtObjectContainer
returns theSystemInfo
for this ObjectContainer.
TheSystemInfo
supplies methods that provide information about system state and system settings of this ObjectContainer.- Specified by:
systemInfo
in interfaceExtObjectContainer
- Returns:
- the
SystemInfo
for this ObjectContainer.
-
version
public long version()
Description copied from interface:ExtObjectContainer
returns the current transaction serial number.
This serial number can be used to query for modified objects and for replication purposes.- Specified by:
version
in interfaceExtObjectContainer
- Returns:
- the current transaction serial number.
-
activate
public void activate(java.lang.Object obj) throws Db4oIOException, DatabaseClosedException
Description copied from interface:ExtObjectContainer
activates 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 implementActivatable
will also be activated. AnyActivatable
objects along the referenced graph will break cascading activation.- Specified by:
activate
in interfaceExtObjectContainer
- Throws:
Db4oIOException
DatabaseClosedException
-
activate
public void activate(java.lang.Object obj, int depth) throws Db4oIOException, DatabaseClosedException
Description copied from interface:ObjectContainer
Activates 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:
activate
in interfaceObjectContainer
- Parameters:
obj
- the objects to be activated.depth
- the object-graphdepth
up 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 Db4oIOException
Description copied from interface:ObjectContainer
- Specified by:
close
in 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, UniqueFieldValueConstraintViolationException
Description copied from interface:ObjectContainer
Commits the running transaction.
Transactions are back-to-back. A call to commit will start a new transaction immediately.- Specified by:
commit
in 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 DatabaseClosedException
Description copied from interface:ObjectContainer
Deactivates 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:
deactivate
in 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 DatabaseClosedException
Description copied from interface:ExtObjectContainer
deactivates an object. Only the passed object will be deactivated, i.e, no object referenced by this object will be deactivated.- Specified by:
deactivate
in interfaceExtObjectContainer
- Parameters:
obj
- the object to be deactivated.- Throws:
DatabaseClosedException
-
delete
public void delete(java.lang.Object obj) throws Db4oIOException, DatabaseClosedException, DatabaseReadOnlyException
Description copied from interface:ObjectContainer
Deletes 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 class
orfor 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:
delete
in 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:ObjectContainer
Returns 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:
ext
in interfaceObjectContainer
- Returns:
- this, casted to ExtObjectContainer
-
queryByExample
public ObjectSet queryByExample(java.lang.Object template) throws Db4oIOException, DatabaseClosedException
Description copied from interface:ObjectContainer
Query-By-Example interface to retrieve objects.
queryByExample() creates anObjectSet
containing 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:
queryByExample
in interfaceObjectContainer
- Parameters:
template
- object to be used as an example to find all matching objects.- Returns:
ObjectSet
containing 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:ObjectContainer
Creates a new S.O.D.A.Query
. NOTE: Soda queries silently ignore invalid specified fields, constraints etc.
Native queries
are the recommended main db4o query interface.- Specified by:
query
in 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:ObjectContainer
Queries for all instances of a class.- Specified by:
query
in 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:ObjectContainer
Native 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:
query
in interfaceObjectContainer
- Parameters:
predicate
- thePredicate
containing 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:ObjectContainer
Native Query Interface. Queries as withObjectContainer.query(com.db4o.query.Predicate)
, but will sort the result according to the given comperator.- Specified by:
query
in interfaceObjectContainer
- Parameters:
predicate
- thePredicate
containing the native query expression.comparator
- theQueryComparator
specifying 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, DatabaseReadOnlyException
Description copied from interface:ObjectContainer
Rolls 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
refresh
them to get back to the state in the database. - In case you are using transparent persistence you can use a
rollback strategy
to rollback the in memory objects as well.
- Specified by:
rollback
in 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, DatabaseReadOnlyException
Description copied from interface:ObjectContainer
Stores 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 type
orfield
.
- Specified by:
store
in 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:
container
in interfaceInternalObjectContainer
-
transaction
public Transaction transaction()
- Specified by:
transaction
in interfaceInternalObjectContainer
-
callbacks
public void callbacks(Callbacks cb)
- Specified by:
callbacks
in interfaceInternalObjectContainer
-
callbacks
public Callbacks callbacks()
- Specified by:
callbacks
in interfaceInternalObjectContainer
-
getNativeQueryHandler
public final NativeQueryHandler getNativeQueryHandler()
- Specified by:
getNativeQueryHandler
in interfaceInternalObjectContainer
-
classMetadataForReflectClass
public ClassMetadata classMetadataForReflectClass(ReflectClass reflectClass)
- Specified by:
classMetadataForReflectClass
in interfaceInternalObjectContainer
-
classMetadataForName
public ClassMetadata classMetadataForName(java.lang.String name)
- Specified by:
classMetadataForName
in interfaceInternalObjectContainer
-
classMetadataForID
public ClassMetadata classMetadataForID(int id)
- Specified by:
classMetadataForID
in interfaceInternalObjectContainer
-
handlers
public HandlerRegistry handlers()
- Specified by:
handlers
in interfaceInternalObjectContainer
-
syncExec
public java.lang.Object syncExec(Closure4 block)
- Specified by:
syncExec
in interfaceInternalObjectContainer
-
instanceCount
public int instanceCount(ClassMetadata clazz, Transaction trans)
- Specified by:
instanceCount
in interfaceInternalObjectContainer
-
query
public ObjectSet query(Predicate predicate, java.util.Comparator comparator) throws Db4oIOException, DatabaseClosedException
Description copied from interface:ObjectContainer
Native Query Interface. Queries as withObjectContainer.query(com.db4o.query.Predicate)
, but will sort the resultingObjectSet
according to the givenComparator
.- Specified by:
query
in interfaceObjectContainer
- Parameters:
predicate
- thePredicate
containing the native query expression.comparator
- the java.util.Comparator specifying the sort order of the result- Returns:
- the
ObjectSet
returned 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:
isClient
in interfaceInternalObjectContainer
-
storeAll
public void storeAll(Transaction transaction, Iterator4 objects)
- Specified by:
storeAll
in interfaceInternalObjectContainer
-
updateDepthProvider
public UpdateDepthProvider updateDepthProvider()
- Specified by:
updateDepthProvider
in interfaceInternalObjectContainer
-
openSession
public ObjectContainer openSession()
Description copied from interface:ExtObjectContainer
opens a new ObjectContainer on top of this ObjectContainer. The ObjectContainer will have it's own transaction and it's own reference system.- Specified by:
openSession
in interfaceExtObjectContainer
- Returns:
- the new ObjectContainer session.
-
newEventRegistry
public EventRegistryImpl newEventRegistry()
- Specified by:
newEventRegistry
in interfaceInternalObjectContainer
-
from
public <T> QLin<T> from(java.lang.Class<T> clazz)
-
inCallback
public boolean inCallback()
- Specified by:
inCallback
in interfaceInternalObjectContainer
-
-