Skip to content

Support LazyConnectionDataSourceProxy #15480

@ttddyy

Description

@ttddyy

Hi,

I would like to suggest a support for LazyConnectionDataSourceProxy in spring-boot.

Here is my opinion:

Transaction management in spring:
When a transactional method(@Transactional) is called, the configured impl of PlatformTransactionManager kicks in and starts/prepare transaction logic(call AbstractPlatformTransactionManager#doBegin).

In the implementation of starting transaction(e.g: JpaTransactionManager, HibernateTransactionManager), when @Transactional definition has non default isolation level or readonly flag is set to true, it acquires a physical connection and set isolation level and/or readonly flag to the connection in its preparation phase. (call to DataSourceUtils#prepareConnectionForTransaction)
Since this happens before invoking the actual target method, this consumes a connection from pool(underlying DataSource) regardless of the method really needs a connection or not.

It is problematic especially on high traffic application with:

  • Many readonly transactional methods (or any non-default transactional methods)
    • This holds connection longer than it needs to do
  • High hits for hibernate 2nd level cache
    • The method may not even need a connection

It is better to delay getting the physical connection as much as possible.

LazyConnectionDataSourceProxy in spring solves this problem. When isolation-level, read-only flag, etc methods are called, this proxy internally keeps those values. Physical connection acquisition is delayed until connection is really required.

Currently, spring-boot doesn't have any support for LazyConnectionDataSourceProxy.
My suggestion is to add a configuration of LazyConnectionDataSourceProxy in spring-boot.
Probably, application property with spring.datasource.lazy-connection=true would wrap the datasource with LazyConnectionDataSourceProxy. I also think it is beneficial to turn this on by default.
LazyConnectionDataSourceProxy should be recommended more, in general, IMO.

Thanks,

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions