Repository navigation
Transactions do not reuse the connector on dispose - #3266
Conversation
|
I think I have another solution which doesn't cause allocations for every new transaction, without having to go through #3260... The problem is the that binding scope is ended as soon as we see the ReadyForQuery protocol message with an idle transaction status. We can delay that and do it inside NpgsqlTransaction.Commit (and Rollback) instead - this would give us the opportunity to terminate the user action before we return the connection to the pool (just like you did for other operations in #3254). (if we do this, then it should become unsupported to do COMMIT or ROLLBACK in raw SQL when multiplexing is on, just like it's unsupported to start a new transaction with BEGIN). Do you want to give this a try? |
I think I know what you're talking about. This could be done similar to how COPY operations deal with the same problem. |
…was commited" This reverts commit ee3b27f.
roji
left a comment
There was a problem hiding this comment.
As your fixes to the tests show, if anyone isn't disposing their transactions, this change will start being potentially problematic. I know that DbTransaction is disposable so people are supposed to dispose, but the reality is that making a type suddenly care about disposal is risky.
Is there any reason not to just do this earlier in commit/rollback? I think it may even simplify the code a bit.
Dispose. On dispose, it can rollback the transaction on the connector. If we return the connector to the pool on transaction's commit/rollback, we should never dispose after that. |
Dispose can rollback, but only if rollback wasn't already called before. So if we always just end the scope on commit/rollback, most of the times it will happen early (when the user calls commit/rollback), and sometimes it will happen when disposing (when the user disposes the transaction without calling commit/rollback). Where do you see an issue? |
You have only one |
|
I must be missing something... Let's assume we move ending the scope to Commit/Rollback as I suggest. If the user calls Rollback explicitly, the scope is ended at that point and the connector is returned to the pool. If the user disposes the transaction after that, IsCompleted is true and we do nothing. If the user doesn't call Rollback and then calls Dispose, IsCompleted is false and we call Rollback. At that point again, the scope is ended and the connector is returned to the pool. Where's the issue? |
Yeah, the connector is in the pool. After that, another connection is open and it takes that exact connector and opens another transaction. Which you will rollback in dispose. |
|
You're right. In that case, we may want to think a bit more about existing code which does not dispose transactions. Right now, it's totally OK to begin a transaction, commit/rollback, and then begin another transaction on the same connection (without disposing the first). This is because our "transaction already in progress" error checks the connector's transaction status, and it is idle by that point (i.e. it doesn't care if the previous transaction has been disposed or not). One thing we could do is to be stricter, and start throwing if an undisposed transaction exists (we could do this only in multiplexing, where it matters, or in general). But don't underestimate the amount of existing code out there (as well as possibly ORMs and other layers) which may hit this. Another possibility is to simply make this work. In other words, if a new transaction is started, and the previous one is undisposed, we simply "take over" the connector (it never gets returned to the pool). What do you think? |
This would work as long as dispose wouldn't rollback. |
Sure. If the user disposes the transaction and rolls back, it's perfectly fine to close the binding scope etc. My main point is about maintaining compatibility for existing code which does not dispose. |
|
@vonzshik good points made offline. Some thoughts:
What do you think, do you see any problems? |
|
Okay @roji, now it should work more or less how we've discussed. |
I went with a simple way of creating a new transaction.