# Using Postgres transactions

**URL:** <https://forum.crystal-lang.org/t/using-postgres-transactions/829>\
**Category:** Help & Support\
**Created:** [June 3, 2019, 7:47pm UTC](https://forum.crystal-lang.org/t/using-postgres-transactions/829 "2019-06-03T19:47:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![curious2learn](https://avatars.discourse-cdn.com/v4/letter/c/5daacb/32.png) [@curious2learn](https://forum.crystal-lang.org/u/curious2learn)\
**Post date:** [June 3, 2019, 7:47pm UTC](https://forum.crystal-lang.org/t/using-postgres-transactions/829/1 "2019-06-03T19:47:54Z")

</div>

Is there an example of using postgres transactions with crystal/db and crystal/pg? Does it simply work if all the statements starting from `BEGIN` to `COMMIT` are put in the `db.exec` string argument? If yes, what is the way to know if the transactions were successful or not?

Thank you.

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [June 3, 2019, 8:10pm UTC](https://forum.crystal-lang.org/t/using-postgres-transactions/829/2 "2019-06-03T20:10:56Z")

</div>

@nsuchy requested the same thing this week . It’s time to make an entry in [https://github.com/crystal-lang/crystal-book/tree/master/database](https://github.com/crystal-lang/crystal-book/tree/master/database) explaining transactions 😅

Use [`Database#transaction`](http://crystal-lang.github.io/crystal-db/api/0.5.1/DB/Database.html#transaction%28%26block%29-instance-method) to start a top level transaction. From the result you will have access to a specific connection to perform the queries.

Nested transactions are also supported / implemented.

---

<div class="post-metadata">

**Author:** ![nsuchy](https://avatars.discourse-cdn.com/v4/letter/n/c0e974/32.png) [@nsuchy](https://forum.crystal-lang.org/u/nsuchy)\
**Post date:** [June 3, 2019, 8:29pm UTC](https://forum.crystal-lang.org/t/using-postgres-transactions/829/3 "2019-06-03T20:29:10Z")

</div>

Hi @curious2learn I actually wrote about this in my blog post ([https://nsuchy.me/2019/06/01/how-i-parsed-huge-json-files-into-an-sqlite-database-in-under-a-second-using-the-power-of-crystal-language/](https://nsuchy.me/2019/06/01/how-i-parsed-huge-json-files-into-an-sqlite-database-in-under-a-second-using-the-power-of-crystal-language/)) where you can see a code example.

You’re able to write it out as (see the linked blog post for context to better understand this code):

```crystal
require "json"
require "sqlite3"
require "./cve_data_entity.cr"
 
module MyProgram
  VERSION = "0.1.0"
 
  filepath = "./src/example-json-files/example-full-2019-dataset.json"
  myobject = CVE_Data_Entity.from_json(File.read(filepath))
 
  DB.open "sqlite3://./src/example-json-files/dbname.sqlt" do |db|
    db.transaction do |tx|
      tx.begin_transaction
      myobject.try(&.cve_items).try(&.each do |item|
        # Insert General Information into the Database
        cve_item_id = item.try(&.cve).try(&.cve_data_meta).try(&.id) || "Not available"
        data_type = item.try(&.cve).try(&.data_type) || "Not available"
        data_format = item.try(&.cve).try(&.data_format) || "Not available"
        data_version = item.try(&.cve).try(&.data_version) || "Not available"
        published_date = item.try(&.publishedDate) || "Not available"
        last_modified_date = item.try(&.lastModifiedDate) || "Not available"
        tx.connection.exec("INSERT INTO GENERAL_INFORMATION (\"ID\", \"DATA_TYPE\", \"DATA_FORMAT\", \"DATA_VERSION\", \"PUBLISHDATE\", \"LASTMODIFIEDDATE\") VALUES (?, ?, ?, ?, ?, ?)", [cve_item_id, data_type, data_format, data_version, published_date, last_modified_date])
 
      end)
      tx.commit
    end
  end
end

```

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [June 3, 2019, 8:31pm UTC](https://forum.crystal-lang.org/t/using-postgres-transactions/829/4 "2019-06-03T20:31:29Z")

</div>

> [@nsuchy](#):
>
> db.transaction do |tx|  
> end

Doesn’t this inherently handle the begin/commit? Versus having to manaully call `tx. begin_transaction`, and `tx.commit`?

---

<div class="post-metadata">

**Author:** ![nsuchy](https://avatars.discourse-cdn.com/v4/letter/n/c0e974/32.png) [@nsuchy](https://forum.crystal-lang.org/u/nsuchy)\
**Post date:** [June 3, 2019, 8:34pm UTC](https://forum.crystal-lang.org/t/using-postgres-transactions/829/5 "2019-06-03T20:34:52Z")

</div>

> [@Blacksmoke16](#):
>
> Doesn’t this inherently handle the begin/commit? Versus having to manaully call `tx. begin_transaction` , and `tx.commit` ?

I’m not sure, that would be nice to have. Manually calling it seemed to be the most consistent and easy to use behavior for me.

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [June 3, 2019, 8:45pm UTC](https://forum.crystal-lang.org/t/using-postgres-transactions/829/6 "2019-06-03T20:45:45Z")

</div>

yes it does! The semantic is implicit commit unless there is an exception raised.

If the exception is `DB::Rollback` it is swallowed, otherwise it is re-raised.

---

<div class="post-metadata">

**Author:** ![curious2learn](https://avatars.discourse-cdn.com/v4/letter/c/5daacb/32.png) [@curious2learn](https://forum.crystal-lang.org/u/curious2learn)\
**Post date:** [June 4, 2019, 12:51am UTC](https://forum.crystal-lang.org/t/using-postgres-transactions/829/7 "2019-06-04T00:51:44Z")

</div>

Thank you very much @nsuchy, @Blacksmoke16 and @bcardiff. Appreciate your responses.
