class Persistence.Context
extends java.lang.Object
Special care is taken to not close a Hibernate session while other entities are dependent upon it for services.
Modifier and Type | Field and Description |
---|---|
private java.lang.Throwable |
beginTxTrace
Throwable which holds stack trace where last transaction began
|
private BufferManager |
bufferManager
Context-local buffer manager
|
private ChangeBroker |
changeBroker
Context-local ChangeBroker
|
private boolean |
closingSession
Flag to prevent nested session close attempts
|
private java.lang.Object |
dialectData
Optional, dialect-specific, context-local data
|
private boolean |
explicitTx
Is current transaction explicit?
|
private int |
implicitTxUseCount
Implicit transaction use count
|
private RecordLockContext |
lockContext
Context-local record lock context
|
private int |
openSessionCount
Count of open Hibernate sessions in this context
|
private ExpiryCache<Persistence.QueryKey,org.hibernate.Query> |
queryCache
Cache of HQL strings, max results, and start offsets to Hibernate queries
|
private boolean |
rereadNoLock
rereadnolock command-line parameter, by default deactivated
|
private org.hibernate.Session |
session
Active Hibernate session, if any
|
private java.util.WeakHashMap<SessionListener,Finalizable> |
sessionListeners
Session listener objects
|
private boolean |
traceTx
Enable tracing transaction starts
|
private org.hibernate.Transaction |
transaction
Active Hibernate transaction, if any
|
Modifier | Constructor and Description |
---|---|
private |
Context()
The default constructor reads rereadNoLock from directory.
|
Modifier and Type | Method and Description |
---|---|
(package private) org.hibernate.Transaction |
beginTransaction(boolean explicit)
Begin a new Hibernate transaction and store the instance for future
use.
|
(package private) void |
cleanup()
Cleanup hook invoked at the end of the current client context.
|
(package private) void |
closeSession(boolean error)
Close the Hibernate
Session object associated with this
context. |
private void |
closeSessionImpl(boolean error)
Close the Hibernate
Session object associated with this
context. |
(package private) void |
commit(boolean explicit)
Commit the current Hibernate transaction and possibly close the
current session.
|
(package private) void |
deregisterSessionListener(SessionListener listener)
Explicitly deregister a global session listener, which represents a dynamic temp-table
resource which has been deleted explicitly.
|
(package private) void |
enableTransactionTrace(boolean enable)
Set enable mode for transaction tracing.
|
(package private) void |
endTransaction(boolean explicit,
boolean error,
boolean rollback)
Discard the current transaction, which was previously committed or
rolled back.
|
(package private) void |
flushSession()
If an open session exists and the change broker indicates a flush is required, force a
session flush now.
|
(package private) java.lang.Throwable |
getBeginTxTrace()
Retrieve trace information about where the current transaction, if any, was started.
|
(package private) java.lang.Object |
getDialectData()
Get the context-local, dialect-specific data object, if any.
|
(package private) org.hibernate.Query |
getQuery(java.lang.String hql,
int maxResults,
int startOffset,
boolean readOnly)
Get a cached query for the given combination of HQL query string, maximum result rows,
and starting row offset, creating and caching a new query if no such query already is
cached.
|
(package private) RecordLockContext |
getRecordLockContext()
Get the record lock context associated with the current client
session.
|
(package private) org.hibernate.Session |
getSession()
Retrieve the Hibernate
Session object associated with
this context, creating it if necessary. |
(package private) org.hibernate.Session |
getSessionNoCreate()
Retrieve the Hibernate
Session object associated with
this context, if any. |
(package private) boolean |
isAutoCommit()
Indicate whether auto-commit is needed; this is the case for temp-tables being modified
outside of an explicit transaction.
|
(package private) boolean |
isFlushRequired(java.lang.String entity)
Determine whether a session flush is required for the given entity type.
|
(package private) boolean |
isFlushRequired(java.lang.String[] entities)
Determine whether a session flush is required for the given entity types.
|
(package private) boolean |
isRereadNoLock()
Checks if the rereadnolock 4GL command-line option is active for this client context.
|
(package private) boolean |
isTransactionOpen(boolean explicit)
Indicate whether a database-level transaction currently is open.
|
private void |
maybeFlushSession()
If an open session exists and the change broker indicates a flush is required, force a
session flush now.
|
private void |
notifySessionEvent(SessionListener.Event event)
Notify all registered session listeners that the current Hibernate session is about to
close or the current, implicit transaction is being committed.
|
(package private) int |
popImplicitTransaction()
Decrement the implicit transaction use counter.
|
(package private) int |
pushImplicitTransaction()
Increment the implicit transaction use counter and begin an implicit
transaction if none is currently active, and the use count currently
is at zero.
|
(package private) void |
registerSessionListener(SessionListener listener,
SessionListener.Scope scope)
Register a
SessionListener to receive session life
cycle event notifications. |
(package private) void |
rollback(boolean explicit,
boolean error)
Roll back the current Hibernate transaction and possibly close the
current session.
|
private final BufferManager bufferManager
private final RecordLockContext lockContext
private final ChangeBroker changeBroker
private final java.util.WeakHashMap<SessionListener,Finalizable> sessionListeners
private ExpiryCache<Persistence.QueryKey,org.hibernate.Query> queryCache
private org.hibernate.Session session
private org.hibernate.Transaction transaction
private int implicitTxUseCount
private boolean closingSession
private volatile int openSessionCount
private java.lang.Object dialectData
private boolean explicitTx
private boolean rereadNoLock
private boolean traceTx
private java.lang.Throwable beginTxTrace
private Context()
RecordLockContext getRecordLockContext()
org.hibernate.Query getQuery(java.lang.String hql, int maxResults, int startOffset, boolean readOnly) throws PersistenceException
hql
- HQL query string.maxResults
- The maximum number of results to be returned.startOffset
- Zero-based offset of first result to be returned (undefined results unless
hql
includes an order by
phrase).readOnly
- true
to execute the query in read-only mode, else
false
.PersistenceException
- if there was an error creating a Hibernate session.void commit(boolean explicit) throws PersistenceException
explicit
- true
if the current transaction is expected to
be explicit, false
if implicit.java.lang.IllegalStateException
- if no transaction is active in the current session; this
represents a programming error.PersistenceException
- if there is an error committing the transaction.void rollback(boolean explicit, boolean error) throws PersistenceException
explicit
- true
if the current transaction is expected to
be explicit, false
if implicit.error
- Indicates whether the rollback is due to an error.java.lang.IllegalStateException
- if no transaction is active in the current session; this
represents a programming error.PersistenceException
- if there is an error rolling back the transaction.void enableTransactionTrace(boolean enable)
enable
- true
to enable; false
to disable.org.hibernate.Transaction beginTransaction(boolean explicit) throws PersistenceException
explicit
- true
if an explicit transaction is being
requested; false
if the request is for an
implicit transaction.PersistenceException
- if there is an error beginning the new transaction or
committing an existing one.void endTransaction(boolean explicit, boolean error, boolean rollback) throws PersistenceException
For an implicit transaction, the currently scheduled, implicit commit task is cancelled and nulled out. In case of an error, the current session is marked broken, so it will be closed and replaced upon the next attempt to use it on the user sesion thread (the current thread must be the implicit commit timer if we get here for an implicit transaction).
For an explicit transaction, the current session is closed and the active dialect is allowed to do some post-transaction processing. Also, the implicit transaction use count is reset.
explicit
- Indicates whether the transaction being ended is explicit
or implicit.error
- Indicates whether the transaction ended with an error.rollback
- Indicates whether the transaction is being ended after a
rollback (as opposed to a commit).PersistenceException
- if an error occurs closing the session or performing
post-transaction processing.java.lang.Throwable getBeginTxTrace()
null
.Throwable
containing stack trace from last transaction start, or
null
.int pushImplicitTransaction() throws PersistenceException
PersistenceException
- if there is an error beginning a new transaction.int popImplicitTransaction()
boolean isFlushRequired(java.lang.String[] entities)
ChangeBroker
has tracked any
change to a DMO of any of the given types since the last flush.entities
- Array of entity names, or null
to check whether a flush is required for
any entity type.true
if a flush is required, else false
.boolean isFlushRequired(java.lang.String entity)
ChangeBroker
has tracked any
change to a DMO of the given type since the last flush.entity
- Name of an entity, or null
to check whether a flush is required for
any entity type.true
if a flush is required, else false
.void registerSessionListener(SessionListener listener, SessionListener.Scope scope)
SessionListener
to receive session life
cycle event notifications. The listener will be deregistered
according to the following rules at the earlier of:
scope
parameter:
ENCLOSING_EXTERNAL_SCOPE
CURRENT_SCOPE
NEXT_EXTERNAL_SCOPE
listener
- Session listener to be registered.scope
- SessionListener.Scope
enum indicating the scope
to which this listener should be registered. When the
scope ends, the listener is deregistered as described above.void deregisterSessionListener(SessionListener listener)
listener
- Listener to deregister.org.hibernate.Session getSessionNoCreate()
Session
object associated with
this context, if any.
If the current session is broken, return null
.
null
if none has been created.org.hibernate.Session getSession() throws PersistenceException
Session
object associated with
this context, creating it if necessary. The session can be closed
by closeSession(boolean)
.PersistenceException
- if an error occurs associating a detached record with the
new session.java.lang.IllegalArgumentException
- if database
has not been registered with the
persistence service.boolean isTransactionOpen(boolean explicit)
explicit
- true
to limit the check to an explicit
transaction; false
to check for either an
implicit or explicit transaction.true
if open, else false
.void closeSession(boolean error) throws PersistenceException
Session
object associated with this
context.
This implementation is tolerant of multiple closes on the same session. If the session was already closed or could not be found, the method simply returns.
error
- true
to indicate the session is closing due to
an error; false
to indicate the session is
closing in the course of normal processing.PersistenceException
- if an open session was found but the attempt to close it
failed.java.lang.Object getDialectData()
null
if no
such data exists.boolean isAutoCommit()
true
if auto-commit is needed, else false
.void flushSession() throws PersistenceException
PersistenceException
- if an error occurs during session flush.private void maybeFlushSession() throws PersistenceException
PersistenceException
- if an error occurs during session flush.private void closeSessionImpl(boolean error) throws PersistenceException
Session
object associated with this
context.
This implementation is tolerant of multiple closes on the same session. If the session was already closed or could not be found, the method simply returns.
All registered session listeners are notified before the session is closed.
error
- true
to indicate the session is closing due to
an error; false
to indicate the session is
closing in the course of normal processing.PersistenceException
- if an open session was found but the attempt to close it
failed, or if any error occurs during a registered session
listener's session closing processing.private void notifySessionEvent(SessionListener.Event event)
All registered listeners are given a chance to process this event, even if one or more
raise a PersistenceException
(there is no such guarantee for unchecked
exceptions). Each exception caught is logged.
event
- Session event type.void cleanup()
boolean isRereadNoLock()
true
if rereadnolock is specified for this client context