如何更换MyBatis连接池?含默认实现、自定义及替换为HikariCP/BoneCP方法
Hey there! Let's tackle your questions about MyBatis connection pools one by one—super common stuff, so I'll keep it clear and actionable:
MyBatis ships with two built-in connection pool options right out of the box:
UnpooledDataSource: This is the default if you don't explicitly specify a connection pool type. It creates a brand-new database connection every time one is needed and closes it immediately after use—no pooling logic at all, which is fine for simple apps but not ideal for high-traffic scenarios.PooledDataSource: A basic but functional connection pool that reuses existing connections to avoid the overhead of creating new ones repeatedly. You can switch to this by setting<dataSource type="POOLED">in your MyBatis configuration file.
HikariCP is widely regarded as the highest-performance connection pool for Java, so it's a popular upgrade. Here's how to set it up:
Add the HikariCP Dependency
If you're using Maven, add this to yourpom.xml:<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>5.0.1</version> <!-- Use the latest stable version available --> </dependency>Configure MyBatis to Use HikariCP
Update yourmybatis-config.xmlto point directly to Hikari'sHikariDataSourceclass, then add your database credentials and Hikari-specific settings:<dataSource type="com.zaxxer.hikari.HikariDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/your_database"/> <property name="username" value="your_username"/> <property name="password" value="your_password"/> <!-- Hikari-specific optimizations --> <property name="maximumPoolSize" value="10"/> <property name="connectionTimeout" value="30000"/> <!-- 30 seconds --> <property name="idleTimeout" value="600000"/> <!-- 10 minutes --> </dataSource>
BoneCP was a go-to pool before HikariCP took off, and it's still usable if you have a preference for it. Here's how to integrate it:
Add the BoneCP Dependency
Add this to your Mavenpom.xml:<dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp</artifactId> <version>0.8.0.RELEASE</version> </dependency>Configure MyBatis for BoneCP
Update yourmybatis-config.xmlto use BoneCP'sBoneCPDataSourceand configure its pool settings:<dataSource type="com.jolbox.bonecp.BoneCPDataSource"> <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/your_database"/> <property name="username" value="your_username"/> <property name="password" value="your_password"/> <!-- BoneCP-specific settings --> <property name="maxConnectionsPerPartition" value="5"/> <property name="minConnectionsPerPartition" value="2"/> <property name="partitionCount" value="2"/> <property name="acquireIncrement" value="1"/> </dataSource>
If you need a fully custom pool implementation, MyBatis makes this straightforward by using its DataSourceFactory interface:
Implement the
DataSourceFactoryInterface
Create a factory class that initializes your custom data source. You can either implement the interface directly or extendUnpooledDataSourceFactoryif you want to build on MyBatis's base logic:import org.apache.ibatis.datasource.DataSourceFactory; import javax.sql.DataSource; import java.util.Properties; public class CustomPoolDataSourceFactory implements DataSourceFactory { private Properties poolProperties; private CustomDataSource customDataSource; @Override public void setProperties(Properties properties) { this.poolProperties = properties; } @Override public DataSource getDataSource() { if (customDataSource == null) { customDataSource = new CustomDataSource(); // Initialize your custom pool with properties from the config customDataSource.setJdbcUrl(poolProperties.getProperty("jdbcUrl")); customDataSource.setUsername(poolProperties.getProperty("username")); customDataSource.setPassword(poolProperties.getProperty("password")); // Add any custom pool configuration here (e.g., max connections, timeout) } return customDataSource; } }Make sure your
CustomDataSourceclass implements the standardjavax.sql.DataSourceinterface and includes all your custom connection pooling logic.Register Your Custom Factory in MyBatis
Update yourmybatis-config.xmlto use your factory class, and pass in any required properties:<dataSource type="com.yourcompany.yourpackage.CustomPoolDataSourceFactory"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/your_database"/> <property name="username" value="your_username"/> <property name="password" value="your_password"/> <!-- Add your custom pool's specific configuration properties here --> <property name="maxPoolSize" value="15"/> <property name="connectionTimeoutMs" value="20000"/> </dataSource>
Hope that clears everything up! If you hit any snags while setting this up, feel free to follow up with specific details about your setup.
内容的提问来源于stack exchange,提问作者Gallegote

