sqlx
sqlx copied to clipboard
MS SQL: ConnectionAborted when running (very long) query
When trying to run a long query against an MS SQL server, I'm getting Io(Kind(ConnectionAborted)). After some trial and error, I have narrowed down the issue to the query string being 1992 (works) or 1993 (breaks) characters in length.
However, I can run my long queries without problems using other tools like the database explorer in IntelliJ IDEA (uses JDBC) so I must assume that the problem is not with the server itself but somewhere on the client side. I'll try to dig and debug deeper into the library and see whether I can find anything but haven't had much luck so far. Considering that a few bytes will be added protocol-wise and there's also one bound variable that needs to be transferred, I was thinking that maybe there's a buffer limited to 2kb somewhere along the line.
Found the following TODO comment in stream.rs of the mssql module:
pub(crate) fn write_packet<'en, T: Encode<'en>>(&mut self, ty: PacketType, payload: T) {
// TODO: Support packet chunking for large packet sizes
// We likely need to double-buffer the writes so we know to chunk
As the default packet size is 4096, this is in all likelihood the reason for the problem, as the encoded length of a ~2000 character-long query (assuming it's encoded as UTF-16) could result in that threshold being exceeded.
I found this in the specs:
https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-tds/e5ea8520-1ea3-4a75-a2a9-c17e63e9ee19
We'll try to allocate some time in one of our next sprints to add support for packet chunking, in the meantime we have resorted to chunking our queries instead :grimacing:
If anyone has some pointers, requests and recommendations, please leave them here :slightly_smiling_face:
I implemented chunking, fixing this issue, in https://github.com/lovasoa/sqlx
I added a test for it here:
https://github.com/lovasoa/sqlx/blob/e6b404a9ae3f7cd97155ccaeed04b61b385bed09/tests/mssql/mssql.rs#L186-L195