-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Transaction commit raises CockroachDB error #933
Comments
We discovered that While trying to debug, I was confused by the way that stream writing and message-awaiting depend on the state of the |
++++1 |
Any existing workaround? |
@sergeyshaykhullin I've been using the following code. It lets you write The implementation calls
|
Thx @imalsogreg it worked! btw follow a more generic implementation that also includes #[async_trait]
/// A trait for adding shim methods to sqlx::QueryAs instances.
/// This is a shim to be used while we work on an upstream fix for sqlx.
/// See https://github.com/launchbadge/sqlx/issues/933 and
/// https://github.com/cockroachdb/cockroach/issues/40195 for more details.
pub trait FetchShim<DB, O>
where
DB: Database,
{
/// Like `sqlx::fetch_one`, except that internally it calls `sqlx::fetch_all`.
/// This will be less efficient than `sqlx::fetch_one`, if the query does not
/// limit its result rows to 1
async fn fetch_one_shim<'e, 'c, E>(self, query: E) -> Result<O, sqlx::Error>
where
O: Send + Unpin + for<'r> FromRow<'r, DB::Row>,
E: 'e + Executor<'c, Database = DB>;
/// Like `sqlx::fetch_one`, except that internally it calls `sqlx::fetch_all`.
/// This will be less efficient than `sqlx::fetch_one`, if the query does not
/// limit its result rows to 1
async fn fetch_optional_shim<'e, 'c, E>(self, query: E) -> Result<Option<O>, sqlx::Error>
where
O: Send + Unpin + for<'r> FromRow<'r, DB::Row>,
E: 'e + Executor<'c, Database = DB>;
}
#[async_trait]
impl<'q, DB, O, A> FetchShim<DB, O> for sqlx::query::QueryAs<'q, DB, O, A>
where
DB: sqlx::Database,
A: 'q + sqlx::IntoArguments<'q, DB>,
{
async fn fetch_one_shim<'e, 'c, E>(self, query: E) -> Result<O, sqlx::Error>
where
DB: sqlx::Database,
O: Send + Unpin + for<'r> sqlx::FromRow<'r, DB::Row>,
E: 'e + sqlx::Executor<'c, Database = DB>,
{
self.fetch_optional_shim(query).await?.ok_or(Error::RowNotFound)
}
async fn fetch_optional_shim<'e, 'c, E>(self, query: E) -> Result<Option<O>, sqlx::Error>
where
O: Send + Unpin + for<'r> sqlx::FromRow<'r, DB::Row>,
E: 'e + sqlx::Executor<'c, Database = DB>,
{
Ok(self.fetch_all(query).await?.into_iter().next())
}
}
#[async_trait]
impl<'q, DB, F, O, A> FetchShim<DB, O> for sqlx::query::Map<'q, DB, F, A>
where
DB: sqlx::Database,
F: FnMut(DB::Row) -> Result<O, sqlx::Error> + Send,
O: Send + Unpin,
A: 'q + Send + sqlx::IntoArguments<'q, DB>,
{
async fn fetch_one_shim<'e, 'c, E>(self, query: E) -> Result<O, sqlx::Error>
where
O: Send + Unpin + for<'r> sqlx::FromRow<'r, DB::Row>,
E: 'e + sqlx::Executor<'c, Database = DB>,
{
self.fetch_optional_shim(query).await?.ok_or(Error::RowNotFound)
}
async fn fetch_optional_shim<'e, 'c, E>(self, query: E) -> Result<Option<O>, sqlx::Error>
where
O: Send + Unpin + for<'r> sqlx::FromRow<'r, DB::Row>,
E: 'e + sqlx::Executor<'c, Database = DB>,
{
Ok(self.fetch_all(query).await?.into_iter().next())
}
} |
I believe this is a bug in CockroachDB, not SQLx. We never use anything but the unnamed portal, which should be replaced every time we send a Trying to find the error message in the source didn't yield any code results, but did give me this commit: cockroachdb/cockroach@0ce4443 So it appears that this will likely be fixed in the next release of CockroachDB. |
Hello! I've been using
sqlx
to talk to CockroachDB databases and recently started an upgrade fromsqlx-0.3.5
tosqlx-0.4.2
. After upgrading, many tests began to fail withI came up with a minimal reproducing test case that does a
SELECT 1
. When this query is executed within a transaction, it fails. But if I run the query directly against the postgres connection pool, it succeeds.Both cases succeed for
sqlx-0.3.5
.I was told in the CockroachDB community slack that my database driver is trying to have multiple active result sets (portals) concurrently on the same connection. I'm not sure where to go from there. Thanks for any ideas! I'm happy to work on a patch, given some clues, if you have ideas about what is happening.
The text was updated successfully, but these errors were encountered: