| 47 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 48 | |
| 49 | @Component |
| 50 | public class DbcpFactory implements DbcpPoolHandler,GlobalConfigUpdateListener { |
| 51 | private static Logger logger = Logger.getLogger( DbcpFactory.class.getName() ); |
| 52 | |
| 53 | private final GlobalConfigHandler globalConfigHandler; |
| 54 | |
| 55 | private final Map<String,DataSource> dataSources = new ConcurrentHashMap<String,DataSource>(); |
| 56 | List<DbcpInitialisedListener> listeners = new ArrayList<DbcpInitialisedListener>(); |
| 57 | boolean initialised = false; |
| 58 | |
| 59 | @Autowired |
| 60 | public DbcpFactory(GlobalConfigHandler globalConfigHandler) |
| 61 | { |
| 62 | this.globalConfigHandler = globalConfigHandler; |
| 63 | globalConfigHandler.addSubscriber("dbcp", this); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | private void createDbcp(DbcpConfig conf) |
| 69 | { |
| 70 | if (!dataSources.containsKey(conf.name)) |
| 71 | { |
| 72 | try |
| 73 | { |
| 74 | |
| 75 | Class.forName(conf.driverClassName); |
| 76 | |
| 77 | DriverManagerConnectionFactory cf = new DriverManagerConnectionFactory(conf.jdbc,conf.user,conf.password); |
| 78 | |
| 79 | PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf,null); |
| 80 | pcf.setValidationQuery(conf.validationQuery); |
| 81 | //, pool, null, conf.validationQuery, false, true,abandondedConfig); |
| 82 | |
| 83 | logger.info("Creating pool "+conf.toString()); |
| 84 | // create a generic pool |
| 85 | GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(pcf); |
| 86 | pool.setMaxTotal(conf.maxTotal); |
| 87 | pool.setMaxIdle(conf.maxIdle); |
| 88 | pool.setMinIdle(conf.minIdle); |
| 89 | pool.setMaxWaitMillis(conf.maxWait); |
| 90 | pool.setTimeBetweenEvictionRunsMillis(conf.timeBetweenEvictionRunsMillis); |
| 91 | pool.setMinEvictableIdleTimeMillis(conf.minEvictableIdleTimeMillis); |
| 92 | pool.setTestWhileIdle(conf.testWhileIdle); |
| 93 | pool.setTestOnBorrow(conf.testOnBorrow); |
| 94 | |
| 95 | AbandonedConfig abandonedConfig = new AbandonedConfig(); |
| 96 | abandonedConfig.setRemoveAbandonedOnMaintenance(conf.removeAbanadoned); |
| 97 | abandonedConfig.setRemoveAbandonedTimeout(conf.removeAbandonedTimeout); |
| 98 | abandonedConfig.setLogAbandoned(conf.logAbandonded); |
| 99 | |
| 100 | pool.setAbandonedConfig(abandonedConfig); |
| 101 | |
| 102 | pcf.setPool(pool); |
| 103 | DataSource ds = new PoolingDataSource(pool); |
| 104 | dataSources.put(conf.name, ds); |
| 105 | |
| 106 | } catch (ClassNotFoundException e) { |