yogas
yogas

Reputation: 209

Configure HikariCp dataSource with HikariConfig in mybatis in xml mapper configuration

I am trying to configure HikariCp in MyBatis using XML configuration

I want to know how to set the hikariCongig object in object in the mapper configuration.

my config looks like this :

<environment id="development">
    <transactionManager type="JDBC" />
    <dataSource type="com.xyz.config.HikariCPDataSourceFactory" >
        <property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/beta-prod-db" />
        <property name="username" value="postgres" />
        <property name="password" value="${password}" />
        <property name="poolName" value="test"/>
        <property name="maxPoolSize" value="20" />
        <property name="registerMbeans" value="true"/>
        <property name="minimumIdle" value="5"/>
    </dataSource>
</environment>

HikariCPDataSourceFactory.java

public class HikariCPDataSourceFactory extends PooledDataSourceFactory {
    public HikariCPDataSourceFactory() {
        // HikariConfig hikariConfig = new HikariConfig();
        this.dataSource = new HikariDataSource();
    }
}

I don't find any online article that shows how to set the hikariConfig object in the hikariDataSource object through XML configuration.

using Spring I can create a bean for hikariConfig and pass it as a parameter in the hikariDataSource object, but here I am not using spring so need to find a way with XML.

Without the hikariConfig object, if I try to get the HikariPoolMXBean object from dataSource I get the exception org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.

HikariCP 1.4.0 MBean InstanceNotFoundException

this article says it only works when I set the hikariConfig Object

Upvotes: 0

Views: 1959

Answers (2)

Miss Chanandler Bong
Miss Chanandler Bong

Reputation: 4258

You need to implement DataSourceFactory and pass the properties you specified in your MyBatis XML Configuration file to the new data source:

public class HikariCPDataSourceFactory implements DataSourceFactory {
    private HikariDataSource dataSource;

    @Override
    public void setProperties(Properties props) {
        HikariConfig config = new HikariConfig(props);
        this.dataSource = new HikariDataSource(config);
    }

    @Override
    public DataSource getDataSource() {
        return dataSource;
    }
}

Upvotes: 2

yogas
yogas

Reputation: 209

i couldnt find a way to configure the hikariConfig in xml Here is the workaround i used that works well for me.

  HikariDataSource hikariDataSource = null;
  HikariConfig hikariConfig = new HikariConfig();
  dataSource.copyStateTo(hikariConfig);
  hikariDataSource = new HikariDataSource(hikariConfig);

once i get the dataSource object i copy the state to a hikariConfig object and create new dataSource object using it. Also we can make it singleton so only one instance is created.

Upvotes: 0

Related Questions