Skip to content

Update SQLAlchemy example to include non-admin and connection refresh logic#119

Merged
mitchell-elholm merged 2 commits intomainfrom
SQLAlchemy_refresh_nonadmin
May 6, 2025
Merged

Update SQLAlchemy example to include non-admin and connection refresh logic#119
mitchell-elholm merged 2 commits intomainfrom
SQLAlchemy_refresh_nonadmin

Conversation

@mitchell-elholm
Copy link
Copy Markdown
Contributor

Description of changes:

Adds non-admin logic and connection token refresh

  • Adds a listener for non-admin, and updates the search path to myschema.
  • Adds a listener to set a new token every time a connection is created. The connection is created immediately after the parameter is updated.
  • Connection refresh can be tested by setting the token expiry to 2 seconds, and then setting a parameter pool_recycle to 1 second in the engine creation. This ensures that it will attempt to open a new connection every second, and that the first token created will not be valid anymore after 1 second. It should succeed, as it created a new token each time it connected. I also tested the negative case, by removing the listener altogether and using a single token with a short timeout, to prove that it would have failed without the token refresh.
engine = create_engine(
        url, 
        connect_args={"sslmode": "verify-full", "sslrootcert": "system"},
        pool_recycle=1 
    )
...

return client.generate_db_connect_admin_auth_token(cluster_endpoint, region, 1)
  • Other minor changes: Adds table drop to allow repeated execution.
  • Removed SDK, added import

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

By submitting this pull request, I confirm that my contribution is made under
the terms of the MIT-0 license.

Thank you for your contribution!

Copy link
Copy Markdown
Collaborator

@trstephen-amazon trstephen-amazon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢


> **Note**
>
> Note that SQLAlchemy with Psycopg3 does not work with Aurora DSQL. SQLAlchemy with Psycopg3 uses nested transactions which rely on savepoints as part of the connection setup. Savepoints are not supported by Aurora DSQL.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL

@mitchell-elholm mitchell-elholm merged commit d7b043f into main May 6, 2025
3 checks passed
@mitchell-elholm mitchell-elholm deleted the SQLAlchemy_refresh_nonadmin branch May 7, 2025 16:35
danielfrankcom pushed a commit to marcbowes/aurora-dsql-samples that referenced this pull request May 9, 2025
* Add vscode improving dsql documentation

* Update ui-tools/vscode/README.md
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