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

Spring集成HikariCP遇循环依赖,Bean创建失败问题求助

Fixing HikariCP Circular Reference Issue in Spring (JDK 1.6)

Hey there, let's tackle this circular reference problem you're hitting when switching to HikariCP. I’ve dealt with similar headaches before, especially with older Spring versions paired with connection pools, so let’s dive in.

What’s Causing the Issue?

The error BeanCurrentlyInCreationException for your dataSource bean usually means Spring is trying to create the dataSource while it’s already in the process of being created—this happens when there’s a circular dependency between beans. In your setup, you have a separate hikariConfig bean injected into dataSource via constructor. Older Spring versions (which you’re likely using since you’re on JDK 1.6) struggle with constructor-injected circular dependencies more than setter-injected ones, and sometimes the HikariCP initialization flow can trigger unexpected early references to the dataSource bean.

Solutions to Try

1. Eliminate the Separate hikariConfig Bean (Simplest Fix)

Instead of defining a standalone hikariConfig bean, configure all HikariCP properties directly on the HikariDataSource bean itself. This removes the dependency between two top-level beans and avoids the circular reference entirely.

Here’s the adjusted XML configuration:

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
    <property name="poolName" value="abc_connection_pool" />
    <property name="dataSourceClassName" value="com.informix.jdbcx.IfxDataSource"/>
    <property name="maximumPoolSize" value="100" />
    <property name="connectionTimeout" value="500" />
    <property name="validationTimeout" value="300" />
    <property name="maxLifetime" value="30000" />
    <property name="transactionIsolation" value="1" />
    <property name="dataSourceProperties">
        <props>
            <prop key="url">${jdbc.url}</prop>
            <prop key="user">${jdbc.username}</prop>
            <prop key="password">${jdbc.password}</prop>
        </props>
    </property>
</bean>

This works because HikariDataSource exposes all the same configuration properties as HikariConfig directly, so you don’t need the separate config bean.

2. Use an Inner HikariConfig Bean

If you prefer keeping the HikariConfig structure (for clarity or reuse), wrap it as an inner bean inside the dataSource definition. This prevents Spring from treating it as a top-level bean that could trigger circular references:

<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
    <constructor-arg>
        <bean class="com.zaxxer.hikari.HikariConfig">
            <property name="poolName" value="abc_connection_pool" />
            <property name="dataSourceClassName" value="com.informix.jdbcx.IfxDataSource"/>
            <property name="maximumPoolSize" value="100" />
            <property name="connectionTimeout" value="500" />
            <property name="validationTimeout" value="300" />
            <property name="maxLifetime" value="30000" />
            <property name="transactionIsolation" value="1" />
            <property name="dataSourceProperties">
                <props>
                    <prop key="url">${jdbc.url}</prop>
                    <prop key="user">${jdbc.username}</prop>
                    <prop key="password">${jdbc.password}</prop>
                </props>
            </property>
        </bean>
    </constructor-arg>
</bean>

3. Check for Hidden Dependencies

Double-check if any other beans in your context (like transaction managers, JdbcTemplate, or custom DAOs) are referencing dataSource during their initialization. If so, try marking those beans with lazy-init="true" to delay their creation until after dataSource is fully initialized:

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" lazy-init="true">
    <property name="dataSource" ref="dataSource" />
</bean>

This is more of a workaround, but it can help isolate if the issue is coming from another bean triggering early dataSource initialization.

4. Verify Informix Driver Compatibility

While less likely, ensure your Informix driver version (3.00.JC3) is fully compatible with HikariCP-java6 2.3.13. Sometimes older JDBC drivers have quirks that can cause unexpected initialization behavior. If possible, test with a slightly newer compatible driver version (still supporting JDK 1.6) to rule out driver-related issues.

Final Notes

Start with solution 1—it’s the most straightforward and will eliminate the circular reference by simplifying your bean setup. If that doesn’t work, move to solution 2, then check for hidden dependencies.

内容的提问来源于stack exchange,提问作者Adrian

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:18:29