public interface Session extends Comparable<Session>
| Modifier and Type | Method and Description |
|---|---|
default void |
applyTo(Collection<? extends SessionDependable> list)
Applies this session to a collection of session dependables.
|
default void |
applyTo(SessionDependable obj)
Applies this session to a session dependable object.
The method must invoke obj.setSession() only if the session really differs. |
static void |
assertCurrentSessionValid()
Asserts that a thread local session is set.
|
long |
begin()
Starts a transaction.
Does nothing if a transaction is already running! |
long |
begin(String txName)
Starts a transaction.
Does nothing if a transaction is already running! |
default void |
clearCurrent()
Clears the current session if this session is current.
|
Session |
clone()
Clones a session.
If the session is closed, a PersistenceException is thrown. The cloned session will always be non-grouped. |
void |
close()
Close a session.
Closing an already closed session is okay and ignored. |
boolean |
commit(long txVoucher)
Commits a transaction if the corresponding begin() has started it.
|
static Session |
getCurrentSession()
Gets the session used by the current thread.
|
SessionTaskDispatcher |
getDispatcher()
Gets the dispatcher for this session.
The dispatcher can be used to submit asynchroneous tasks in a serial manner. |
int |
getInstanceNumber()
Gets the unique instance number of this Session.
|
long |
getKeepAliveInterval()
Gets the auto keep alive interval.
|
String |
getName()
Gets the session's name.
|
Thread |
getOwnerThread()
Gets the owner thread.
|
SessionPool |
getPool()
Gets the session pool.
|
<T extends Serializable> |
getProperty(String key)
Gets a session property.
|
RemoteSession |
getRemoteSession()
Gets the remote session.
|
static Session |
getSession()
Gets the thread local session.
|
int |
getSessionGroupId()
Gets the session's group ID.
|
int |
getSessionId()
Gets the session ID.
|
SessionInfo |
getSessionInfo()
Gets the current user info.
|
String |
getTxName()
Gets the transaction name if a tx is currently running.
|
String |
getUrl()
Gets the url.
|
int |
groupWith(int sessionId)
Assigns this session to another session building a session group.
|
boolean |
isAlive()
Checks whether the session is still in use.
|
default boolean |
isCurrent()
Returns whether this session is the thread's local current session.
|
boolean |
isOpen()
Gets the connection state.
|
boolean |
isPooled()
Checks whether this session is pooled.
|
boolean |
isRemote()
Gets the persistence locality of this session.
Sessions may refer to persistence layers not running in the current JVM. |
boolean |
isTxRunning()
Determines whether a tx is currently running.
|
default void |
makeCurrent()
Makes this session the current session for this thread.
|
void |
open()
Opens a session.
Opening an already open session throws a PersistenceException. |
void |
releaseSavepoint(SavepointHandle handle)
Removes the specified
Savepoint and subsequent Savepoint objects from the current
transaction. |
void |
reOpen()
Re-opens a session.
If the session is open, it will be closed first. If the session belongs to a group, the re-opened session will be grouped as well. |
boolean |
rollback(long txVoucher)
Rolls back a transaction if the corresponding begin() has started it.
The rollback is logged as INFO along with all statements. |
void |
rollback(SavepointHandle handle)
Undoes all changes made after the given
Savepoint object was set. |
boolean |
rollbackSilently(long txVoucher)
Rolls back a transaction if the corresponding begin() has started it.
Same as rollback(long) but without logging. |
void |
setAlive(boolean alive)
Sets the session's alive state.
|
static void |
setCurrentSession(Session session)
Sets the session used by the current thread.
The session is stored as ThreadLocal. |
void |
setKeepAliveInterval(long keepAliveInterval)
Sets the auto keep alive interval.
If set, the session will be setAlive every keepAliveInterval milliseconds by the SessionKeepAliveDaemon. |
void |
setOwnerThread(Thread ownerThread)
Sets the exclusive owner thread.
|
<T extends Serializable> |
setProperty(String key,
T value)
Sets a session property.
Session properties are application specific. For remote sessions, the properties are delegated to the corresponding local session at the remote side. |
SavepointHandle |
setSavepoint()
Creates an unnamed savepoint in the current transaction.
|
SavepointHandle |
setSavepoint(String name)
Creates a savepoint with the given name in the current transaction.
|
<T,E extends Exception> |
transaction(Provider<T,E> txe)
Runs code within a transaction.
The transaction name is derived from the enclosing method. |
<T,E extends Exception> |
transaction(String txName,
Provider<T,E> txe)
Runs code within a transaction.
|
compareTostatic Session getCurrentSession()
static void setCurrentSession(Session session)
ThreadLocal.session - the dbstatic void assertCurrentSessionValid()
PersistenceException - if thread-local session is nullstatic Session getSession()
PersistenceException - if thread-local session is nulldefault void makeCurrent()
default boolean isCurrent()
default void clearCurrent()
PersistenceException - if this is not the current sessiondefault void applyTo(SessionDependable obj)
obj - the database object, null if ignoredefault void applyTo(Collection<? extends SessionDependable> list)
list - the collection of session dependablesString getName()
int getSessionId()
int getSessionGroupId()
int groupWith(int sessionId)
sessionId - the ID of the session to build a group withvoid open()
PersistenceException.void close()
Notice: a non-blocking close can be requested by setKeepAliveInterval(long) with
an interval < 0.
Session clone()
void reOpen()
SessionInfo getSessionInfo()
long begin(String txName)
txName - is the optional transaction name, null if nonelong begin()
boolean commit(long txVoucher)
txVoucher - the transaction voucher, 0 if ignore (nested tx)boolean rollback(long txVoucher)
txVoucher - the transaction voucher, 0 if ignore (nested tx)boolean rollbackSilently(long txVoucher)
rollback(long) but without logging. Used for expected rollbacks
or rollbacks not related to persistence exceptions.txVoucher - the transaction voucher, 0 if ignore (nested tx)boolean isTxRunning()
String getTxName()
isTxRunning()<T,E extends Exception> T transaction(String txName, Provider<T,E> txe) throws E extends Exception
Provided as an alternative to the @Transaction-interceptor, if the class holding the implementation of the
transaction is not an Interceptable.
Usage:
return session.transaction(() -> {
..do something..
return whatever;
});
whatever is returned by session.transaction(). The type is determined
by type inference automatically. If there is nothing to return, null must be returned by the envelope:
session.transaction(() -> {
..do something..
return null;
});
The exception is optional. If given, it can be caught as if there was no lambda.
try {
session.transaction(() -> {
throw new MyException("blah");
});
}
catch (MyException ex) {
...
}
T - the return typeE - the exception typetxName - the transaction name, null will derive it from enclosing methodtxe - the transaction supplierE - the exception typeE extends Exception<T,E extends Exception> T transaction(Provider<T,E> txe) throws E extends Exception
T - the return typeE - the exception typetxe - the transaction supplierE - the exception typeE extends ExceptionSavepointHandle setSavepoint()
SavepointHandle setSavepoint(String name)
name - the savepoint namevoid rollback(SavepointHandle handle)
Savepoint object was set.handle - the savepoint handlevoid releaseSavepoint(SavepointHandle handle)
Savepoint and subsequent Savepoint objects from the current
transaction.handle - the savepoint handleint getInstanceNumber()
void setOwnerThread(Thread ownerThread)
Allows to detect other threads accidently using this session.
ownerThread - the owner thread, null to clearThread getOwnerThread()
void setAlive(boolean alive)
alive - is true to signal it's alive, false to clearboolean isAlive()
void setKeepAliveInterval(long keepAliveInterval)
SessionKeepAliveDaemon.
Useful for remote sessions to prevent them from being closed by
the remote server due to timeout.keepAliveInterval - the interval in ms, 0 to disable, < 0 to close sessionlong getKeepAliveInterval()
boolean isPooled()
SessionPool getPool()
boolean isOpen()
boolean isRemote()
getRemoteSession()RemoteSession getRemoteSession()
PersistenceException - if session is not remoteisRemote()String getUrl()
SessionTaskDispatcher getDispatcher()
<T extends Serializable> T setProperty(String key, T value)
T - the value's typekey - the property keyvalue - the property value<T extends Serializable> T getProperty(String key)
T - the value's typekey - the property keyTentackle - distributed, domain- and model-driven