public class JPAStorageService extends AbstractStorageService
StorageService that uses JPA to persist to a database.| Modifier and Type | Field and Description |
|---|---|
private javax.persistence.EntityManagerFactory |
entityManagerFactory
Entity manager factory.
|
private org.slf4j.Logger |
log
Class logger.
|
private int |
transactionRetry
Number of times to retry a transaction if it rolls back.
|
| Constructor and Description |
|---|
JPAStorageService(javax.persistence.EntityManagerFactory factory)
Creates a new JPA storage service.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
create(java.lang.String context,
java.lang.String key,
java.lang.String value,
java.lang.Long expiration)
Creates a new record in the store with an expiration.
|
boolean |
delete(java.lang.String context,
java.lang.String key)
Deletes an existing record from the store.
|
void |
deleteContext(java.lang.String context)
Forcibly removes all records in a given context along with any
associated resources devoted to maintaining the context.
|
protected void |
deleteContextImpl(java.lang.String context,
java.lang.Long expiration)
Deletes every record with the supplied context.
|
protected boolean |
deleteImpl(java.lang.Long version,
java.lang.String context,
java.lang.String key)
Deletes the record matching the supplied parameters.
|
boolean |
deleteWithVersion(long version,
java.lang.String context,
java.lang.String key)
Deletes an existing record from the store if it currently has a specified version.
|
protected void |
doDestroy() |
private <T> java.util.List<T> |
executeNamedQuery(javax.persistence.EntityManager manager,
java.lang.String query,
java.util.Map<java.lang.String,java.lang.Object> params,
java.lang.Class<T> clazz,
javax.persistence.LockModeType lockMode)
Executes the supplied named query.
|
protected java.util.TimerTask |
getCleanupTask()
Returns a cleanup task function to schedule for background cleanup.
|
int |
getTransactionRetry()
Returns the number of times a transaction will be retried if a
RollbackException is encountered. |
StorageRecord |
read(java.lang.String context,
java.lang.String key)
Returns an existing record from the store, if one exists.
|
net.shibboleth.utilities.java.support.collection.Pair<java.lang.Long,StorageRecord> |
read(java.lang.String context,
java.lang.String key,
long version)
Returns an existing record from the store, along with its version.
|
java.util.List<StorageRecord> |
readAll()
Returns all records from the store.
|
java.util.List<StorageRecord> |
readAll(java.lang.String context)
Returns all records from the store for the supplied context.
|
java.util.List<java.lang.String> |
readContexts()
Returns all contexts from the store.
|
protected net.shibboleth.utilities.java.support.collection.Pair<java.lang.Long,StorageRecord> |
readImpl(java.lang.String context,
java.lang.String key,
java.lang.Long version)
Reads the record matching the supplied parameters.
|
void |
reap(java.lang.String context)
Manually trigger a cleanup of expired records.
|
void |
setTransactionRetry(int retry)
Sets the number of times a transaction will be retried.
|
boolean |
update(java.lang.String context,
java.lang.String key,
java.lang.String value,
java.lang.Long expiration)
Updates an existing record in the store.
|
void |
updateContextExpiration(java.lang.String context,
java.lang.Long expiration)
Updates the expiration time of all records in the context.
|
boolean |
updateExpiration(java.lang.String context,
java.lang.String key,
java.lang.Long expiration)
Updates expiration of an existing record in the store.
|
protected java.lang.Long |
updateImpl(java.lang.Long version,
java.lang.String context,
java.lang.String key,
java.lang.String value,
java.lang.Long expiration)
Updates the record matching the supplied parameters.
|
java.lang.Long |
updateWithVersion(long version,
java.lang.String context,
java.lang.String key,
java.lang.String value,
java.lang.Long expiration)
Updates an existing record in the store, if a version matches.
|
create, create, delete, deleteWithVersion, doInitialize, getCapabilities, getCleanupInterval, getCleanupTaskTimer, getContextSize, getKeySize, getValueSize, read, setCleanupInterval, setCleanupTaskTimer, setContextSize, setKeySize, setValueSize, update, update, updateExpiration, updateWithVersion, updateWithVersionsetIdgetIddestroy, initialize, isDestroyed, isInitialized@Nonnull private final org.slf4j.Logger log
@Nonnull private final javax.persistence.EntityManagerFactory entityManagerFactory
@NonNegative private int transactionRetry
public JPAStorageService(@Nonnull
javax.persistence.EntityManagerFactory factory)
factory - entity manager factorypublic int getTransactionRetry()
RollbackException is encountered.public void setTransactionRetry(int retry)
retry - number of transaction retriesprotected void doDestroy()
doDestroy in class AbstractStorageServicepublic boolean create(@Nonnull @NotEmpty
java.lang.String context,
@Nonnull @NotEmpty
java.lang.String key,
@Nonnull @NotEmpty
java.lang.String value,
@Nullable @Positive
java.lang.Long expiration)
throws java.io.IOException
context - a storage context labelkey - a key unique to contextvalue - value to storeexpiration - expiration for record, or nulljava.io.IOException - if fatal errors occur in the insertion process@Nonnull @NonnullElements public java.util.List<StorageRecord> readAll() throws java.io.IOException
java.io.IOException - if errors occur in the read process@Nonnull @NonnullElements public java.util.List<StorageRecord> readAll(@Nonnull @NotEmpty java.lang.String context) throws java.io.IOException
context - a storage context labeljava.io.IOException - if errors occur in the read process@Nonnull
@NonnullElements
public java.util.List<java.lang.String> readContexts()
throws java.io.IOException
java.io.IOException - if errors occur in the read process@Nullable public StorageRecord read(@Nonnull @NotEmpty java.lang.String context, @Nonnull @NotEmpty java.lang.String key) throws java.io.IOException
context - a storage context labelkey - a key unique to contextjava.io.IOException - if errors occur in the read process@Nonnull public net.shibboleth.utilities.java.support.collection.Pair<java.lang.Long,StorageRecord> read(@Nonnull @NotEmpty java.lang.String context, @Nonnull @NotEmpty java.lang.String key, @Positive long version) throws java.io.IOException
The first member of the pair returned will contain the version of the record in the store, or will be null if no record exists. The second member will contain the record read back. If null, the record either didn't exist (if the first member was also null) or the record was the same version as that supplied by the caller.
context - a storage context labelkey - a key unique to contextversion - only return record if newer than supplied versionjava.io.IOException - if errors occur in the read process@Nonnull protected net.shibboleth.utilities.java.support.collection.Pair<java.lang.Long,StorageRecord> readImpl(@Nonnull @NotEmpty java.lang.String context, @Nonnull @NotEmpty java.lang.String key, @Positive java.lang.Long version) throws java.io.IOException
context - to search forkey - to search forversion - to matchjava.io.IOException - if errors occur in the read processpublic boolean update(@Nonnull @NotEmpty
java.lang.String context,
@Nonnull @NotEmpty
java.lang.String key,
@Nonnull @NotEmpty
java.lang.String value,
@Nullable @Positive
java.lang.Long expiration)
throws java.io.IOException
context - a storage context labelkey - a key unique to contextvalue - updated valueexpiration - expiration for record, or nulljava.io.IOException - if errors occur in the update process@Nullable
public java.lang.Long updateWithVersion(@Positive
long version,
@Nonnull @NotEmpty
java.lang.String context,
@Nonnull @NotEmpty
java.lang.String key,
@Nonnull @NotEmpty
java.lang.String value,
@Nullable @Positive
java.lang.Long expiration)
throws java.io.IOException,
VersionMismatchException
version - only update if the current version matches this valuecontext - a storage context labelkey - a key unique to contextvalue - updated valueexpiration - expiration for record, or nulljava.io.IOException - if errors occur in the update processVersionMismatchException - if the record has already been updated to a newer versionpublic boolean updateExpiration(@Nonnull @NotEmpty
java.lang.String context,
@Nonnull @NotEmpty
java.lang.String key,
@Nullable @Positive
java.lang.Long expiration)
throws java.io.IOException
context - a storage context labelkey - a key unique to contextexpiration - expiration for record, or nulljava.io.IOException - if errors occur in the update process@Nullable
protected java.lang.Long updateImpl(@Nullable
java.lang.Long version,
@Nonnull @NotEmpty
java.lang.String context,
@Nonnull @NotEmpty
java.lang.String key,
@Nonnull @NotEmpty
java.lang.String value,
@Nullable @Positive
java.lang.Long expiration)
throws java.io.IOException,
VersionMismatchException
version - to checkcontext - to search forkey - to search forvalue - to updateexpiration - to updatejava.io.IOException - if errors occur in the update processVersionMismatchException - if the record found contains a version that does not match the parameterpublic boolean deleteWithVersion(@Positive
long version,
@Nonnull @NotEmpty
java.lang.String context,
@Nonnull @NotEmpty
java.lang.String key)
throws java.io.IOException,
VersionMismatchException
version - record version to deletecontext - a storage context labelkey - a key unique to contextjava.io.IOException - if errors occur in the deletion processVersionMismatchException - if the record has already been updated to a newer versionpublic boolean delete(@Nonnull @NotEmpty
java.lang.String context,
@Nonnull @NotEmpty
java.lang.String key)
throws java.io.IOException
context - a storage context labelkey - a key unique to contextjava.io.IOException - if errors occur in the deletion processprotected boolean deleteImpl(@Nullable @Positive
java.lang.Long version,
@Nonnull @NotEmpty
java.lang.String context,
@Nonnull @NotEmpty
java.lang.String key)
throws java.io.IOException,
VersionMismatchException
version - to checkcontext - to search forkey - to search forjava.io.IOException - if errors occur in the delete processVersionMismatchException - if the record found contains a version that does not match the parameterpublic void updateContextExpiration(@Nonnull @NotEmpty
java.lang.String context,
@Nullable @Positive
java.lang.Long expiration)
throws java.io.IOException
context - a storage context labelexpiration - a new expiration timestamp, or nulljava.io.IOException - if errors occur in the cleanup processpublic void deleteContext(@Nonnull @NotEmpty
java.lang.String context)
throws java.io.IOException
context - a storage context labeljava.io.IOException - if errors occur in the cleanup processpublic void reap(@Nonnull @NotEmpty
java.lang.String context)
throws java.io.IOException
context - a storage context labeljava.io.IOException - if errors occur in the cleanup processprotected void deleteContextImpl(@Nonnull @NotEmpty
java.lang.String context,
@Nonnull
java.lang.Long expiration)
throws java.io.IOException
context - to deleteexpiration - (optional) to require for deletionjava.io.IOException - if errors occur in the delete processprivate <T> java.util.List<T> executeNamedQuery(@Nonnull
javax.persistence.EntityManager manager,
@Nonnull @NotEmpty
java.lang.String query,
@Nonnull
java.util.Map<java.lang.String,java.lang.Object> params,
@Nonnull
java.lang.Class<T> clazz,
@Nonnull
javax.persistence.LockModeType lockMode)
throws java.io.IOException
T - type of entity to returnmanager - to execute the queryquery - to executeparams - parameters for the queryclazz - type of entity to returnlockMode - of the transactionjava.io.IOException - if an error occurs executing the query@Nullable protected java.util.TimerTask getCleanupTask()
The default implementation does not supply one.
getCleanupTask in class AbstractStorageService