I need to change the url of data source using session or change current connection to new connection or build new data source at runtime (in my controller or service)
if any one can tell me another way exclude multi data source tell me
Thank you
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I have an approach that’s not the cleanest, but should work. If you get access to the
dataSourceSpring bean you can change its properties. You need to get it to close all pooled connections and then new connections will use the new settings.You can get to the datasource using dependency injection like any bean, i.e.
def dataSourceUnproxied. You have to use “dataSourceUnproxied” instead of “dataSource” to get at the realDataSource, not the transaction-aware proxy that Grails wraps the real one with.Having done that you can change the url, username, etc. like this:
Then close it to force all connections to close, but reset the
closedflag to trick it into re-connecting the next timegetConnection()is called:This is very specific to the pool implementation since the
DataSourceinterface only has a few methods. This works with theorg.apache.commons.dbcp.BasicDataSourcethat Grails uses by default but if you’re using a different pool implementation you’ll need to look at its source for an equivalent approach.