org.springframework.datastore.core
Class AbstractSession<N>

java.lang.Object
  extended by org.springframework.datastore.core.AbstractSession<N>
All Implemented Interfaces:
Session, SessionImplementor, EntityInterceptorAware
Direct Known Subclasses:
RedisSession, SimpleMapSession

public abstract class AbstractSession<N>
extends Object
implements Session, SessionImplementor

Abstract implementation of the Session interface that uses a list of Persister instances to save, update and delete instances

Since:
1.0

Field Summary
protected  Map<Object,Map<String,Object>> attributes
           
protected  Map<Class,Map<Serializable,Object>> firstLevelCache
           
protected  List<EntityInterceptor> interceptors
           
protected  ConcurrentLinkedQueue lockedObjects
           
protected  Map<Object,Serializable> objectToKey
           
protected  Collection<Runnable> pendingDeletes
           
protected  Collection<Runnable> pendingInserts
           
protected  Collection<Runnable> pendingUpdates
           
protected  Map<Class,Persister> persisters
           
 
Constructor Summary
AbstractSession(Datastore datastore, MappingContext mappingContext)
           
 
Method Summary
 void addEntityInterceptor(EntityInterceptor interceptor)
          Adds an EntityInterceptor
 void attach(Object o)
          Attaches an object the current session
 Transaction beginTransaction()
          Starts a transaction
protected abstract  Transaction beginTransactionInternal()
           
protected  void cacheObject(Serializable identifier, Object o)
           
 void clear()
          Clears any pending changes to the datastore
 void clear(Object o)
          Clear a specific object
 boolean contains(Object o)
          Whether the object is contained within the first level cache
protected abstract  Persister createPersister(Class cls, MappingContext mappingContext)
           
 Query createQuery(Class type)
          Creates a query instance for the give type
 void delete(Iterable objects)
          Deletes one or many objects
 void delete(Object obj)
          Deletes a single object
 void disconnect()
          Performs clear up.
 void flush()
          Flushes any pending changes to the datastore
 Object getAttribute(Object entity, String attributeName)
          Obtains an attribute for the given entity
 Datastore getDatastore()
          The Datastore that created this Session
 javax.persistence.FlushModeType getFlushMode()
          Obtains the current FlushModeType
 MappingContext getMappingContext()
          Obtains the MappingContext instance
 Collection<Runnable> getPendingDeletes()
           
 Collection<Runnable> getPendingInserts()
           
 Collection<Runnable> getPendingUpdates()
           
 Persister getPersister(Object o)
          The persister for the given object
 Transaction getTransaction()
          Obtains the current transaction instance
 Object lock(Class type, Serializable key)
          Retrieves an individual object, using a write lock to prevent loss of updates
 void lock(Object o)
          Obtains a write lock on the given object
 List<Serializable> persist(Iterable objects)
          Persists several objects returning their identifiers in the order specified by the Iterable
 Serializable persist(Object o)
          Stores and object and returns its key
protected  void postFlush()
           
 Object proxy(Class type, Serializable key)
          Retrieves a proxy for the given key
 void refresh(Object o)
          Refreshes the given objects state
 Object retrieve(Class type, Serializable key)
          Retrieves an individual object
 List retrieveAll(Class type, Iterable keys)
          Retrieves several objects for the specified keys
 List retrieveAll(Class type, Serializable... keys)
          Retrieves several objects for the specified keys
 void setAttribute(Object entity, String attributeName, Object value)
          Associates an attribute with the given persistent entity.
 void setEntityInterceptors(List<EntityInterceptor> interceptors)
           
 void setFlushMode(javax.persistence.FlushModeType flushMode)
          The flush mode, defaults to FlushModeType.AUTO
 void unlock(Object o)
          Releases a lock, if not called all locked objects should be released by Session.disconnect()
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.springframework.datastore.core.Session
getNativeInterface, isConnected
 

Field Detail

persisters

protected Map<Class,Persister> persisters

interceptors

protected List<EntityInterceptor> interceptors

lockedObjects

protected ConcurrentLinkedQueue lockedObjects

firstLevelCache

protected Map<Class,Map<Serializable,Object>> firstLevelCache

attributes

protected Map<Object,Map<String,Object>> attributes

objectToKey

protected Map<Object,Serializable> objectToKey

pendingInserts

protected Collection<Runnable> pendingInserts

pendingUpdates

protected Collection<Runnable> pendingUpdates

pendingDeletes

protected Collection<Runnable> pendingDeletes
Constructor Detail

AbstractSession

public AbstractSession(Datastore datastore,
                       MappingContext mappingContext)
Method Detail

setAttribute

public void setAttribute(Object entity,
                         String attributeName,
                         Object value)
Description copied from interface: Session
Associates an attribute with the given persistent entity. Attributes will be cleared out when the Session is closed or cleared.

Specified by:
setAttribute in interface Session
Parameters:
entity - The persistent instance (must be associated with this Session)
attributeName - The attribute name
value - The value

getAttribute

public Object getAttribute(Object entity,
                           String attributeName)
Description copied from interface: Session
Obtains an attribute for the given entity

Specified by:
getAttribute in interface Session
Parameters:
entity - The entity
attributeName - The attribute
Returns:
The attribute value

getPendingInserts

public Collection<Runnable> getPendingInserts()
Specified by:
getPendingInserts in interface SessionImplementor

getPendingUpdates

public Collection<Runnable> getPendingUpdates()
Specified by:
getPendingUpdates in interface SessionImplementor

getPendingDeletes

public Collection<Runnable> getPendingDeletes()
Specified by:
getPendingDeletes in interface SessionImplementor

getFlushMode

public javax.persistence.FlushModeType getFlushMode()
Description copied from interface: Session
Obtains the current FlushModeType

Specified by:
getFlushMode in interface Session
Returns:
The FlushModeType instance

setFlushMode

public void setFlushMode(javax.persistence.FlushModeType flushMode)
Description copied from interface: Session
The flush mode, defaults to FlushModeType.AUTO

Specified by:
setFlushMode in interface Session
Parameters:
flushMode - The FlushModeType

getDatastore

public Datastore getDatastore()
Description copied from interface: Session
The Datastore that created this Session

Specified by:
getDatastore in interface Session
Returns:
The Datastore instance

addEntityInterceptor

public void addEntityInterceptor(EntityInterceptor interceptor)
Description copied from interface: EntityInterceptorAware
Adds an EntityInterceptor

Specified by:
addEntityInterceptor in interface EntityInterceptorAware
Parameters:
interceptor - The interceptor to add

setEntityInterceptors

public void setEntityInterceptors(List<EntityInterceptor> interceptors)
Specified by:
setEntityInterceptors in interface EntityInterceptorAware
Parameters:
interceptors - A list of entity interceptors

getMappingContext

public MappingContext getMappingContext()
Description copied from interface: Session
Obtains the MappingContext instance

Specified by:
getMappingContext in interface Session
Returns:
The MappingContext

flush

public void flush()
Description copied from interface: Session
Flushes any pending changes to the datastore

Specified by:
flush in interface Session

postFlush

protected void postFlush()

clear

public void clear()
Description copied from interface: Session
Clears any pending changes to the datastore

Specified by:
clear in interface Session

getPersister

public final Persister getPersister(Object o)
Description copied from interface: Session
The persister for the given object

Specified by:
getPersister in interface Session
Parameters:
o - The object
Returns:
The persister

createPersister

protected abstract Persister createPersister(Class cls,
                                             MappingContext mappingContext)

contains

public boolean contains(Object o)
Description copied from interface: Session
Whether the object is contained within the first level cache

Specified by:
contains in interface Session
Parameters:
o - The object to check
Returns:
True if it is

clear

public void clear(Object o)
Description copied from interface: Session
Clear a specific object

Specified by:
clear in interface Session
Parameters:
o - The object to clear

attach

public void attach(Object o)
Description copied from interface: Session
Attaches an object the current session

Specified by:
attach in interface Session
Parameters:
o - The object to attach

cacheObject

protected void cacheObject(Serializable identifier,
                           Object o)

persist

public Serializable persist(Object o)
Description copied from interface: Session
Stores and object and returns its key

Specified by:
persist in interface Session
Parameters:
o - The object
Returns:
The the generated key

refresh

public void refresh(Object o)
Description copied from interface: Session
Refreshes the given objects state

Specified by:
refresh in interface Session
Parameters:
o - The object to refresh

retrieve

public Object retrieve(Class type,
                       Serializable key)
Description copied from interface: Session
Retrieves an individual object

Specified by:
retrieve in interface Session
Parameters:
type - The type
key - The key
Returns:
The object

proxy

public Object proxy(Class type,
                    Serializable key)
Description copied from interface: Session
Retrieves a proxy for the given key

Specified by:
proxy in interface Session
Parameters:
type - The type
key - The key
Returns:
The object

lock

public void lock(Object o)
Description copied from interface: Session
Obtains a write lock on the given object

Specified by:
lock in interface Session
Parameters:
o - The object to lock

lock

public Object lock(Class type,
                   Serializable key)
Description copied from interface: Session
Retrieves an individual object, using a write lock to prevent loss of updates

Specified by:
lock in interface Session
Parameters:
type - The type
key - The key
Returns:
The object

unlock

public void unlock(Object o)
Description copied from interface: Session
Releases a lock, if not called all locked objects should be released by Session.disconnect()

Specified by:
unlock in interface Session
Parameters:
o - The object to unlock

delete

public void delete(Object obj)
Description copied from interface: Session
Deletes a single object

Specified by:
delete in interface Session
Parameters:
obj - The object to delete

delete

public void delete(Iterable objects)
Description copied from interface: Session
Deletes one or many objects

Specified by:
delete in interface Session
Parameters:
objects - The objects to delete

disconnect

public void disconnect()
Performs clear up. Subclasses should always call into this super implementation.

Specified by:
disconnect in interface Session

persist

public List<Serializable> persist(Iterable objects)
Description copied from interface: Session
Persists several objects returning their identifiers in the order specified by the Iterable

Specified by:
persist in interface Session
Parameters:
objects - The Objects
Returns:
The identifiers

retrieveAll

public List retrieveAll(Class type,
                        Iterable keys)
Description copied from interface: Session
Retrieves several objects for the specified keys

Specified by:
retrieveAll in interface Session
Parameters:
type - The type
keys - The keys
Returns:
A list of objects

retrieveAll

public List retrieveAll(Class type,
                        Serializable... keys)
Description copied from interface: Session
Retrieves several objects for the specified keys

Specified by:
retrieveAll in interface Session
Parameters:
type - The type
keys - The keys
Returns:
A list of objects

createQuery

public Query createQuery(Class type)
Description copied from interface: Session
Creates a query instance for the give type

Specified by:
createQuery in interface Session
Parameters:
type - The type
Returns:
The query

beginTransaction

public final Transaction beginTransaction()
Description copied from interface: Session
Starts a transaction

Specified by:
beginTransaction in interface Session
Returns:
The transaction

beginTransactionInternal

protected abstract Transaction beginTransactionInternal()

getTransaction

public Transaction getTransaction()
Description copied from interface: Session
Obtains the current transaction instance

Specified by:
getTransaction in interface Session
Returns:
The Transaction instance