fun transactional()

in dnq-transient-store/src/main/kotlin/com/jetbrains/teamsys/dnq/database/TransientEntityStoreExt.kt [23:71]


    fun <T> transactional(
            store: TransientEntityStore,
            readonly: Boolean = false,
            queryCancellingPolicy: QueryCancellingPolicy? = null,
            isNew: Boolean = false,
            block: (TransientStoreSession) -> T
    ): T {
        val superSession = store.threadSession
        val superIsSuspended: Boolean
        if (isNew) {
            store.suspendThreadSession()
            superIsSuspended = true
        } else {
            if (superSession == null) {
                superIsSuspended = false
            } else {
                if (superSession.isReadonly != readonly) {
                    store.suspendThreadSession()
                    superIsSuspended = true
                } else {
                    return block(superSession)
                }
            }
        }
        try {
            val newSession = store.beginSession(readonly, queryCancellingPolicy)
            var wasEx = true
            try {
                val result = block(newSession)
                wasEx = false
                return result
            } finally {
                if ((superSession == null || superIsSuspended) && newSession.isOpened) {
                    if (wasEx) {
                        newSession.abort()
                    } else {
                        doCommit(newSession)
                    }
                }
            }
        } finally {
            if (superIsSuspended) {
                store.resumeSession(superSession)
                if (queryCancellingPolicy != null && superSession != null) {
                    superSession.queryCancellingPolicy = queryCancellingPolicy
                }
            }
        }
    }