#' @title Check whether connection is in transaction
#'
#' @description
#' Transactions are important to ensuring that database writes are predictable.
#'
#' @param conn (DBIConnection) object
#'
#' @family Extra DBI Methods
#' @export
dbInTransaction <- function(conn) {
UseMethod("dbInTransaction")
}
#' @export
dbInTransaction.PqConnection <- function(conn) {
identical(
dbGetQuery(conn, "SELECT txid_current()"),
dbGetQuery(conn, "SELECT txid_current()")
)
}
#' @export
dbInTransaction.default <- function(conn) {
# Attempt to start a transaction
start <- try(dbBegin(conn), silent = TRUE)
if (inherits(start, "try-error")) {
# If failed, then inside a transaction, return TRUE
TRUE
} else {
# If not, then rollback the transaction create and return FALSE
dbRollback(conn)
FALSE
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.