来源: proxool配置过程中出现问题解决 – 阿里巴巴专栏
在spring里配置proxool0.9.1时, <bean id="proxoolDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="alias"> <value>${proxool.alias}</value> </property> <property name="driver"> <value>${proxool.driver}</value> </property> <property name="driverUrl"> <value>${proxool.url}</value> </property> <property name="user"> <value>${proxool.username}</value> </property> <property name="password"> <value>${proxool.password}</value> </property> <property name="minimumConnectionCount"> <value>${proxool.minimumConnectionCount}</value> </property> <property name="maximumConnectionCount"> <value>${proxool.maximumConnectionCount}</value> </property> <property name="prototypeCount"> <value>${proxool.prototypeCount}</value> </property> <property name="simultaneousBuildThrottle"> <value>${proxool.simultaneousBuildThrottle}</value> </property> <property name="maximumActiveTime"> <value>${proxool.maximumActiveTime}</value> </property> <property name="trace"> <value>${proxool.trace}</value> </property> <property name="verbose"> <value>${proxool.verbose}</value> </property> <property name="houseKeepingTestSql"> <value>${proxool.houseKeepingTestSql}</value> </property> <property name="statistics"> <value>${proxool.statistics}</value> </property> <property name="statisticsLogLevel"> <value>${proxool.statisticsLogLevel}</value> </property> <property name="testBeforeUse"> <value>${proxool.testAfterUse}</value> </property> <property name="testAfterUse"> <value>${proxool.testAfterUse}</value> </property> <!-- <property name="maximumConnectionLifeTime"> <value>600</value> </property> <property name="houseKeepingSleepTime"> <value>${proxool.houseKeepingSleepTime}</value> </property> <property name="maximumConnectionLifeTime"> <value>${proxool.maximumConnectionLifeTime}</value> </property> --> </bean> 最后三个属性是不支持的,spring会报错: org.springframework.beans.NotWritablePropertyException: Invalid property 'houseKeepingSleepTime' of bean class [org.logicalcobwebs.proxool.ProxoolDataSource]: Bean property 'houseKeepingSleepTime' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:751) 查看proxool源码得知: org.logicalcobwebs.proxool.ProxoolDataSource.java public class ProxoolDataSource implements DataSource ObjectFactory { private static final Log LOG = LogFactory.getLog(ProxoolDataSource.class); private int loginTimeout; private PrintWriter logWriter; private String alias; private String driver; private String fatalSqlExceptionWrapperClass; private long houseKeepingSleepTime; private String houseKeepingTestSql; private long maximumActiveTime; private int maximumConnectionCount; private long maximumConnectionLifetime;; private int minimumConnectionCount; private long overloadWithoutRefusalLifetime; ................ /** * @see ConnectionPoolDefinitionIF#getHouseKeepingSleepTime */ public void setHouseKeepingSleepTime(int houseKeepingSleepTime) { this.houseKeepingSleepTime = houseKeepingSleepTime; }
这两个地方不一至,一个是long,一个是int,造成了以上的问题
修改源码发现在jdk1.6下无法编译,换成jdk1.5编译通过
需要导入包:apache 工具包等proxool官方有说明
jakarta.apache.org/commons/logging/ – commons-logging-1.0.4.jar
avalon.apache.org/framework – avalon-framework-4.1.2.jar
原作者在写时可能没有考虑到spring的注入机制,只考虑到一般的调用,spring的注入是规则是方法参数的类型必须与变量声明的类型一至,所有在spring中无法注入。现在改了以下方法参数类型:
setHouseKeepingSleepTime(int houseKeepingSleepTime) 改为 setHouseKeepingSleepTime(long houseKeepingSleepTime) setMaximumConnectionLifetime(int maximumConnectionLifetime) 改为 setMaximumConnectionLifetime(long maximumConnectionLifetime) setRecentlyStartedThreshold(int recentlyStartedThreshold) 改为 setRecentlyStartedThreshold(long recentlyStartedThreshold) setOverloadWithoutRefusalLifetime(int overloadWithoutRefusalLifetime) 改为 setOverloadWithoutRefusalLifetime(long overloadWithoutRefusalLifetime) private void populatePropertiesFromReference(Reference reference) { RefAddr property = reference.get(ProxoolConstants.ALIAS_PROPERTY); if (property != null) { setAlias(property.getContent().toString()); } property = reference.get(ProxoolConstants.DRIVER_CLASS_PROPERTY); if (property != null) { setDriver(property.getContent().toString()); } property = reference.get(ProxoolConstants.FATAL_SQL_EXCEPTION_WRAPPER_CLASS_PROPERTY); if (property != null) { setFatalSqlExceptionWrapperClass(property.getContent().toString()); } property = reference.get(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY); if (property != null) { //setHouseKeepingSleepTime(Integer.valueOf(property.getContent().toString()).intValue()); setHouseKeepingSleepTime(Long.valueOf(property.getContent().toString()).longValue()); } property = reference.get(ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY); if (property != null) { setHouseKeepingTestSql(property.getContent().toString()); } property = reference.get(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY); if (property != null) { setMaximumConnectionCount(Integer.valueOf(property.getContent().toString()).intValue()); } property = reference.get(ProxoolConstants.MAXIMUM_CONNECTION_LIFETIME_PROPERTY); if (property != null) { //setMaximumConnectionLifetime(Integer.valueOf(property.getContent().toString()).intValue()); setMaximumConnectionLifetime(Long.valueOf(property.getContent().toString()).longValue()); } property = reference.get(ProxoolConstants.MAXIMUM_ACTIVE_TIME_PROPERTY); if (property != null) { //setMaximumActiveTime(Long.valueOf(property.getContent().toString()).intValue()); setMaximumActiveTime(Long.valueOf(property.getContent().toString()).longValue()); } property = reference.get(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY); if (property != null) { setMinimumConnectionCount(Integer.valueOf(property.getContent().toString()).intValue()); } property = reference.get(ProxoolConstants.OVERLOAD_WITHOUT_REFUSAL_LIFETIME_PROPERTY); if (property != null) { //setOverloadWithoutRefusalLifetime(Integer.valueOf(property.getContent().toString()).intValue()); setOverloadWithoutRefusalLifetime(Long.valueOf(property.getContent().toString()).longValue()); } property = reference.get(ProxoolConstants.PASSWORD_PROPERTY); if (property != null) { setPassword(property.getContent().toString()); } property = reference.get(ProxoolConstants.PROTOTYPE_COUNT_PROPERTY); if (property != null) { setPrototypeCount(Integer.valueOf(property.getContent().toString()).intValue()); } property = reference.get(ProxoolConstants.RECENTLY_STARTED_THRESHOLD_PROPERTY); if (property != null) { //setRecentlyStartedThreshold(Integer.valueOf(property.getContent().toString()).intValue()); setRecentlyStartedThreshold(Long.valueOf(property.getContent().toString()).longValue()); } property = reference.get(ProxoolConstants.SIMULTANEOUS_BUILD_THROTTLE_PROPERTY); if (property != null) { setSimultaneousBuildThrottle(Integer.valueOf(property.getContent().toString()).intValue()); } property = reference.get(ProxoolConstants.STATISTICS_PROPERTY); if (property != null) { setStatistics(property.getContent().toString()); } property = reference.get(ProxoolConstants.STATISTICS_LOG_LEVEL_PROPERTY); if (property != null) { setStatisticsLogLevel(property.getContent().toString()); } property = reference.get(ProxoolConstants.TRACE_PROPERTY); if (property != null) { setTrace("true".equalsIgnoreCase(property.getContent().toString())); } property = reference.get(ProxoolConstants.DRIVER_URL_PROPERTY); if (property != null) { setDriverUrl(property.getContent().toString()); } property = reference.get(ProxoolConstants.USER_PROPERTY); if (property != null) { setUser(property.getContent().toString()); } property = reference.get(ProxoolConstants.VERBOSE_PROPERTY); if (property != null) { setVerbose("true".equalsIgnoreCase(property.getContent().toString())); } property = reference.get(ProxoolConstants.JMX_PROPERTY); if (property != null) { setJmx("true".equalsIgnoreCase(property.getContent().toString())); } property = reference.get(ProxoolConstants.JMX_AGENT_PROPERTY); if (property != null) { setJmxAgentId(property.getContent().toString()); } property = reference.get(ProxoolConstants.TEST_BEFORE_USE_PROPERTY); if (property != null) { setTestBeforeUse("true".equalsIgnoreCase(property.getContent().toString())); } property = reference.get(ProxoolConstants.TEST_AFTER_USE_PROPERTY); if (property != null) { setTestAfterUse("true".equalsIgnoreCase(property.getContent().toString())); } // Pick up any properties that we don't recognise Enumeration e = reference.getAll(); while (e.hasMoreElements()) { StringRefAddr stringRefAddr = (StringRefAddr) e.nextElement(); String name = stringRefAddr.getType(); String content = stringRefAddr.getContent().toString(); if (name.indexOf(ProxoolConstants.PROPERTY_PREFIX) != 0) { delegateProperties.put(name content); } } }
早期这个类型都是int的,可能后来为了可以设置可大于int型10位以上的数字才改为long的,因为如果是毫秒级的话,10位int的最大数是2147483647也就是只能表示596个小时,也就是24天。看了一下源码,好像5个long的变量有改其中一个,另外4个没改,是作者忘记了?还是另有原因?
另外,这里改源码只是方便我这里用spring注入配置,如果是通过手工调用获取连接的,改了设置参数时,要在数字后加一个L或l来表示long.