# Connection


A managed database connection drawn from the pool.


Usage


``` python
Connection(
    backend,
    connection_id,
)
```


Connections track their own state (idle, active, closed) and expose that state through read-only properties. Use [get_connection()](get_connection.md#gdtest_complete_docstrings.get_connection) to obtain one; call [close()](Connection.md#gdtest_complete_docstrings.Connection.close) when finished.


## Parameters


`backend: BackendName`  
Which database backend this connection targets.

`connection_id: ConnectionId`  
The pool-assigned unique identifier.


## See Also

[get_connection()](get_connection.md#gdtest_complete_docstrings.get_connection)  

[reinitialize()](reinitialize.md#gdtest_complete_docstrings.reinitialize)  


## Attributes

| Name | Description |
|----|----|
| [backend](#backend) | The database backend this connection targets. |
| [connection_id](#connection_id) | The pool-assigned unique identifier for this connection. |
| [is_closed](#is_closed) | Whether the connection has been permanently closed. |
| [is_idle](#is_idle) | Whether the connection has no in-flight queries. |

------------------------------------------------------------------------


### backend


The database backend this connection targets.


`backend: BackendName`


This is the backend name passed at construction time and does not change over the connection's lifetime. Useful for dispatching backend-specific SQL dialect adjustments.


#### See Also

[SUPPORTED_BACKENDS](SUPPORTED_BACKENDS.md#gdtest_complete_docstrings.SUPPORTED_BACKENDS)  

[BackendName](BackendName.md#gdtest_complete_docstrings.BackendName)  

------------------------------------------------------------------------


### connection_id


The pool-assigned unique identifier for this connection.


`connection_id: ConnectionId`


Appears in log output and health-check reports. Two connections never share an ID within the same process, even after one has been closed and garbage-collected.


#### See Also

[ConnectionId](ConnectionId.md#gdtest_complete_docstrings.ConnectionId)  

------------------------------------------------------------------------


### is_closed


Whether the connection has been permanently closed.


`is_closed: bool`


A closed connection cannot be reused. Attempting to execute a query on a closed connection raises `ConnectionClosedError`.


#### See Also

[is_idle](Connection.md#gdtest_complete_docstrings.Connection.is_idle)  

[close()](Connection.md#gdtest_complete_docstrings.Connection.close)  

------------------------------------------------------------------------


### is_idle


Whether the connection has no in-flight queries.


`is_idle: bool`


Computed from the internal transaction counter, so this is always consistent even if queries are being submitted from another thread. An idle connection can be safely returned to the pool or closed.


#### See Also

[is_closed](Connection.md#gdtest_complete_docstrings.Connection.is_closed)  

[close()](Connection.md#gdtest_complete_docstrings.Connection.close)  


## Methods

| Name | Description |
|----|----|
| [close()](#close) | Close the connection and return it to the pool. |

------------------------------------------------------------------------


### close()


Close the connection and return it to the pool.


Usage


``` python
close()
```


After calling [close()](Connection.md#gdtest_complete_docstrings.Connection.close), [is_closed](Connection.md#gdtest_complete_docstrings.Connection.is_closed) returns `True` and any further operations on this connection raise `ConnectionClosedError`.


#### See Also

[is_closed](Connection.md#gdtest_complete_docstrings.Connection.is_closed)  

[is_idle](Connection.md#gdtest_complete_docstrings.Connection.is_idle)
