# Caqti transaction examples

**URL:** <https://discuss.ocaml.org/t/caqti-transaction-examples/8220>\
**Category:** Learning\
**Tags:** db, caqti\
**Created:** [July 31, 2021, 8:56am UTC](https://discuss.ocaml.org/t/caqti-transaction-examples/8220 "2021-07-31T08:56:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mudrz](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@mudrz](https://discuss.ocaml.org/u/mudrz)\
**Post date:** [July 31, 2021, 8:56am UTC](https://discuss.ocaml.org/t/caqti-transaction-examples/8220/1 "2021-07-31T08:56:13Z")

</div>

EDIT: pauerkedal updated the oneshot documentation and it is now much clearer

is anyone familiar with caqti and transactions?

I’m getting errors like `Invalid concurrent usage of PostgreSQL connection detected` and:

- can’t figure out what’s wrong
- don’t find such errors in the source code

more details:

> <https://github.com/paurkedal/ocaml-caqti/issues/73>
>
> I wanted to apply a few .sql files
> 
> apply:
> \- read file
> \- execute file\_conten…ts as SQL query
> \- read next file ..
> 
> if anything fails - rollback -\> wanted everything to run as a transaction:
> \- I start a connection with \`Connection.start ()\`
> \- execute apply for a list of files
> - if nothing failed -\> \`Connection.commit ()\`
> - if something failed -\> \`Connection.rollback ()\`
> 
> To execute apply I used:
> \`\`\`ocaml
> let query = Caqti\_request.exec Caqti\_type.unit file\_contents in
> Connection.exec query ()
> \`\`\`
> 
> Error1: \`Request to ... failed: ERROR: cannot insert multiple commands into a prepared statement\`
> 
> Not sure how to insert multiple commands, found that \`oneshot: true\` moves past this error, (but I couldn't find any information on what is \`oneshot\`, are there any docs?)
> 
> Error2: \`Unexpected result from ... More than one response received.\`
> 
> Does this mean that because there are multiple results \`Caqti\_type.unit\` does not work?
> Tried this as a workaround:
> \`\`\`ocaml
> let typ =
> Caqti\_type.custom
> ~encode:(fun \_ -\> failwith "no parameters expected")
> ~decode:(fun \_ -\> Ok ())
> Caqti\_type.unit
> in
> let query = Caqti\_request.exec ~oneshot:true typ contents in
> Connection.exec query ()
> \`\`\`
> 
> But found Error3: \`Invalid concurrent usage of PostgreSQL connection detected.\`
> 
> I'm not sure what is invalid and what this means?
> Are there any resources with documentation and examples that can help with these issues?

this is the gist:

- I start a connection with `Connection.start ()`
- then I execute different queries

````ocaml
let query = Caqti_request.exec Caqti_type.unit file_contents in
Connection.exec query ()
```

  * if nothing failed -> `Connection.commit ()`
  * if something failed -> `Connection.rollback ()`
````
