Skip to content

feat: add use_database() / current_database() API and execute tests in concurrent#154

Merged
Mini256 merged 20 commits intomainfrom
wenxuan/feat/use_db
Jul 23, 2025
Merged

feat: add use_database() / current_database() API and execute tests in concurrent#154
Mini256 merged 20 commits intomainfrom
wenxuan/feat/use_db

Conversation

@breezewish
Copy link
Copy Markdown
Member

New:

  • TiDBClient.current_database
  • TiDBClient.use_database

The verbose tests and doc comments are written by AI.

Note: the current implementation is basic, not good, see TODO in the code for the desired state.

Signed-off-by: Wish <breezewish@outlook.com>
@breezewish breezewish changed the title feat: Support TiDBClient.use_database feat: TiDBClient.use_database Jul 16, 2025
cursor[bot]

This comment was marked as outdated.

@Mini256 Mini256 changed the title feat: TiDBClient.use_database feat: add TiDBClient.use_database Jul 17, 2025
@Mini256 Mini256 changed the title feat: add TiDBClient.use_database feat: add use_database() / current_database() API and execute tests in concurrent Jul 17, 2025
cursor[bot]

This comment was marked as outdated.

@Mini256 Mini256 merged commit 0561957 into main Jul 23, 2025
3 of 4 checks passed
@Mini256 Mini256 deleted the wenxuan/feat/use_db branch July 23, 2025 04:04
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Bug: Temporary Client Ownership Causes Resource Conflicts

In TiDBClient.use_database(), a temporary TiDBClient instance (new_client) is created solely to extract its _db_engine and other attributes. This leads to unnecessary object instantiation and a resource ownership issue: new_client retains references to the engine now owned by self. If new_client were to be disposed, it could prematurely dispose of the engine currently in use by the main client, leading to resource conflicts.

pytidb/client.py#L165-L179

pytidb/pytidb/client.py

Lines 165 to 179 in 09fd405

new_url = self._db_engine.url.set(database=database)
# Attempt to create the new client first, only dispose and update attributes if successful
new_client = TiDBClient.connect(
url=new_url.render_as_string(hide_password=False),
**self._reconnect_params,
)
# Now that new_client is successfully created, dispose the old engine and update all attributes
self._db_engine.dispose()
self._db_engine = new_client._db_engine
self._inspector = new_client._inspector
self._identifier_preparer = new_client._db_engine.dialect.identifier_preparer
self._reconnect_params = new_client._reconnect_params

Fix in CursorFix in Web


Was this report helpful? Give feedback by reacting with 👍 or 👎

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