Skip to content

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

Closed
@johnny2002

Description

@johnny2002

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: dataIssues in data modules (jdbc, orm, oxm, tx)status: duplicateA duplicate of another issuetype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions