Announcement Announcement Module
Collapse
No announcement yet.
Configuring org.apache.commons.dbcp.BasicDataSource Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Configuring org.apache.commons.dbcp.BasicDataSource

    Spring version: 2.0.5

    I am using the apache BasicDataSource for connection pooling using the following bean configuration.

    <bean id="localDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.sybase.jdbc3.jdbc.SybDriver"/>
    <property name="username" value="user1"/>
    <property name="password" value="pass1"/>
    <property name="url" value="jdbc:sybase:Tds:datasvcs:5001/raccount"/>
    <property name="initialSize" value="1"/>
    <property name="maxActive" value="10"/>
    </bean>

    It has been working fine, but now I need to add the connection property using the method call BasicDataSource.addConnectionProperty("DYNAMIC_PRE PARE", "true");

    How can I tell spring to execute above method call when the datasource is instantiated. I may have to set more than one Connection property.

  • #2
    Doesn't look like it exposes a setProperties(..) method. You could extend the class and add one yourself.
    Last edited by karldmoore; Aug 29th, 2007, 11:55 AM.

    Comment


    • #3
      karldmoore,

      Thanks for the quick reply.

      I can extend the class and add appropriate methods, I will need to add one method for each property. Is there any way to call the arbitrary method with parameters at initilization. If I can do that then I can just call the addConnectionProperty method few times with appropriate parameters.

      Comment


      • #4
        Originally posted by rajwani View Post
        karldmoore,

        Thanks for the quick reply.

        I can extend the class and add appropriate methods, I will need to add one method for each property. Is there any way to call the arbitrary method with parameters at initilization. If I can do that then I can just call the addConnectionProperty method few times with appropriate parameters.
        You can as well implement BeanPostProcessor that will do these calls for you.
        Properties names and values you may specify in the configuration for this BeanPostProcessor.

        Regards,
        Oleksandr

        Comment


        • #5
          I can extend the class and add appropriate methods, I will need to add one method for each property. Is there any way to call the arbitrary method with parameters at initilization. If I can do that then I can just call the addConnectionProperty method few times with appropriate parameters.
          Just implement a setProperties method as karl suggested. Let this method take a Properties object as a parameter and you should be ok.

          Code:
          public void setProperties(Properties props) {
              Iterator entries = props.entrySet().iterator();
              while (entries.hasNext()) {
                Entry entry = (Entry) entries.next();
                addConnectionProperty(entry.getKey(), entry.getValue());
              }
          }

          Comment


          • #6
            Thanks everybody for your help. I just created the extended class which implements only one method setProperties, as suggested by mdeinum.

            Comment


            • #7
              Originally posted by rajwani View Post
              Thanks everybody for your help. I just created the extended class which implements only one method setProperties, as suggested by mdeinum.
              I'm not sure why this method isn't exposed in the first place, I'm petty sure I'm read quite a few posts like this in the past!
              Last edited by karldmoore; Aug 29th, 2007, 11:55 AM.

              Comment


              • #8
                I used:

                <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
                <property name="driverClassName" value="${driverClassName}" />
                <property name="url" value="${url}" />
                <property name="username" value="${username}" />
                <property name="password" value="${password}" />
                <property name="minIdle" value="2" />
                </bean>

                <bean class="org.springframework.beans.factory.config.Me thodInvokingFactoryBean" lazy-init="false">
                <property name="targetObject" ref="dataSource" />
                <property name="targetMethod" value="addConnectionProperty" />
                <property name="arguments">
                <list>
                <value>characterEncoding</value>
                <value>UTF-8</value>
                </list>
                </property>
                </bean>

                ------------------------
                FYI

                Comment

                Working...
                X