Spring @RefreshScope does not work on final class ComboPooledDataSource

时间:2017-04-24 17:34:51

标签: spring c3p0

I am using CGLib (AOP) proxy. Is there any workaround when ComboPooledDataSource is final class as @RefreshScope does not work on final class?

@Bean(name = "portalDataSource", destroyMethod = "close")
@RefreshScope
public DataSource dataSource() Integer iMaxConTimeout) throws Exception {
    ComboPooledDataSource cpds = new ComboPooledDataSource();
    cpds.setDriverClass("com.mysql.jdbc.Driver"); //loads the jdbc driver  
    cpds.setJdbcUrl("....");
    cpds.setUser("...");
    cpds.setPassword("...");


    // the settings below are optional -- c3p0 can work with defaults
    cpds.setMinPoolSize(iMinDBCons);
    cpds.setMaxPoolSize(iMaxDBCons);
    cpds.setMaxIdleTime(iMaxConTimeout);    
    return cpds;
}

The final class ComboPooledDataSource is part of c3p0 connection pool.

<!-- Hibernate c3p0 connection pool -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>5.0.4.Final</version>
</dependency>

1 个答案:

答案 0 :(得分:0)

第1步:创建一个名为SigComboPooledDataSource的类

public class SigComboPooledDataSource extends TransactionAwareDataSourceProxy {

    @Autowired
    // Inject your class by constructor
    SigComboPooledDataSource(ComboPooledDataSource dataSource) {
        super.setTargetDataSource(dataSource);
    }

    public void close() {
         ((ComboPooledDataSource) super.getTargetDataSource()).close();
    }

}

第2步

@Bean(name = "portalDataSource", destroyMethod = "close")
@RefreshScope
public DataSource dataSource() Integer iMaxConTimeout) throws Exception {

    ComboPooledDataSource cpds = new ComboPooledDataSource();    
    cpds.setDriverClass("com.mysql.jdbc.Driver"); //loads the jdbc driver      
    cpds.setJdbcUrl("....");    
    cpds.setUser("...");
    cpds.setPassword("...");   
    cpds.setPassword("...");

    // the settings below are optional -- c3p0 can work with defaults
    cpds.setMinPoolSize(iMinDBCons);
    cpds.setMaxPoolSize(iMaxDBCons);
    cpds.setMaxIdleTime(iMaxConTimeout);    
    return new SigComboPooledDataSource(cpds);
}