Package org.apache.openjpa.abstractstore
Class AbstractStoreManager
java.lang.Object
org.apache.openjpa.abstractstore.AbstractStoreManager
- All Implemented Interfaces:
StoreManager,Closeable
- Direct Known Subclasses:
XMLStoreManager
Abstract store manager implementation to ease development of custom
OpenJPA back-ends. A concrete subclass must define implementations for the
following methods:
StoreManager.exists(org.apache.openjpa.kernel.OpenJPAStateManager, java.lang.Object)initialize(org.apache.openjpa.kernel.OpenJPAStateManager, org.apache.openjpa.kernel.PCState, org.apache.openjpa.kernel.FetchConfiguration, java.lang.Object)load(org.apache.openjpa.kernel.OpenJPAStateManager, java.util.BitSet, org.apache.openjpa.kernel.FetchConfiguration, int, java.lang.Object)flush(Collection,Collection,Collection,Collection,Collection)executeExtent(org.apache.openjpa.meta.ClassMetaData, boolean, org.apache.openjpa.kernel.FetchConfiguration)
open() has been called. Store manager instances might be
created to call metadata methods such as newConfiguration() or
getUnsupportedOptions() and never opened. These instances should
not consume any data store resources.
Notes:
- The
StoreManager.initialize(org.apache.openjpa.kernel.OpenJPAStateManager, org.apache.openjpa.kernel.PCState, org.apache.openjpa.kernel.FetchConfiguration, java.lang.Object)method is responsible for creating new instances of objects freshly loaded from the database. The method will be invoked with aOpenJPAStateManagerthat the newly-loaded object should be associated with. To create the new object and set up this association correctly, the implementation should use theOpenJPAStateManager.initialize(java.lang.Class, org.apache.openjpa.kernel.PCState)method. - If your data store supports some sort of transaction or
unit of work, you should override the
begin(),commit(), androllback()methods. - This class provides no infrastructure support for optimistic
transactions. To provide optimistic transaction support:
- Override
beginOptimistic(),rollbackOptimistic(), andsyncVersion(org.apache.openjpa.kernel.OpenJPAStateManager, java.lang.Object). - Override
getUnsupportedOptions()to not includeOpenJPAConfiguration.OPTION_OPTIMISTICin the list of unsupported options. - Ensure that your flush implementation sets the next
version for each modified object via the
OpenJPAStateManager.setNextVersion(java.lang.Object)method. - If your version object does not implement
Comparable, overridecompareVersion(org.apache.openjpa.kernel.OpenJPAStateManager, java.lang.Object, java.lang.Object), which relies on theComparable.compareTo(T)method.
- Override
- If your data store supports a mechanism for automatically
generating and managing identity values (or if you want to
provide that facility on top of your data store), implement
the
getDataStoreIdSequence(org.apache.openjpa.meta.ClassMetaData)method if you want to use alongas your datastore identity type and are happy with OpenJPA'sIdclass. To use another datastore identity type, overridegetManagedType(java.lang.Object),getDataStoreIdType(org.apache.openjpa.meta.ClassMetaData),copyDataStoreId(java.lang.Object, org.apache.openjpa.meta.ClassMetaData), andnewDataStoreId(java.lang.Object, org.apache.openjpa.meta.ClassMetaData)instead. In either case, overridegetUnsupportedOptions()to not includeOpenJPAConfiguration.OPTION_ID_DATASTOREin the list of unsupported options. - If your data store does not support queries (or if you do
not want to convert OpenJPA's query parse tree into a
datastore-specific query), you still have two options in terms
of query execution:
- In-memory execution: If you execute a query against an extent or a class, OpenJPA will automatically load the full extent of objects into memory and execute the query in memory.
- openjpa.MethodQL: MethodQL allows
you to use the query APIs to execute a method that finds
data in your back-end and returns that data as a
ResultList. For more details on MethodQL, see the OpenJPA Reference Guide.
- Since:
- 0.3.1
-
Field Summary
FieldsFields inherited from interface org.apache.openjpa.kernel.StoreManager
FORCE_LOAD_ALL, FORCE_LOAD_DFG, FORCE_LOAD_NONE, FORCE_LOAD_REFRESH, VERSION_DIFFERENT, VERSION_EARLIER, VERSION_LATER, VERSION_SAME -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanassignDataStoreId(OpenJPAStateManager sm, Object val) Assign a new datastore identity to the given instance.booleanassignField(OpenJPAStateManager sm, int field, boolean preFlush) Assign a value to the given field.booleanassignObjectId(OpenJPAStateManager sm, boolean preFlush) Assign an object id to the given new instance.voidbeforeStateChange(OpenJPAStateManager sm, PCState fromState, PCState toState) Notification that the given state manager is about to change its lifecycle state.voidbegin()OpenJPA assumes that after this method is invoked, all data accesses through this store manager will be part of a single unit of work that can be rolled back.voidNo-op implementation.booleanReturnsfalse.voidclose()Free any resources this store manager is using.voidcommit()This is a no-op implementation.intcompareVersion(OpenJPAStateManager state, Object v1, Object v2) copyDataStoreId(Object oid, ClassMetaData meta) Copy the given object id value.abstract ResultObjectProviderexecuteExtent(ClassMetaData meta, boolean subs, FetchConfiguration fetch) Create aResultObjectProviderthat can return all instances oftype, optionally including subclasses as defined bysubclasses.Breaks downstatesbased on the objects' current states, and delegates toflush(Collection,Collection,Collection,Collection,Collection).protected abstract Collection<Exception>flush(Collection<OpenJPAStateManager> pNew, Collection<OpenJPAStateManager> pNewUpdated, Collection<OpenJPAStateManager> pNewFlushedDeleted, Collection<OpenJPAStateManager> pDirty, Collection<OpenJPAStateManager> pDeleted) Responsible for writing modifications happened back to the data store.Returnsnull.Returns theStoreContextthat this store manager is associated with.getDataStoreIdSequence(ClassMetaData forClass) Returns the system-configured sequence.Class<?>Return the class used by this StoreManager for datastore identity values.Class<?>getManagedType(Object oid) Return the persistent class for the given data store identity value.protected StringReturns a string name to identify the platform of this store manager.protected Collection<String>Returns a set of option names that this store manager does not support.getValueSequence(FieldMetaData forField) Returns null.abstract booleaninitialize(OpenJPAStateManager sm, PCState state, FetchConfiguration fetch, Object edata) This method is invoked when OpenJPA needs to load an object whose identity is known but which has not yet been loaded from the data store.abstract booleanload(OpenJPAStateManager sm, BitSet fields, FetchConfiguration fetch, int lockLevel, Object edata) This method is invoked when OpenJPA needs to load additional data into an object that has already been at least partially loaded by a previousinitialize(org.apache.openjpa.kernel.OpenJPAStateManager, org.apache.openjpa.kernel.PCState, org.apache.openjpa.kernel.FetchConfiguration, java.lang.Object)invocation.loadAll(Collection<OpenJPAStateManager> sms, PCState state, int load, FetchConfiguration fetch, Object edata) This implementation just delegates to the proper singular method (StoreManager.initialize(org.apache.openjpa.kernel.OpenJPAStateManager, org.apache.openjpa.kernel.PCState, org.apache.openjpa.kernel.FetchConfiguration, java.lang.Object)orStoreManager.load(org.apache.openjpa.kernel.OpenJPAStateManager, java.util.BitSet, org.apache.openjpa.kernel.FetchConfiguration, int, java.lang.Object)) depending on each state manager's state.protected OpenJPAConfigurationReturn a new configuration instance for this runtime.newDataStoreId(Object val, ClassMetaData meta) Create a new unique datastore identity for the given type from the given oid value (presumably pk, stringified oid, or oid instance).Return a fetch configuration suitable for this runtime.Return a query implementation suitable for this store.protected voidopen()No-op implementation.voidOverride to release previously-retained connection.voidOverride to retain a dedicated connection.voidrollback()This is a no-op implementation.voidNo-op implementation.final voidsetContext(StoreContext ctx) Set a reference to the corresponding context.booleansyncVersion(OpenJPAStateManager sm, Object edata) Since this store manager does not provide optimistic locking support, this method always returnstrue.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.openjpa.kernel.StoreManager
exists, isCached
-
Field Details
-
ctx
-
-
Constructor Details
-
AbstractStoreManager
public AbstractStoreManager()
-
-
Method Details
-
setContext
Description copied from interface:StoreManagerSet a reference to the corresponding context. This method will be called before the store manager is used. The store manager is responsible for pulling any necessary configuration data from the context, including the transaction mode and connection retain mode.- Specified by:
setContextin interfaceStoreManager
-
getContext
Returns theStoreContextthat this store manager is associated with. -
open
protected void open()No-op implementation. Ready this store manager for persistent operations. -
beginOptimistic
public void beginOptimistic()No-op implementation. Override this method to provide optimistic locking semantics for your data store if you need notification of the beginning of an optimistic transaction.- Specified by:
beginOptimisticin interfaceStoreManager
-
rollbackOptimistic
public void rollbackOptimistic()No-op implementation. Override this method to provide optimistic locking semantics for your data store if you need notification of a rollback of an optimistic transaction beforebegin()is invoked.- Specified by:
rollbackOptimisticin interfaceStoreManager
-
begin
public void begin()OpenJPA assumes that after this method is invoked, all data accesses through this store manager will be part of a single unit of work that can be rolled back. This is a no-op implementation. If your data store does not support any concept of locking or transactions, you need not override this method.- Specified by:
beginin interfaceStoreManager
-
commit
public void commit()This is a no-op implementation. If your data store does not have a concept of transactions or a unit of work, you need not override this method. If it does, then override this method to notify the data store that the current transaction should be committed.- Specified by:
commitin interfaceStoreManager
-
rollback
public void rollback()This is a no-op implementation. If your data store does not have a concept of transactions or a unit of work, you need not override this method. If it does, then override this method to notify the data store that the current transaction should be rolled back.- Specified by:
rollbackin interfaceStoreManager
-
syncVersion
Since this store manager does not provide optimistic locking support, this method always returnstrue.- Specified by:
syncVersionin interfaceStoreManager- Parameters:
sm- the instance to checkedata- the current execution data, or null if not given to the calling method of the context- Returns:
- true if the instance still exists in the datastore and is up-to-date, false otherwise
-
initialize
public abstract boolean initialize(OpenJPAStateManager sm, PCState state, FetchConfiguration fetch, Object edata) This method is invoked when OpenJPA needs to load an object whose identity is known but which has not yet been loaded from the data store.smis a partially-set-up state manager for this object. The ID and least-derived type information for the instance to load can be obtained by invokingsm.getObjectId()andsm.getMetaData(). When implementing this method, load the data for this object from the data store, determine the most-derived subclass of the newly-loaded data, and then use theOpenJPAStateManager.initialize(java.lang.Class, org.apache.openjpa.kernel.PCState)method to populatesmwith a new instance of the appropriate type. OnceOpenJPAStateManager.initialize(java.lang.Class, org.apache.openjpa.kernel.PCState)has been invoked, proceed to load field data intosmas in theload(org.apache.openjpa.kernel.OpenJPAStateManager, java.util.BitSet, org.apache.openjpa.kernel.FetchConfiguration, int, java.lang.Object)method, by usingOpenJPAStateManager.store(int, java.lang.Object)(or the appropriateOpenJPAStateManager.storetypemethod) to put the data into the object.- Specified by:
initializein interfaceStoreManager- Parameters:
sm- the instance to initializestate- the lifecycle state to initialize the state manager withfetch- configuration for how to load the instanceedata- the current execution data, or null if not given to the calling method of the broker- Returns:
- true if the matching instance exists in the data store, false otherwise
-
load
public abstract boolean load(OpenJPAStateManager sm, BitSet fields, FetchConfiguration fetch, int lockLevel, Object edata) This method is invoked when OpenJPA needs to load additional data into an object that has already been at least partially loaded by a previousinitialize(org.apache.openjpa.kernel.OpenJPAStateManager, org.apache.openjpa.kernel.PCState, org.apache.openjpa.kernel.FetchConfiguration, java.lang.Object)invocation. Load data intosmby usingOpenJPAStateManager.store(int, java.lang.Object)(or the appropriateOpenJPAStateManager.storetypemethod) to put the data into the object.- Specified by:
loadin interfaceStoreManager- Parameters:
sm- the instance to loadfields- set of fields to load; all field indexes in this set must be loaded; this set is mutablefetch- the fetch configuration to use when loading related objectslockLevel- attempt to load simple fields at this lock level; relations should be loaded at the read lock level of the fetch configurationedata- the current execution data, or null if not given to the calling method of the broker- Returns:
- false if the object no longer exists in the database, true otherwise
-
loadAll
public Collection<Object> loadAll(Collection<OpenJPAStateManager> sms, PCState state, int load, FetchConfiguration fetch, Object edata) This implementation just delegates to the proper singular method (StoreManager.initialize(org.apache.openjpa.kernel.OpenJPAStateManager, org.apache.openjpa.kernel.PCState, org.apache.openjpa.kernel.FetchConfiguration, java.lang.Object)orStoreManager.load(org.apache.openjpa.kernel.OpenJPAStateManager, java.util.BitSet, org.apache.openjpa.kernel.FetchConfiguration, int, java.lang.Object)) depending on each state manager's state. If your data store provides bulk loading APIs, overriding this method to be more clever may be advantageous.- Specified by:
loadAllin interfaceStoreManager- Parameters:
sms- the state manager instances to loadstate- the lifecycle state to initialize uninitialized state managers with; may be null if no uninitialized instances are included insmsload- one of the FORCE_LOAD_* constants describing the fields to force-load if this is a refresh or retrieve actionfetch- the current fetch configuration to use when loading related objectsedata- the current execution data, or null if not given to the calling method of the broker- Returns:
- a collection of the state manager identities for which no data store record exists
- See Also:
-
flush
Breaks downstatesbased on the objects' current states, and delegates toflush(Collection,Collection,Collection,Collection,Collection).- Specified by:
flushin interfaceStoreManager- See Also:
-
org.apache.openjpa.util.ApplicationIds#assign()
-
beforeStateChange
Description copied from interface:StoreManagerNotification that the given state manager is about to change its lifecycle state. The store manager is not required to do anything in this method, but some back ends may need to.- Specified by:
beforeStateChangein interfaceStoreManager
-
assignObjectId
Description copied from interface:StoreManagerAssign an object id to the given new instance. Return false if the instance cannot be assigned an identity because a flush is required (for example, the identity is determined by the datastore on insert). For application identity instances, the assigned object id should be based on field state. The implementation is responsible for using the proper value strategy according to the instance metadata. This method is called the first time a user requests the oid of a new instance before flush.- Specified by:
assignObjectIdin interfaceStoreManagerpreFlush- whether this assignment is being requested by the system as part of pre-flush activities, and can be ignored if it is more efficient to assign withinStoreManager.flush(java.util.Collection<org.apache.openjpa.kernel.OpenJPAStateManager>)- See Also:
-
assignDataStoreId
Assign a new datastore identity to the given instance. This given value may be null. -
assignField
Description copied from interface:StoreManagerAssign a value to the given field. Return false if the value cannot be assigned because a flush is required (for example, the field value is determined by the datastore on insert). This method is called the first time a user requests the value of a field with a value-strategy on a new instance before flush.- Specified by:
assignFieldin interfaceStoreManagerpreFlush- whether this assignment is being requested by the system as part of pre-flush activities, and can be ignored if it is more efficient to assign withinStoreManager.flush(java.util.Collection<org.apache.openjpa.kernel.OpenJPAStateManager>)- See Also:
-
getManagedType
Description copied from interface:StoreManagerReturn the persistent class for the given data store identity value. If the given value is not a datastore identity object, return null.- Specified by:
getManagedTypein interfaceStoreManager
-
getDataStoreIdType
Description copied from interface:StoreManagerReturn the class used by this StoreManager for datastore identity values. The given metadata may be null, in which case the return value should the common datastore identity class for all classes, or null if this store manager does not use a common identity class.- Specified by:
getDataStoreIdTypein interfaceStoreManager
-
copyDataStoreId
Description copied from interface:StoreManagerCopy the given object id value. Use the described type of the given metadata, which may be a subclass of the given oid's described type.- Specified by:
copyDataStoreIdin interfaceStoreManager
-
newDataStoreId
Description copied from interface:StoreManagerCreate a new unique datastore identity for the given type from the given oid value (presumably pk, stringified oid, or oid instance).- Specified by:
newDataStoreIdin interfaceStoreManager
-
retainConnection
public void retainConnection()Override to retain a dedicated connection.- Specified by:
retainConnectionin interfaceStoreManager
-
releaseConnection
public void releaseConnection()Override to release previously-retained connection.- Specified by:
releaseConnectionin interfaceStoreManager
-
getClientConnection
Returnsnull. If your data store can provide a distinct connection object, return it here.- Specified by:
getClientConnectionin interfaceStoreManager
-
executeExtent
public abstract ResultObjectProvider executeExtent(ClassMetaData meta, boolean subs, FetchConfiguration fetch) Create aResultObjectProviderthat can return all instances oftype, optionally including subclasses as defined bysubclasses. The implementation of the result provider will typically execute some sort of data store query to find all the applicable objects, loop through the results, extracting object IDs from the data, and invokeStoreContext.find(Object,FetchConfiguration,BitSet,Object,int)on each OID. When invoking this method, the first argument is the OID. The second is the given fetch configuration. The third argument is a mask of fields to exclude from loading; it will typically be null. The fourth argument is an object that will be passed through toinitialize(org.apache.openjpa.kernel.OpenJPAStateManager, org.apache.openjpa.kernel.PCState, org.apache.openjpa.kernel.FetchConfiguration, java.lang.Object)orload(org.apache.openjpa.kernel.OpenJPAStateManager, java.util.BitSet, org.apache.openjpa.kernel.FetchConfiguration, int, java.lang.Object), and typically will contain the actual data to load. For example, for a JDBC-based store manager, this might be the result set that is being iterated over. If this argument isnull, then theinitialize(org.apache.openjpa.kernel.OpenJPAStateManager, org.apache.openjpa.kernel.PCState, org.apache.openjpa.kernel.FetchConfiguration, java.lang.Object)orload(org.apache.openjpa.kernel.OpenJPAStateManager, java.util.BitSet, org.apache.openjpa.kernel.FetchConfiguration, int, java.lang.Object)method will have to issue another command to the data store in order to fetch the data to be loaded.- Specified by:
executeExtentin interfaceStoreManager
-
newQuery
Description copied from interface:StoreManagerReturn a query implementation suitable for this store. If the query is iterated within a data store transaction, returned instances should be locked. Return null if this store does not support native execution of the given language. OpenJPA can execute JPQL in memory even without back end support.- Specified by:
newQueryin interfaceStoreManager- Parameters:
language- the query language
-
newFetchConfiguration
Description copied from interface:StoreManagerReturn a fetch configuration suitable for this runtime. Typically will be or extendFetchConfigurationImpl.- Specified by:
newFetchConfigurationin interfaceStoreManager
-
compareVersion
Castsv1andv2toComparable, and invokesv1.compareTo (v2). Ifv1is less thanv2, returnsStoreManager.VERSION_EARLIER. If the same, returnsStoreManager.VERSION_SAME. Otherwise, returnsStoreManager.VERSION_LATER. If eitherv1orv2arenull, returnsStoreManager.VERSION_DIFFERENT.- Specified by:
compareVersionin interfaceStoreManager- Parameters:
state- the state manager for the objectv1- the first version object to comparev2- the second version object to compare- Returns:
StoreManager.VERSION_LATERifv1is later thanv2StoreManager.VERSION_EARLIERifv1is earlier thanv2StoreManager.VERSION_SAMEifv1is the same asv2StoreManager.VERSION_DIFFERENTifv1is different fromv2, but the time difference of the versions cannot be determined
-
getDataStoreIdSequence
Returns the system-configured sequence. To use some other sort of datastore identifier (a GUID, string, or someting of that nature), overridegetManagedType(java.lang.Object),getDataStoreIdType(org.apache.openjpa.meta.ClassMetaData),copyDataStoreId(java.lang.Object, org.apache.openjpa.meta.ClassMetaData),newDataStoreId(java.lang.Object, org.apache.openjpa.meta.ClassMetaData).- Specified by:
getDataStoreIdSequencein interfaceStoreManager
-
getValueSequence
Returns null.- Specified by:
getValueSequencein interfaceStoreManager
-
cancelAll
public boolean cancelAll()Returnsfalse. If your data store supports cancelling queries, this method should cancel any currently-running queries and returntrueif any were cancelled.- Specified by:
cancelAllin interfaceStoreManager- Returns:
- true if any statements cancelled, false otherwise
-
close
public void close()Description copied from interface:StoreManagerFree any resources this store manager is using.- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceStoreManager
-
flush
protected abstract Collection<Exception> flush(Collection<OpenJPAStateManager> pNew, Collection<OpenJPAStateManager> pNewUpdated, Collection<OpenJPAStateManager> pNewFlushedDeleted, Collection<OpenJPAStateManager> pDirty, Collection<OpenJPAStateManager> pDeleted) Responsible for writing modifications happened back to the data store. If you do not remove theOpenJPAConfiguration.OPTION_INC_FLUSHoption ingetUnsupportedOptions(), this will be called only once at the end of a transaction. Otherwise, it may be called periodically throughout the course of a transaction. If this store manager supports optimistic transactions, datastore version information should be updated during flush, and the state manager's version indicator should be updated through theOpenJPAStateManager.setNextVersion(java.lang.Object)method. This method will only be invoked if there are meaningful changes to store. This differs from the behavior ofStoreManager.flush(java.util.Collection<org.apache.openjpa.kernel.OpenJPAStateManager>), which may be invoked with a collection of objects in states that do not require any datastore action (for example, objects in the transient-transactional state).- Parameters:
pNew- Objects that should be added to the store, and that have not previously been flushed.pNewUpdated- New objects that have been modified since they were initially flushed. These were inpersistentNewin an earlier flush invocation.pNewFlushedDeleted- New objects that have been deleted since they were initially flushed. These were inpersistentNewin an earlier flush invocation.pDirty- Objects that were loaded from the data store and have since been modified.pDeleted- Objects that were loaded from the data store and have since been deleted. These may have been in a previous flush invocation's persistentDirty list.- Returns:
- a collection of exceptions encountered during flushing.
-
newConfiguration
Return a new configuration instance for this runtime. Configuration data is maintained at the factory level and is available to all OpenJPA components; therefore it is a good place to maintain shared resources such as connection pools, etc. -
getUnsupportedOptions
Returns a set of option names that this store manager does not support. By default, returns the following: -
getPlatform
Returns a string name to identify the platform of this store manager. Returns the class name of this store manager by default.
-