Skip to content
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

Need to know transaction.isReadonly while getting connection - spring-jdbc #25323

Closed
johnny2002 opened this issue Jun 27, 2020 · 8 comments
Closed
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) status: duplicate A duplicate of another issue type: enhancement A general enhancement

Comments

@johnny2002
Copy link

johnny2002 commented Jun 27, 2020

Backgroup: I have a large project with more than 500 nodes need to connection to one Database, so connection count is too high. In order to reduce db connections, I want to reuse one connection for all readonly transactions. So, I need to get the "TransactionSynchronizationManager.isCurrentTransactionReadOnly()" in Datasource.getConnection() method. While, in spring-jdbc-5.2.2, DataSourceTransactionManager.doBegin method, the connection is gotten at line 263, the readonly flag is setted at line 298.

Suggestion: Set the readonly flag before getting connection. e.g. in DataSourceTransactionManager.doBegin method

protected void doBegin(Object transaction, TransactionDefinition definition) {
TransactionSynchronizationManager.setCurrentTransactionReadOnly(definition.isReadOnly());
DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction;
...
Connection newCon = obtainDataSource().getConnection();
...

Then I will override a Datasource, that, return one shared connection to all readonly transactions.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 27, 2020
@quaff
Copy link
Contributor

quaff commented Jun 29, 2020

Is it safe to reuse connection across threads?

@johnny2002
Copy link
Author

Is it safe to reuse connection across threads?

for readonly operations, it is.

@quaff
Copy link
Contributor

quaff commented Jun 30, 2020

Is it safe to reuse connection across threads?

for readonly operations, it is.

It depends on driver implementation.

@johnny2002
Copy link
Author

Is it safe to reuse connection across threads?

for readonly operations, it is.

It depends on driver implementation.

At least, as I tested, oracle, mysql, pgsql are no problem.

@johnny2002
Copy link
Author

johnny2002 commented Jun 30, 2020

further more, no matter share connection is safe or not, let developers can get the readonly property as earlier as possible is a good manner. Developers may use the readonly property for other purpos.

@johnny2002
Copy link
Author

Is it safe to reuse connection across threads?

for readonly operations, it is.

It depends on driver implementation.

So, what's the decision? will you adopt the change request?

@rstoyanchev rstoyanchev added in: data Issues in data modules (jdbc, orm, oxm, tx) status: superseded An issue that has been superseded by another and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 10, 2021
@snicoll
Copy link
Member

snicoll commented Sep 13, 2023

The PR has been declined, reopening to reconsider this.

@snicoll snicoll reopened this Sep 13, 2023
@snicoll snicoll added type: enhancement A general enhancement and removed status: superseded An issue that has been superseded by another labels Sep 13, 2023
@snicoll snicoll added this to the 6.x Backlog milestone Sep 13, 2023
@jhoeller jhoeller added the status: duplicate A duplicate of another issue label Dec 23, 2023
@jhoeller jhoeller removed this from the 6.x Backlog milestone Dec 23, 2023
@jhoeller
Copy link
Contributor

jhoeller commented Dec 23, 2023

This effectively got addressed through #31785 in 6.1.2 where LazyConnectionDataSourceProxy has dedicated support for a read-only DataSource now. And generally, as per #21415, LazyConnectionDataSourceProxy is recommended for lazy-determined DataSource routing based on those thread-local settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) status: duplicate A duplicate of another issue type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

6 participants