All Downloads are FREE. Search and download functionalities are using the official Maven repository.

no.ks.kes.mongodb.MongoDBTransactionAwareCollectionFactory.kt Maven / Gradle / Ivy

package no.ks.kes.mongodb

import com.mongodb.client.MongoCollection
import org.bson.Document
import org.springframework.data.mongodb.MongoDatabaseFactory
import org.springframework.data.mongodb.MongoDatabaseUtils
import org.springframework.data.mongodb.MongoTransactionManager
import org.springframework.data.mongodb.SessionSynchronization
import org.springframework.transaction.support.TransactionTemplate

class MongoDBTransactionAwareCollectionFactory(private val factory: MongoDatabaseFactory) {

    fun getTransactionTemplate() = TransactionTemplate(getMongoTransactionManager())

    fun getMongoTransactionManager() = MongoTransactionManager(factory)

    fun getCollection(collectionName: String): MongoCollection =
        MongoDatabaseUtils.getDatabase(factory, SessionSynchronization.ON_ACTUAL_TRANSACTION)
            .getCollection(collectionName)

    fun getCollection(collectionName: String, databaseName: String): MongoCollection =
        MongoDatabaseUtils.getDatabase(databaseName, factory, SessionSynchronization.ON_ACTUAL_TRANSACTION)
            .getCollection(collectionName)

    fun initCollection(collectionName: String) {
        if (!factory.mongoDatabase.listCollectionNames().contains(collectionName)) {
            factory.mongoDatabase.createCollection(collectionName)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy