You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Bruno P. Kinoshita (JIRA)" <ji...@apache.org> on 2016/10/26 23:56:58 UTC

[jira] [Commented] (DBCP-471) Cannot use connectionInitSqls property in BasicDataSource

    [ https://issues.apache.org/jira/browse/DBCP-471?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15610090#comment-15610090 ] 

Bruno P. Kinoshita commented on DBCP-471:
-----------------------------------------

Hi,

I successfully reproduced the issue with:

- Create a simple Maven project
- Add the following dependencies: org.apache.commons:commons-dbcp2:2.1.1, and org.springframework:spring-core:3.2.4.RELEASE, and org.springframework:spring-context:3.2.4.RELEASE
- Copy the XML with bean definition provided in this issue, as something like spring.xml in the src/main/resources
- And then finally create a class with code similar to the following

{code}
package test;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

    public static void main(String[] args) throws Exception {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        BasicDataSource ds = (BasicDataSource) ctx.getBean("dataSource");
        System.out.println(ds.getUrl());
        ((ClassPathXmlApplicationContext) ctx).close();
        System.out.println("OK!");
    }

}
{code}

Running this code, instead of getting the DataSource URL, followed by OK!, you will get the following stack trace:

{noformat}
Oct 27, 2016 12:49:13 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c4437c4: startup date [Thu Oct 27 12:49:13 NZDT 2016]; root of context hierarchy
Oct 27, 2016 12:49:13 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Oct 27, 2016 12:49:13 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@49e4cb85: defining beans [dataSource]; root of factory hierarchy
Oct 27, 2016 12:49:13 PM org.springframework.beans.factory.support.DefaultListableBeanFactory destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@49e4cb85: defining beans [dataSource]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [spring.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'connectionInitSqls' of bean class [org.apache.commons.dbcp2.BasicDataSource]: Bean property 'connectionInitSqls' 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.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1423)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1128)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at test.App.main(App.java:10)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'connectionInitSqls' of bean class [org.apache.commons.dbcp2.BasicDataSource]: Bean property 'connectionInitSqls' 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:1042)
	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:902)
	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:57)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1420)
	... 13 more
{noformat}

However, I believe this was fixed in [SPR-10995|https://jira.spring.io/browse/SPR-10995], released in Spring 3.2.5.RELEASE. If you update your pom.xml, to use 3.2.5, the issue is gone, and you see the right output.

Further issues included in [3.2.5 changelog|http://docs.spring.io/spring/docs/3.2.5.RELEASE/changelog.txt], which was released in 2013-11-06.

The issue is not really related to Commons Dbcp, but to dependency injection libraries. And if you use the latest stable versions (4.3.3.RELEASE from 2016-09-20 or 3.2.17.RELEASE from 2016-05-06) you won't have this problem.

> Cannot use connectionInitSqls property in BasicDataSource
> ---------------------------------------------------------
>
>                 Key: DBCP-471
>                 URL: https://issues.apache.org/jira/browse/DBCP-471
>             Project: Commons Dbcp
>          Issue Type: Bug
>    Affects Versions: 2.1.1
>            Reporter: happybuddha
>
> Error : 
> {code}Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'connectionInitSqls' of bean class [org.apache.commons.dbcp2.BasicDataSource]: Bean property 'connectionInitSqls' 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:1042)
> 	at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:902)
> 	at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
> {code}
> Configuration : 
> {code}
>   <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
>     <property name="driverClassName" value="${database.driverClassName}" />
>     <property name="url" value="${database.url}" />
>     <property name="username" value="${database.username}" />
>     <property name="password" value="${database.password}" />
>     <property name="connectionInitSqls">
>     <list>
>     	<value>Select 1 from dual;</value>
>     </list>
>     </property>
>   </bean>
> {code}
> Similar issue for another bean : https://issues.apache.org/jira/browse/DBCP-469
> The property is listed as a List<String>, has a getter which returns a List<String> but has a setter which takes a Collection<String> 
> This is also not in line with what was in versions before 2 (dbcp.BasicDataSource)



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)