-
Notifications
You must be signed in to change notification settings - Fork 38.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
Document LazyConnectionDataSourceProxy setup for routing datasource to act on transaction definition read-only flag #21415
Comments
@jhoeller as the old assignee, can this be triaged? |
I faced the same issue recently. I had a quick workaround until this issue not getting fixed.
As we can see, I am extending DataSourceTransactionManager and updating TransactionSynchronizationManager with the new TransactionDefinition before acquiring a connection for the JDBC transaction. This way we will always get the updated value in |
This is a close sibling to #19688, just with the initial case being the read-only flag. We mean to deal with these in combination for a JDBC theme in the 6.1.2 release. As outlined on #19688, I am inclined to address the isolation level scenario here through an extension of |
Experimenting with a few scenarios here, such an extension to As a consequence, I am going to turn this ticket into a documentation ticket for |
Doesn't seem to be working as expected in Spring 6.1.4 Using an AbstractRoutingDatasource wrapped with LazyConnectionDataSourceProxy (and setting the read-only datasource) during the initial DataSourceTransactionManager.doGetTransaction the DataSourceTransactionObject object is not marked as read-only, only the TransactionDefinition is. When this delegates to the AbstractRoutingDatasource it returns the read-write DataSource as the current TransactionSynchronizationManager is still not marked as read-only. It will still make a connection to the read-write DataSource and THEN set the read-only flag in the TransactionSynchronizationManager. To make things more confusing, it looks like after this the LazyConnectionDataSourceProxy is doing its routing and returning the correct DataSource. I was under the assumption that I wouldn't see any connection made to the read-write as the transaction should have been marked in the TransactionSynchronizationManager as read-only on transaction start, but that looks to not be the case. Only when using the work around mentioned by kanakharaharsh above does the transaction NOT start a connection to the read-write and only operates on the read-only. |
The Also, as of 6.1.2, |
Ok, after removing my
|
That's because those lines are logged before a connection is actually made, so the I can confirm, via use of the debugger, that |
Sample that shows the differentiation between read-only and read-write transactions using datasource proxy. |
Kaan Ozdokmeci opened SPR-16876 and commented
In org.springframework.transaction.support.AbstractPlatformTransactionManager#getTransactionmethod,
the synchronization point is created after the transaction is created and a connection is attached to it via the doBegin method.
Having the synchronization point created afterwards prevents the ability to utilize an AbstractRoutingDatasource that is able to pick between read-only and read-write datasources for the transaction by inspecting
org.springframework.transaction.support.TransactionSynchronizationManager#isCurrentTransactionReadOnly
when determining the appropriate datasource.
Is there any reason not to move synchronization point creation before the doBegin method so that routing datasources can act on the transaction definition?
If not happy to send a PR.
No further details from SPR-16876
The text was updated successfully, but these errors were encountered: