@CompileStatic class GormSharedSessionMongoTransactionManager extends DatastoreTransactionManager
A PlatformTransactionManager that drives a single GORM
MongoDB transaction and additionally exposes its com.mongodb.client.ClientSession to Spring Data MongoDB, so
that GORM operations and MongoTemplate/repository operations executed within one
@Transactional method participate in the same MongoDB transaction.
It extends DatastoreTransactionManager — inheriting GORM's begin/commit/rollback,
session binding and flushing — and, once GORM has started its com.mongodb.client.ClientSession, binds a
Spring Data MongoResourceHolder referencing that same session, keyed by the
MongoDatabaseFactory. Spring Data's MongoTemplate discovers that holder via the
thread-bound resources and runs inside the session, so a single commit (or abort) — driven by
GORM — applies to both stacks atomically.
This requires GORM server-side transactions to be enabled
(grails.mongodb.transactional = true); without an active com.mongodb.client.ClientSession there is
nothing to share and Spring Data operations run outside of a transaction as before.
Propagation: as for GORM's DatastoreTransactionManager. A transaction
started inside another joins it, for Spring Data as for GORM. REQUIRES_NEW suspends both:
the new transaction runs in a GORM session and com.mongodb.client.ClientSession of its own, which its
MongoTemplate calls use, and the outer transaction's are restored when it completes. A
transaction begun in a session of its own (withNewSession) likewise runs its
MongoTemplate calls in its own com.mongodb.client.ClientSession, or, read-only, without one, and the
surrounding transaction's is put back when it completes. NESTED is not supported.
| Constructor and description |
|---|
GormSharedSessionMongoTransactionManager(MongoDatastore datastore, MongoDatabaseFactory databaseFactory) |
| Type Params | Return Type | Name and description |
|---|---|---|
|
protected void |
doBegin(java.lang.Object transaction, TransactionDefinition definition) |
|
protected void |
doCleanupAfterCompletion(java.lang.Object transaction) |
|
protected void |
doResume(java.lang.Object transaction, java.lang.Object suspendedResources) |
|
protected java.lang.Object |
doSuspend(java.lang.Object transaction) |
| Methods inherited from class | Name |
|---|---|
class DatastoreTransactionManager |
doBegin, doCleanupAfterCompletion, doCommit, doGetTransaction, doResume, doRollback, doSetRollbackOnly, doSuspend, getDatastore, isExistingTransaction, setDatastore, setDatastoreManagedSession |