General J2EE: How to dynamically configure a datasource
Hi, I have a web application requiring to connect to one of several SQL Server databases. They are all identical in structure; what changes is the IP address, user and password. I am using JBoss; so far I had configured datasources in a static way as -ds.xml files in the app server deploy directory. But in this case I don't know the connection parameters in advance: they are discovered at runtime depending on the user who logs in. Is there any standard way to dynamically setup a javax.sql.Datasource object and do the JNDI binding at runtime? Can this code be independent of the application server being used? Thanks
- Posted by: Aldo Laiseca
- Posted on: October 11 2006 17:33 EDT
As far a I know, you cannot just change the whole datasource at run time. What you can do is, create datasources for all the databases you have and they will have the jndi binding. Now according to the user input, you can look up different datasources. This will anyway solve your problem. cheers, http://javaicillusion.blogspot.com/
Imagine that you are in Java SE environment (where you can create JDBC connections and datasources manually), then (don't forget completely about Java EE ;)) bind/rebind the newly created datasource to JNDI under the same name (so your apps will see it).
Offcourse, you can bind unbind and also rebind resources under same name dynamically, but how far will it scale. And binding with existing name is skeptical. cheers,