org.apache.openjpa.slice.jdbc
Class DistributedJDBCStoreManager

java.lang.Object
  extended by org.apache.openjpa.jdbc.kernel.JDBCStoreManager
      extended by org.apache.openjpa.slice.jdbc.DistributedJDBCStoreManager
All Implemented Interfaces:
JDBCStore, StoreManager, Closeable, DistributedStoreManager

 class DistributedJDBCStoreManager
extends JDBCStoreManager
implements DistributedStoreManager

A Store manager for multiple physical databases referred as slice. This receiver behaves like a Transaction Manager as it implements two-phase commit protocol if all the component slices is XA-complaint. The actions are delegated to the underlying slices. The actions are executed in parallel threads whenever possible such as flushing or query.

Author:
Pinaki Poddar

Nested Class Summary
 
Nested classes/interfaces inherited from class org.apache.openjpa.jdbc.kernel.JDBCStoreManager
JDBCStoreManager.CancelPreparedStatement, JDBCStoreManager.CancelStatement, JDBCStoreManager.ClientConnection, JDBCStoreManager.RefCountConnection
 
Field Summary
 
Fields 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
DistributedJDBCStoreManager(DistributedJDBCConfiguration conf)
          Constructs a set of child StoreManagers each connected to a physical DataSource.
 
Method Summary
 SliceStoreManager addSlice(Slice slice)
          Adds the given slice with the given properties.
 boolean assignField(OpenJPAStateManager sm, int field, boolean preFlush)
          Assign a value to the given field.
 boolean assignObjectId(OpenJPAStateManager sm, boolean preFlush)
          Assign an object id to the given new instance.
 void beforeStateChange(OpenJPAStateManager sm, PCState fromState, PCState toState)
          Notification that the given state manager is about to change its lifecycle state.
 void beginOptimistic()
          Notification that an optimistic transaction has started.
 boolean cancelAll()
          Cancel all pending data store statements.
 int compareVersion(OpenJPAStateManager sm, Object v1, Object v2)
          Compare the two version objects.
protected  JDBCStoreManager.RefCountConnection connectInternal()
          Connect to the database.
 Object copyDataStoreId(Object oid, ClassMetaData meta)
          Copy the given object id value.
 ResultObjectProvider executeExtent(ClassMetaData meta, boolean subclasses, FetchConfiguration fetch)
          Return a provider for all instances of the given candidate class, optionally including subclasses.
 boolean exists(OpenJPAStateManager sm, Object edata)
          Verify that the given instance exists in the data store; return false if it does not.
protected  SliceInfo findSliceNames(OpenJPAStateManager sm, Object edata)
          Decides the index of the StoreManager by first looking at the implementation data.
 Collection flush(Collection sms)
          Flush the given StateManagers after binning them to respective physical slices.
 Object getClientConnection()
          Return a connection to the data store suitable for client use.
 DistributedJDBCConfiguration getConfiguration()
          Return the configuration for this runtime.
 Seq getDataStoreIdSequence(ClassMetaData forClass)
          Return a sequence that generates datastore identity values for the given class.
 Class<?> getDataStoreIdType(ClassMetaData meta)
          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.
 SliceStoreManager getSlice(int i)
           
(package private)  List<SliceStoreManager> getTargets(FetchConfiguration fetch)
          Gets the list of slices mentioned as hint of the given fetch configuration.
 Seq getValueSequence(FieldMetaData forField)
          Return a sequence that generates values for the given field.
 boolean initialize(OpenJPAStateManager sm, PCState state, FetchConfiguration fetch, Object edata)
          Initialize the given state manager.
 boolean load(OpenJPAStateManager sm, BitSet fields, FetchConfiguration fetch, int lockLevel, Object edata)
          Load the given state manager.
 Collection loadAll(Collection sms, PCState state, int load, FetchConfiguration fetch, Object edata)
          Initialize, load, or validate the existance of all of the given objects.
 Object newDataStoreId(Object oidVal, ClassMetaData meta)
          Create a new unique datastore identity for the given type from the given oid value (presumably pk, stringified oid, or oid instance).
 FetchConfiguration newFetchConfiguration()
          Return a fetch configuration suitable for this runtime.
 StoreQuery newQuery(String language)
          Construct a distributed query to be executed against all the slices.
(package private)  boolean occurs(OpenJPAStateManager sm, Collection<org.apache.openjpa.slice.jdbc.DistributedJDBCStoreManager.StateManagerSet> reminder)
           
 void setContext(StoreContext ctx)
          Sets the context for this receiver and all its underlying slices.
 boolean syncVersion(OpenJPAStateManager sm, Object edata)
          Update the version information in the given state manager to the version stored in the data store.
 
Methods inherited from class org.apache.openjpa.jdbc.kernel.JDBCStoreManager
begin, close, commit, find, getConnection, getContext, getDataSource, getDBDictionary, getFetchConfiguration, getLockManager, getNewConnection, getSQLFactory, getType, getVersion, initializeState, isCached, isEmptyResult, load, loadSubclasses, newDataStoreId, releaseConnection, retainConnection, rollback, rollbackOptimistic, select, setContext, setMappedBy
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.openjpa.kernel.StoreManager
begin, close, commit, isCached, releaseConnection, retainConnection, rollback, rollbackOptimistic
 

Constructor Detail

DistributedJDBCStoreManager

public DistributedJDBCStoreManager(DistributedJDBCConfiguration conf)
Constructs a set of child StoreManagers each connected to a physical DataSource. The supplied configuration carries multiple URL for underlying physical slices. The first slice is referred as master and is used to get Sequence based entity identifiers.

Method Detail

getConfiguration

public DistributedJDBCConfiguration getConfiguration()
Description copied from interface: JDBCStore
Return the configuration for this runtime.

Specified by:
getConfiguration in interface JDBCStore
Overrides:
getConfiguration in class JDBCStoreManager

getSlice

public SliceStoreManager getSlice(int i)

addSlice

public SliceStoreManager addSlice(Slice slice)
Description copied from interface: DistributedStoreManager
Adds the given slice with the given properties. This newly added slice will participate in the current and subsequent transaction.

Specified by:
addSlice in interface DistributedStoreManager
Returns:
the store manager for the newly added slice.
See Also:
DistributedBroker#addSlice(String, Map), DistributedBrokerFactory.addSlice(String, Map)

findSliceNames

protected SliceInfo findSliceNames(OpenJPAStateManager sm,
                                   Object edata)
Decides the index of the StoreManager by first looking at the implementation data. If no implementation data is found, then estimates targets slices by using additional connection info. If no additional connection info then calls back to user-defined policy.


assignField

public boolean assignField(OpenJPAStateManager sm,
                           int field,
                           boolean preFlush)
Description copied from interface: StoreManager
Assign 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:
assignField in interface StoreManager
Overrides:
assignField in class JDBCStoreManager
preFlush - 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 within StoreManager.flush(java.util.Collection)
See Also:
ImplHelper.generateFieldValue(org.apache.openjpa.kernel.StoreContext, org.apache.openjpa.meta.FieldMetaData)

assignObjectId

public boolean assignObjectId(OpenJPAStateManager sm,
                              boolean preFlush)
Description copied from interface: StoreManager
Assign 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:
assignObjectId in interface StoreManager
Overrides:
assignObjectId in class JDBCStoreManager
preFlush - 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 within StoreManager.flush(java.util.Collection)
See Also:
ImplHelper.generateFieldValue(org.apache.openjpa.kernel.StoreContext, org.apache.openjpa.meta.FieldMetaData), ImplHelper.generateIdentityValue(org.apache.openjpa.kernel.StoreContext, org.apache.openjpa.meta.ClassMetaData, int), org.apache.openjpa.util.ApplicationIds#assign()

beforeStateChange

public void beforeStateChange(OpenJPAStateManager sm,
                              PCState fromState,
                              PCState toState)
Description copied from interface: StoreManager
Notification 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:
beforeStateChange in interface StoreManager
Overrides:
beforeStateChange in class JDBCStoreManager

beginOptimistic

public void beginOptimistic()
Description copied from interface: StoreManager
Notification that an optimistic transaction has started. This method does not replace the StoreManager.begin() method, which will still be called when a true data store transaction should begin.

Specified by:
beginOptimistic in interface StoreManager
Overrides:
beginOptimistic in class JDBCStoreManager

cancelAll

public boolean cancelAll()
Description copied from interface: StoreManager
Cancel all pending data store statements.

Specified by:
cancelAll in interface StoreManager
Overrides:
cancelAll in class JDBCStoreManager
Returns:
true if any statements cancelled, false otherwise

compareVersion

public int compareVersion(OpenJPAStateManager sm,
                          Object v1,
                          Object v2)
Description copied from interface: StoreManager
Compare the two version objects.

Specified by:
compareVersion in interface StoreManager
Overrides:
compareVersion in class JDBCStoreManager
Parameters:
sm - the state manager for the object
v1 - the first version object to compare
v2 - the second version object to compare
Returns:

copyDataStoreId

public Object copyDataStoreId(Object oid,
                              ClassMetaData meta)
Description copied from interface: StoreManager
Copy 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:
copyDataStoreId in interface StoreManager
Overrides:
copyDataStoreId in class JDBCStoreManager

executeExtent

public ResultObjectProvider executeExtent(ClassMetaData meta,
                                          boolean subclasses,
                                          FetchConfiguration fetch)
Description copied from interface: StoreManager
Return a provider for all instances of the given candidate class, optionally including subclasses. The given candidate may be an unmapped type with mapped subclasses. If the provider is iterated within a data store transaction, returned instances should be locked.

Specified by:
executeExtent in interface StoreManager
Overrides:
executeExtent in class JDBCStoreManager

exists

public boolean exists(OpenJPAStateManager sm,
                      Object edata)
Description copied from interface: StoreManager
Verify that the given instance exists in the data store; return false if it does not.

Specified by:
exists in interface StoreManager
Overrides:
exists in class JDBCStoreManager

flush

public Collection flush(Collection sms)
Flush the given StateManagers after binning them to respective physical slices.

Specified by:
flush in interface StoreManager
Overrides:
flush in class JDBCStoreManager
See Also:
org.apache.openjpa.util.ApplicationIds#assign()

occurs

boolean occurs(OpenJPAStateManager sm,
               Collection<org.apache.openjpa.slice.jdbc.DistributedJDBCStoreManager.StateManagerSet> reminder)

getClientConnection

public Object getClientConnection()
Description copied from interface: StoreManager
Return a connection to the data store suitable for client use. If this method is called during a data store transaction, thie connection must be transactional. If no connection is in use, this method should create one to return.

Specified by:
getClientConnection in interface StoreManager
Overrides:
getClientConnection in class JDBCStoreManager

getDataStoreIdSequence

public Seq getDataStoreIdSequence(ClassMetaData forClass)
Description copied from interface: StoreManager
Return a sequence that generates datastore identity values for the given class. This method will only be called when the identity strategy for the class is one of: If the identity strategy cannot be represented as a sequence, return null.

Specified by:
getDataStoreIdSequence in interface StoreManager
Overrides:
getDataStoreIdSequence in class JDBCStoreManager

getDataStoreIdType

public Class<?> getDataStoreIdType(ClassMetaData meta)
Description copied from interface: StoreManager
Return 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:
getDataStoreIdType in interface StoreManager
Overrides:
getDataStoreIdType in class JDBCStoreManager

getManagedType

public Class<?> getManagedType(Object oid)
Description copied from interface: StoreManager
Return the persistent class for the given data store identity value. If the given value is not a datastore identity object, return null.

Specified by:
getManagedType in interface StoreManager
Overrides:
getManagedType in class JDBCStoreManager

getValueSequence

public Seq getValueSequence(FieldMetaData forField)
Description copied from interface: StoreManager
Return a sequence that generates values for the given field. This method will only be called when the value strategy for the field is one of: If the value strategy cannot be represented as a sequence, return null.

Specified by:
getValueSequence in interface StoreManager
Overrides:
getValueSequence in class JDBCStoreManager

initialize

public boolean initialize(OpenJPAStateManager sm,
                          PCState state,
                          FetchConfiguration fetch,
                          Object edata)
Description copied from interface: StoreManager
Initialize the given state manager. The object id of the state manager will be set, and the state manager's metadata be set to the class of the instance to load, or possibly one of its superclasses. Initialization involves first calling the OpenJPAStateManager.initialize(java.lang.Class, org.apache.openjpa.kernel.PCState) method with a new instance of the correct type constructed with the PCRegistry.newInstance(Class, org.apache.openjpa.enhance.StateManager, boolean) method (this will reset the state manager's metadata if the actual type was a subclass). After instance initialization, load any the fields for the given fetch configuration that can be efficiently retrieved. If any of the configured fields are not loaded in this method, they will be loaded with a subsequent call to StoreManager.load(org.apache.openjpa.kernel.OpenJPAStateManager, java.util.BitSet, org.apache.openjpa.kernel.FetchConfiguration, int, java.lang.Object). If this method is called during a data store transaction, the instance's database record should be locked. Version information can be loaded if desired through the OpenJPAStateManager.setVersion(java.lang.Object) method.

Specified by:
initialize in interface StoreManager
Overrides:
initialize in class JDBCStoreManager
Parameters:
sm - the instance to initialize
state - the lifecycle state to initialize the state manager with
fetch - configuration for how to load the instance
edata - 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 boolean load(OpenJPAStateManager sm,
                    BitSet fields,
                    FetchConfiguration fetch,
                    int lockLevel,
                    Object edata)
Description copied from interface: StoreManager
Load the given state manager. Note that any collection or map types loaded into the state manager will be proxied with the correct type; therefore the store manager does not have to load the same concrete collection/map types as the instance declares. However, array types must be consistent with the array type stored by the persistence capable instance. If this method is called during a data store transaction, the instance should be locked. If the given state manager does not have its version set already, version information can be loaded if desired through the OpenJPAStateManager.setVersion(java.lang.Object) method.

Specified by:
load in interface StoreManager
Overrides:
load in class JDBCStoreManager
Parameters:
sm - the instance to load
fields - set of fields to load; all field indexes in this set must be loaded; this set is mutable
fetch - the fetch configuration to use when loading related objects
lockLevel - attempt to load simple fields at this lock level; relations should be loaded at the read lock level of the fetch configuration
edata - 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 loadAll(Collection sms,
                          PCState state,
                          int load,
                          FetchConfiguration fetch,
                          Object edata)
Description copied from interface: StoreManager
Initialize, load, or validate the existance of all of the given objects. This method is called from various broker methods that act on multiple objects, such as StoreContext.retrieveAll(java.util.Collection, boolean, org.apache.openjpa.kernel.OpCallbacks). It gives the store manager an opportunity to efficiently batch-load data for several objects. Each of the given state managers will be in one of three states, each requiring a different action: Store managers that cannot efficiently batch load can simply test for these conditions and delegate to the proper methods.

Specified by:
loadAll in interface StoreManager
Overrides:
loadAll in class JDBCStoreManager
Parameters:
sms - the state manager instances to load
state - the lifecycle state to initialize uninitialized state managers with; may be null if no uninitialized instances are included in sms
load - one of the FORCE_LOAD_* constants describing the fields to force-load if this is a refresh or retrieve action
fetch - the current fetch configuration to use when loading related objects
edata - 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:
ImplHelper.loadAll(java.util.Collection, org.apache.openjpa.kernel.StoreManager, org.apache.openjpa.kernel.PCState, int, org.apache.openjpa.kernel.FetchConfiguration, java.lang.Object)

newDataStoreId

public Object newDataStoreId(Object oidVal,
                             ClassMetaData meta)
Description copied from interface: StoreManager
Create a new unique datastore identity for the given type from the given oid value (presumably pk, stringified oid, or oid instance).

Specified by:
newDataStoreId in interface StoreManager
Overrides:
newDataStoreId in class JDBCStoreManager

newQuery

public StoreQuery newQuery(String language)
Construct a distributed query to be executed against all the slices.

Specified by:
newQuery in interface StoreManager
Overrides:
newQuery in class JDBCStoreManager
Parameters:
language - the query language

newFetchConfiguration

public FetchConfiguration newFetchConfiguration()
Description copied from interface: StoreManager
Return a fetch configuration suitable for this runtime. Typically will be or extend FetchConfigurationImpl.

Specified by:
newFetchConfiguration in interface StoreManager
Overrides:
newFetchConfiguration in class JDBCStoreManager

setContext

public void setContext(StoreContext ctx)
Sets the context for this receiver and all its underlying slices.

Specified by:
setContext in interface StoreManager
Overrides:
setContext in class JDBCStoreManager

syncVersion

public boolean syncVersion(OpenJPAStateManager sm,
                           Object edata)
Description copied from interface: StoreManager
Update the version information in the given state manager to the version stored in the data store.

Specified by:
syncVersion in interface StoreManager
Overrides:
syncVersion in class JDBCStoreManager
Parameters:
sm - the instance to check
edata - 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

connectInternal

protected JDBCStoreManager.RefCountConnection connectInternal()
                                                       throws SQLException
Description copied from class: JDBCStoreManager
Connect to the database. This method is separated out so that it can be overridden.

Overrides:
connectInternal in class JDBCStoreManager
Throws:
SQLException

getTargets

List<SliceStoreManager> getTargets(FetchConfiguration fetch)
Gets the list of slices mentioned as hint of the given fetch configuration.

Returns:
all active slices if a) the hint is not specified or b) a null value or c) a non-String or d) matches no active slice.


Copyright © 2006-2011 Apache Software Foundation. All Rights Reserved.