You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何更换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 Default Connection Pool Implementations

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.
Replacing the Default Pool with HikariCP

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:

  1. Add the HikariCP Dependency
    If you're using Maven, add this to your pom.xml:

    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>5.0.1</version> <!-- Use the latest stable version available -->
    </dependency>
    
  2. Configure MyBatis to Use HikariCP
    Update your mybatis-config.xml to point directly to Hikari's HikariDataSource class, 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>
    
Replacing the Default Pool with BoneCP

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:

  1. Add the BoneCP Dependency
    Add this to your Maven pom.xml:

    <dependency>
        <groupId>com.jolbox</groupId>
        <artifactId>bonecp</artifactId>
        <version>0.8.0.RELEASE</version>
    </dependency>
    
  2. Configure MyBatis for BoneCP
    Update your mybatis-config.xml to use BoneCP's BoneCPDataSource and 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>
    
Using a Custom Connection Pool

If you need a fully custom pool implementation, MyBatis makes this straightforward by using its DataSourceFactory interface:

  1. Implement the DataSourceFactory Interface
    Create a factory class that initializes your custom data source. You can either implement the interface directly or extend UnpooledDataSourceFactory if 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 CustomDataSource class implements the standard javax.sql.DataSource interface and includes all your custom connection pooling logic.

  2. Register Your Custom Factory in MyBatis
    Update your mybatis-config.xml to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:47:51