Pale Blue Dot
Pale Blue Dot

Reputation: 591

DatasSourceBuilder vs DriverManagerDataSource

Creating DataSource using DatasSourceBuilder

@Bean
    DataSource dataSource() throws SQLException {
        DatasSourceBuilder dataSource = DatasSourceBuilder.create();
        dataSource.url("");
        ...
        ..
        
        return dataSource.build();
        
    }

Creating DataSource using DriverManagerDataSource

@Bean
    DriverManagerDataSource dataSource() throws SQLException {
    
        DriverManagerDataSource dataSource = DriverManagerDataSource();
        dataSource.setUrl("");
        ...
        ..
        
        return dataSource;
        
    }

I am creating jdbc with both of the above method

@Bean
public JdbcTemplate jdbcTemplate()
{
return new jdbcTemplate(dataSource());
}

I am uisng jdbcTemplate like below

void m1()
{
simpleJdbc = new simpleJdbc(jdbcTemplate);
simpleJdbc.execute(procedure)
}

So my question is if i call m1() 50 times repeatedly then how many connections will be created in both cases i.e DriverManagerDataSource and DatasSourceBuilder

Upvotes: 2

Views: 2500

Answers (1)

M. Deinum
M. Deinum

Reputation: 125242

The easy answer is

  • DriverManagerDataSource -> as many connections as you call the method
  • DataSourceBuilder -> as many connections as specified by the max poolsize property.

However the real answer would be it depends. If you call m1 in a single transaction and call it 50 times then it will open in either way just a single connection. The opened connection is bound to the transaction and reused.

That being said, you shouldn't be using DriverManagerDataSource for a production ready application. It isn't a connection pool and will open connections when needed, opening a connection is slow and it will create an unbounded number of connections (depending on the need) so if you have 100 requests needing a connection it will open 100 connections (probably flooding your DB).

Use DriverManagerDataSource only for tests and demos but not for production applications.

Upvotes: 4

Related Questions