• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Database datasource bean: how to set properties at runtime?

 
Greenhorn
Posts: 6
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.

I think it would be easier to understand the problem if I wrote its context first. I have a Spring application which uses data stored in a database. The database info such as url, username and password should not be fixed. Here's what I want to achieve: after the applications starts, the user is prompted for a url of the database, username and password. The given information is used to set the properties of a datasource bean.

I did find a solution but I don't think it's in a good programming style. Here's the xml file code with the definition of the datasource bean:



The prompting for the db information takes place in the main method. Here's the code:



and the setting of the properties takes place at the construction of Zarzadca object:



This solutions works but I'm not happy with it. I know there is a possibility to point the bean to it's init method, where I could prompt for and set the properties of the db. So I was thinking about writing a class that extends the com.mchange.v2.c3p0.ComboPooledDataSource class and put an init method there. Sadly, I found out that the class is final and therefore cannot be extended. Could someone please help me solve this? I would be very thankful.

p.s. oh, and I would rather not use the property files.
 
Paul Piesiak
Greenhorn
Posts: 6
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I think I found another solution that looks much better.
I created an interface called DataSourceConfigInterface:

I also created a class ComboPooledDataSourceConfig that implements this interface and is used to set the com.mchange.v2.c3p0.ComboPooledDataSource implementation of the data source. In this class the user is promted for the url, username and password and these information are set to the data source:

I moved the definition of dataSource bean from the dao context to another xml file, so now I have this:
dbDataReaderContext.xml

and a new xml file dbDataSourceContext.xml

notice that I used the "init-method" in the dataSourceConfig bean so the method would start automatically. If one would use some other data source implementation which properties need to be set up at runtime, just create a new config class and a new xml file like the one above.
So now my main method looks like this:

and my Zarzadca constructor looks like this:


I think this looks much better than the previous code. But if anyone has a different idea or would like to comment this solution please let me know.
 
What a show! What atmosphere! What fun! What a tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic