Skip to content

Instantly share code, notes, and snippets.

@satooshi
Last active January 1, 2019 12:52
Show Gist options
  • Select an option

  • Save satooshi/bfe4c01ac48a5f2b80db7cf4fba42a03 to your computer and use it in GitHub Desktop.

Select an option

Save satooshi/bfe4c01ac48a5f2b80db7cf4fba42a03 to your computer and use it in GitHub Desktop.

Revisions

  1. satooshi revised this gist Jan 1, 2019. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions how_to_begin_transaction.cr
    Original file line number Diff line number Diff line change
    @@ -4,11 +4,11 @@ DB.open "postgres://postgres:postgres@localhost:5432/blog_development" do |db|
    db.transaction do |tx|
    conn = tx.connection

    pp conn.scalar "select count(*) from articles"
    pp conn.scalar "select count(*) from articles" # => 5
    pp conn.exec "insert into articles (title, url, markdown, created_at, updated_at) values ('title', 'url', 'markdown', now(), now())"
    pp db.scalar "select count(*) from articles"
    pp conn.scalar "select count(*) from articles"
    pp db.scalar "select count(*) from articles" # => 5 (because this established another connection)
    pp conn.scalar "select count(*) from articles" # => 6
    tx.rollback
    pp conn.scalar "select count(*) from articles"
    pp conn.scalar "select count(*) from articles" # => 5 (rollbacked properly)
    end
    end
  2. satooshi created this gist Jan 1, 2019.
    14 changes: 14 additions & 0 deletions how_to_begin_transaction.cr
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    require "pg"

    DB.open "postgres://postgres:postgres@localhost:5432/blog_development" do |db|
    db.transaction do |tx|
    conn = tx.connection

    pp conn.scalar "select count(*) from articles"
    pp conn.exec "insert into articles (title, url, markdown, created_at, updated_at) values ('title', 'url', 'markdown', now(), now())"
    pp db.scalar "select count(*) from articles"
    pp conn.scalar "select count(*) from articles"
    tx.rollback
    pp conn.scalar "select count(*) from articles"
    end
    end