Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a reference to the connection from prepared statements #107

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

casperisfine
Copy link
Contributor

Ref: #105

To close a prepared statement you need to have access to the connection that created it.

But in managed languages like Ruby, the obvious thing to do is to close the prepared statement when the associated object is garbage collected.

But the order in which objects are garbaged collected is never guaranteed so when freeing a statement the connection might have been freed already and it's hard to detect.

We checked how libmysqlclient handles it, and each MYSQL_STMT has a reference to its MYSQL (connection), and the connection keeps a doubly linked list of the statements it created.

When a statement is closed it's removed from the list, when the connection is closed, all the connection references are set to NULL.

We implemented exactly the same logic here.

Additionally, prepared statement can only be used with the connection they were created from. As such having all the trilogy_stmt_* function take a connection isn't great for usability. So this change opens the door to only taking a trilogy_stmt_t *.

Ref: trilogy-libraries#105

To close a prepared statement you need to have access to the connection
that created it.

But in managed languages like Ruby, the obvious thing to do is to
close the prepared statement when the associated object is garbage
collected.

But the order in which objects are garbaged collected is never guaranteed
so when freeing a statement the connection might have been freed
already and it's hard to detect.

We checked how libmysqlclient handles it, and each `MYSQL_STMT` has
a reference to its `MYSQL` (connection), and the connection keeps a
doubly linked list of the statements it created.

When a statement is closed it's removed from the list, when the connection
is closed, all the connection references are set to NULL.

We implemented exactly the same logic here.

Additionally, prepared statement can only be used with the connection
they were created from. As such having all the `trilogy_stmt_*` function
take a connection isn't great for usability. So this change opens the
door to only taking a `trilogy_stmt_t *`.

Co-Authored-By: Adrianna Chang <adrianna.chang@shopify.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants