Results 1 to 8 of 8

Thread: Setting Datasource properties with PropertyPlaceholderConfigurer

  1. #1
    Join Date
    Mar 2007
    Posts
    16

    Default Setting Datasource properties with PropertyPlaceholderConfigurer

    Hi all,

    I am facing an issue on how to set the Datasource using PropertyPlaceholderConfigurer.

    My datasource configuration is like this:
    Code:
    	<bean id="dataSource" destroy-method="close"
          		class="org.apache.commons.dbcp.BasicDataSource">
        	<property name="driverClassName" value="com.ibm.db2e.jdbc.DB2eDriver"/>
        	<property name="url" value="${jdbc.url}"/>
        	<property name="username" value="${jdbc.username}"/>
        	<property name="password" value="${jdbc.password}"/>
    	</bean>
    And since my web down through DAO are using Spring beans
    and JSF managed beans, so I must use application context approach which means loading them with
    Code:
     
    org.springframework.web.context.ContextLoaderServlet
    My application contexts consists of
    Code:
    applicationContext-web.xml, 
    applicationContext-service.xml and applicationContext-dao.xml
    I put the above dataSource definition inside applicationContext-dao.xml.

    The most difficult part is that I can not use properties files such
    as jdbc.properties to change the above placeholders, since
    i need the DB URL, User Id and Password to be determined at Login time.

    DB URL to be determined at runtime? Yeah this is special case since
    DB2e database will access the database based on downsynced data
    which is something like
    Code:
    jdbc:db2e:C:\fooProject\<userId>\
    Hence I need those 3 information to be determined after login.

    The thing is all these 3 contexts are initialized at runtime,
    but the datasource is not defined yet.

    Just fyi, I have tried UserCredentialsDataSourceAdapter but DB2e does not
    support it.

    Anybody has idea about this?

    Thanks in advance.

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Hmm you kind of ruined by answer, I was all ready to say UserCredentialsDataSourceAdapter until the last lines of the post. Why can't you use it with DB2?

  3. #3
    Join Date
    Mar 2007
    Posts
    16

    Default

    Thanks, but its not for DB2 that i am using, it is DB2e with its limitations.

    I have tried it before, and it throws error Unsupported Exception.

    Another thing is that UserCredentialsDataSourceAdapter
    does not support to change JDBC url as well.

  4. #4
    Join Date
    Mar 2007
    Posts
    16

    Default

    I just found that I might use BeanPostProcessor or BeanFactoryPostProcessor
    to customize the dataSource bean before initialized ..

    Do u think this is a good idea?

    Thanks,
    frasyad

  5. #5
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    PropertyPlaceholderConfigurer implements the BeanFactoryPostProcessor interface. Just to clarify what you are trying to do here. Are there going to be lots of dataSource changes? e.g. each time a user logs on. Or is this going to be configured once?

  6. #6
    Join Date
    Mar 2007
    Posts
    16

    Default

    Hi karldmoore,

    Actually the datasource will only be initialized and created everytime
    user logs in.

    Because we are using user id and password of that logged in user
    to be the user id and password to connect to the database.

    Another thing is that the User id will be part of JDBC url,
    as i mentioned before

    Code:
    jdbc:db2e:C:\fooProject\<userId>\
    I don't know if i can use PropertyPlaceHolderConfigurer for this purpose.
    Bcos afaik, PropertyPlaceHolderConfigurer will only get the properties from
    properties file and from System properties.

    However, I have tried to configure the datasource like this:
    Code:
    <bean id="dataSource" destroy-method="close"
          	class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.ibm.db2e.jdbc.DB2eDriver"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    </bean>
    
    
    <bean id="placeholderConfig" 
    	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName">
    <value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
    </property>
    <property name="ignoreUnresolvablePlaceholders">
    <value>true</value>
    </property>
    <property name="location">
    <value>file:c:/xxx/jdbc.properties</value>
    </property>
    </bean>
    But then I got this error which says that Can not create datasource with
    url "jdbc.url".

    And fyi, I have set the properties before calling the DAO like this
    Code:
    System.setProperty("jdbc.url", url);
    System.setProperty("jdbc.username", dbUserId);
    System.setProperty("jdbc.password", dbPassword);
    It seems that the datasource definition after loaded in ApplicationContext
    can not be overrided anymore.

    Please do help with this issue.

    Thanks,
    frasyad

  7. #7
    Join Date
    Mar 2007
    Posts
    515

    Default

    Have you looked at this thread ? http://forum.springframework.org/showthread.php?t=36993
    Might help.

  8. #8
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I think the problem here is that the dataSource is actually scoped for the users login. As the UserCredentialsDataSourceAdapter doesn't work for you, you could look at scoped beans. This doesn't seem ideal though and would be more expensive.
    http://www.springframework.org/docs/...factory-scopes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •