Announcement Announcement Module
Collapse
No announcement yet.
connection properties setup Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • connection properties setup

    Hello,

    I use Apache poolable datasource BasicDataSource. Here's the bean conf:
    Code:
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    	    <property name="driverClassName"><value>$&#123;jdbc.driverClassName&#125;</value></property>
    	    <property name="url"><value>$&#123;jdbc.url&#125;</value></property>
    	    <property name="username"><value>$&#123;jdbc.username&#125;</value></property>
    	    <property name="password"><value>$&#123;jdbc.password&#125;</value></property>
        </bean>
    and I want to set up some params for the connection as it's done in:

    Code:
    Properties props = new Properties&#40;&#41;;
        try &#123;
          Class.forName&#40;"org.gjt.mm.mysql.Driver"&#41;.newInstance&#40;&#41;;
          props.put&#40;"useUnicode","true"&#41;;
          props.put&#40;"characterEncoding","Cp1251"&#41;;
          conn = DriverManager.getConnection&#40;url, props&#41;;
        &#125; catch &#40;...&#41;
    How can i achieve this in spring?

    thank you!

  • #2
    BasicDataSource has a protected connectionProperties that is only accessed using addConnectionProperty / removeConnectionProperty. AFAIK Spring IoC can not invoke these methods.
    If all the connections you will get from the BasicDataSource will use the same properties, you can extend BasicDataSource to add a getter/setter for connectionProperties then configure the dataSource as follows:
    Code:
      <bean id="dataSource" class="org.taha.datasources.MyBasicDataSource" destroy-method="close">
        <property name="driverClassName"><value>$&#123;jdbc.driverClassName&#125;</value></property>
        <property name="url"><value>$&#123;jdbc.url&#125;</value></property>
        <property name="username"><value>$&#123;jdbc.username&#125;</value></property>
        <property name="password"><value>$&#123;jdbc.password&#125;</value></property>
        <property name="connectionProperties">
          <props>
            <prop key="useUnicode">true</prop>
            <prop key="characterEncoding">Cp1251</prop>
          </props>
        </property>
      </bean>
    HTH

    Comment

    Working...
    X