Search code examples
javadatasourcemybatisapache-commons-dbcp

How to make a choice between PooledDataSource and dbcp when I work with the MyBatis?


I am now config a project work with spring and mybatis.

There are many example online tell me should use a org.springframework.jdbc.datasource.DriverManagerDataSource as the datasource, but mybatis use PooledDataSource as the datasource, it's confusing me. so, I read source codes of the DirverManagerDataSource, it's just a mock pool connection. so, I know how to make a choice.

But, I have a new problem soon. How to make a choice between PooledDataSource and DBCP? I read source codes of the PooledDataSource, it's also a simple implementation, but the codes of these two lib is too complex, i don't have enough time to read these codes.

can you tell me which is more powerful? and why? thanks!


Solution

  • For Spring and MyBatis, it functionally does not matter what you use. As long as there is a DataSource available to the configuration, then everything will work. With Spring, it is usually easiest to create the DataSource in the Spring config and pass that to a MyBatis SqlSessionFactoryBean.

    MyBatis' PooledDataSource should be ok from a performance perspective but it does synchronize so it may have contention issues as very high concurrency. It can be configured in Spring as a normal bean.

    DPCP has more features (esp JMX monitoring) and version 2 should have better performance than DPCP v1.

    If you are running in a container you may want to use the container's pool via JNDI (see JndiObjectFactoryBean). If you are using Tomcat, it will have it's own pool implementation.