Announcement Announcement Module
No announcement yet.
Dynamically set JDBC properties Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamically set JDBC properties

    I'm currently writing quite a few batch programs, i.e. non-Web applications being started from a main method.

    Each of these programs should logon to the data bases using its own dedicated username. These users will be set up in the data bases and granted exactly those rights that the respective programs need.

    Each program should also be able to run on different data bases: development, test, and production. This will be controlled by setting a command line parameter.

    Unfortunately, for n programs, this leaves me with 3*n application context files, if I store the JDBC params there. Of course I split my app context, having files that contain only the dataSource object definition, but anyhow, 3*n of those files are needed.

    It would be so easy to determine username and passwd programmatically. However, this doesn't work in a straightforward way, as a call of new WhateverApplicationContext(...) will both create and wire the beans, and open the data base. So I can't get at the dataSource bean in between to fill in the right values.

    I also thought of writing a bean post processor for populating the dataSource. But both the command line arguments and the knowledge which main program is running are sitting in the main method, and I don't see a way to access this main method from the bean post processor.

    Any clever ideas or pointers would be appreciated.

  • #2
    You can put the arguments into the Java system properties, and then use a PropertyPlaceholderConfigurer which will do the substitution when you load your application context. You'll need to set a property on the PropertyPlaceholderConfigurer which will also take properties from the Java system properties.