R: Transaction management.

postgres-transactions {RPostgres} | R Documentation

## Transaction management.

### Description

`dbBegin()` starts a transaction. `dbCommit()` and `dbRollback()` end the transaction by either committing or rolling back the changes.

### Usage

    ## S4 method for signature 'PqConnection'
    dbBegin(conn, ...)

    ## S4 method for signature 'PqConnection'
    dbCommit(conn, ...)

    ## S4 method for signature 'PqConnection'
    dbRollback(conn, ...)

### Arguments

`conn` | a PqConnection object, produced by `DBI::dbConnect()`
`...` | Unused, for extensibility.

### Value

A boolean, indicating success or failure.

### Examples

    # For running the examples on systems without PostgreSQL connection:
    run <- postgresHasDefault()

    library(DBI)
    if (run) con <- dbConnect(RPostgres::Postgres())
    if (run) dbWriteTable(con, "USarrests", datasets::USArrests, temporary = TRUE)
    if (run) dbGetQuery(con, 'SELECT count(*) from "USarrests"')

    if (run) dbBegin(con)
    if (run) dbExecute(con, 'DELETE from "USarrests" WHERE "Murder" > 1')
    if (run) dbGetQuery(con, 'SELECT count(*) from "USarrests"')
    if (run) dbRollback(con)

    # Rolling back changes leads to original count
    if (run) dbGetQuery(con, 'SELECT count(*) from "USarrests"')

    if (run) dbRemoveTable(con, "USarrests")
    if (run) dbDisconnect(con)

[Package RPostgres version 1.1.1 Index]