Search code examples
springspring-mvcdatasourceconnection-pooling

BasicDataSource vs DriverManagerDataSource. Which is better for production ready spring-hibernate application?


There are many DataSource implementation classes are available like org.apache.commons.dbcp2.BasicDataSource, org.springframework.jdbc.datasource.DriverManagerDataSource. How can I decide(chose) which one to use in my Spring-mvc + Hibernate application?


Solution

  • If you go through Docs

    DriverManagerDataSource: This class is not an actual connection pool; it does not actually pool Connections. It just serves as simple replacement for a full-blown connection pool, implementing the same standard interface, but creating new Connections on every call.

    Useful for test or standalone environments

    On other hand DBCP's BasicDataSource provide "real" connection pool outside of a J2EE container. Commons DBCP's BasicDataSource and C3P0's ComboPooledDataSource are full connection pool beans, supporting the same basic properties as this class plus specific settings (such as min/max pool size etc).

    So if you are developing a test application then DriverManagerDataSource is okay but for production ready applications must go with "real" connection pool i.e C3p0's ComboPooledDataSource or DBCP's BasicDataSource.

    Ref: https://docs.spring.io/spring/docs/3.0.0.RC3/reference/html/ch12s03.html