All Projects → michaelbull → kotlin-coroutines-jdbc

michaelbull / kotlin-coroutines-jdbc

Licence: ISC license
A library for interacting with blocking JDBC drivers using Kotlin Coroutines.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to kotlin-coroutines-jdbc

neo4j-php-client
Php client and driver for neo4j database
Stars: ✭ 95 (+137.5%)
Mutual labels:  transaction, connection
snap
Snap Programming Language
Stars: ✭ 20 (-50%)
Mutual labels:  functional, coroutines
koru
Simple coroutine wrappers for Kotlin Native. Generated from annotations. Compatible with RxSwift, Combine etc.
Stars: ✭ 141 (+252.5%)
Mutual labels:  coroutines, suspend
func-dependency-injection-go
Dependency injection example using higher order functions
Stars: ✭ 26 (-35%)
Mutual labels:  functional, higher-order-functions
hermes-js
Universal action dispatcher for JavaScript apps
Stars: ✭ 15 (-62.5%)
Mutual labels:  functional, transaction
Kotlin Flow Extensions
Extensions to the Kotlin Flow library.
Stars: ✭ 404 (+910%)
Mutual labels:  functional, coroutines
SeLite
Automated database-enabled navigation ✔️ of web applications
Stars: ✭ 34 (-15%)
Mutual labels:  functional
grand central
State-management and action-dispatching for Ruby apps
Stars: ✭ 20 (-50%)
Mutual labels:  functional
functional-structures-refactoring-kata
Starting code and proposed solution for Functional Structures Refactoring Kata
Stars: ✭ 31 (-22.5%)
Mutual labels:  functional
pecan
Macro-based coroutines for Haxe
Stars: ✭ 37 (-7.5%)
Mutual labels:  coroutines
lambda-zero
A minimalist pure lazy functional programming language
Stars: ✭ 65 (+62.5%)
Mutual labels:  functional
bitski-ios
Bitski iOS SDK
Stars: ✭ 18 (-55%)
Mutual labels:  transaction
fnts
λ Minimal Functional Programming Utilities for TypeScript & JavaScript
Stars: ✭ 75 (+87.5%)
Mutual labels:  functional
AWR.Athena
Short R Wrapper for Athena JDBC connections
Stars: ✭ 23 (-42.5%)
Mutual labels:  jdbc
BuBBLE
A DSL/LISP dialect written in Haskell
Stars: ✭ 20 (-50%)
Mutual labels:  functional
pipe
Functional Pipeline in Go
Stars: ✭ 30 (-25%)
Mutual labels:  functional
L3-37
Yet another Tokio connection pooler. May cause robot uprising.
Stars: ✭ 53 (+32.5%)
Mutual labels:  connection
transmute
kind of like lodash but works with Immutable
Stars: ✭ 35 (-12.5%)
Mutual labels:  functional
BOHM1.1
Bologna Optimal Higher-Order Machine, Version 1.1
Stars: ✭ 45 (+12.5%)
Mutual labels:  higher-order-functions
pixel
A lightweight image loader for Android backed by Kotlin Coroutines.
Stars: ✭ 79 (+97.5%)
Mutual labels:  coroutines

kotlin-coroutines-jdbc

Maven Central CI Status License

A library for interacting with blocking JDBC drivers using Kotlin Coroutines.

Use of this library allows you to offload blocking JDBC calls to a dedicated CoroutineDispatcher (e.g. Dispatchers.IO), thus suspending your coroutine and freeing your thread for other work while waiting.

Installation

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.michael-bull.kotlin-coroutines-jdbc:kotlin-coroutines-jdbc:1.0.2")
}

Introduction

The primary higher-order function exposed by the library is the transaction function.

suspend inline fun <T> transaction(crossinline block: suspend CoroutineScope.() -> T): T

Calling this function with a specific suspending block will run the block in the context of a CoroutineTransaction.

Calls to transaction can be nested inside another, with each child re-using the first CoroutineTransaction. Only the outermost call will either commit or rollback the transaction.

Starting a fresh transaction will add a CoroutineTransaction to the current CoroutineContext. Transactions cannot be re-used after completion and attempting to do so will result in a runtime failure.

A transaction will establish a new Connection if an open one does not already exist in the active CoroutineContext. If the transaction does establish a new Connection, it will attempt to close it upon completion.

An active CoroutineConnection is accessible from the current CoroutineContext. The connection from the context can be used to prepare statements.

Example

import com.github.michaelbull.jdbc.context.CoroutineDataSource
import com.github.michaelbull.jdbc.context.connection
import com.github.michaelbull.jdbc.transaction
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import javax.sql.DataSource
import kotlin.coroutines.coroutineContext

class Example(dataSource: DataSource) {

    private val scope = CoroutineScope(Dispatchers.IO + CoroutineDataSource(dataSource))
    private val customers = CustomerRepository()

    fun query() {
        scope.launchTransaction()
    }

    private fun CoroutineScope.launchTransaction() = launch {
        val customers = addThenFindAllCustomers()
        customers.forEach(::println)
    }

    private suspend fun addThenFindAllCustomers(): List<String> {
        return transaction {
            customers.add("John Doe")
            customers.findAll()
        }
    }
}

class CustomerRepository {

    suspend fun add(name: String) {
        coroutineContext.connection.prepareStatement("INSERT INTO customers VALUES (?)").use { stmt ->
            stmt.setString(1, name)
            stmt.executeUpdate()
        }
    }

    suspend fun findAll(): List<String> {
        val customers = mutableListOf<String>()

        coroutineContext.connection.prepareStatement("SELECT name FROM customers").use { stmt ->
            stmt.executeQuery().use { rs ->
                while (rs.next()) {
                    customers += rs.getString("name")
                }
            }
        }

        return customers
    }
}

Further Reading

Contributing

Bug reports and pull requests are welcome on GitHub.

License

This project is available under the terms of the ISC license. See the LICENSE file for the copyright information and licensing terms.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].