> For the complete documentation index, see [llms.txt](https://clear.gitbook.io/project/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://clear.gitbook.io/project/model/transactions-and-save-points/connection-pool.md).

# Connection pool

Clear uses connection pooling to allow multiple transactions to run in parallel over multiple fibers.

By default, each connection is fetched from the connection pool for each fibers. Let's see this example:

```ruby
begin
  Clear::SQL.execute("CREATE TABLE tests (id serial PRIMARY KEY)")

  spawn do
    # Clear automatically create a connection nº1 
    Clear::SQL.transaction do
      Clear::SQL.insert("tests", {id: 1}).execute
      sleep 0.2 #< Wait and do not commit the transaction for now
    end
  end

  @@count = -1

  # Spawn a new fiber
  spawn do
    sleep 0.1 #Wait a bit, to ensure than the first connection is inside a transaction
    # execute in connection nº2
    @@count = Clear::SQL.select.from("tests").count
  end

  sleep 0.3 # Let the 2 fiber time to finish...

  # The count is zero, because: it has been setup by the second fiber, which
  # called AFTER the insert but BEFORE the commit on the connection nº1
  @@count.should eq 0 

  # Now the transaction of connection nº1 is over, count should be 1
  count = Clear::SQL.select.from("tests").count
  count.should eq 1
ensure
  Clear::SQL.execute("DROP TABLE tests;")
end
```

Each call to SQL is using a new connection, from the free connection pool; if a transaction is in progress, each call will use the same connection during the whole transaction.

{% hint style="warning" %}
If all the connections are busy, the fiber will wait indefinitely until a new connection is freed.
{% endhint %}

{% hint style="danger" %}
There is currently no way to force a fiber to use the same connection has another fiber. This may be improved in the future.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://clear.gitbook.io/project/model/transactions-and-save-points/connection-pool.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
