Lets a custom app manage its own Hibernate transaction and per-entity locking from JS, exposed as the transactionManager variable. A controller function can begin, commit or roll back the current request's transaction explicitly, or use runInTransaction to run a callback inside one, optionally taking a local lock on a single entity so concurrent requests cannot process it at the same time.
Methods
begin()
Returns: void
Starts a new Hibernate transaction on the current request and stores it as a request attribute so a later call to commit or rollback on this manager can find it. Does nothing if there is no current request.
commit()
Returns: void
Commits the transaction previously started on the current request with begin, if one is present, and removes it from the request attributes. Does nothing if no transaction is in progress.
rollback()
Returns: void
Rolls back the transaction previously started on the current request with begin, if one is present, and removes it from the request attributes. Does nothing if no transaction is in progress.
runInTransaction(Object som)
Returns: void
Runs the given JS function inside a Hibernate session and transaction, reusing an existing transaction if one is already running on the current session.
| Parameter | Description |
|---|---|
som | the JS function to invoke inside the transaction |
runInTransaction(Object entity, Object function)
Returns: void
Runs the given JS function inside a Hibernate session and transaction, reusing an existing transaction if one is already running. When an entity is supplied, a local lock keyed on that entity is held for the duration, so this method throws if another operation already holds the lock for the same entity on this server.
| Parameter | Description |
|---|---|
entity | a String, Long, Integer, or any object implementing Relational, such as Profile or Organisation, used as the lock key; if null no lock is taken |
function | the JS function to invoke inside the transaction |
lockEntityLocally(Object entity, Object som)
Returns: void
Runs the given JS function while holding a local lock on the given entity, preventing this method from being run concurrently for the same entity on this server only. Unlike runInTransaction, this does not open a Hibernate transaction itself.
| Parameter | Description |
|---|---|
entity | a String, Long, Integer, or any object implementing Relational, used as the lock key |
som | the JS function to invoke while the lock is held |
executeInTransaction(Supplier<T> r)
Returns: T
Runs the given supplier inside a Hibernate transaction and returns its result.
| Parameter | Description |
|---|---|
r | the supplier to invoke inside the transaction |
setRollbackOnly()
Returns: void
Marks the current Hibernate transaction so that it is rolled back rather than committed, without ending it immediately. Call this from inside a transaction to ensure it gets rolled back; the rollback occurs when the transaction scope ends.