this might sound weird. want know how/if can create datasource objects during runtime , not when container start-up.
here problem working on:
i have mysql database stores url, username , password other sql servers need connect , overnight processing. list of sql servers changes everytime. cannot hard-coded in properties files. further, no of sql servers 5000 or more.
the business logic involves reading mysql database (which datasource bean created during container start-up) , each entry in sql_server_mapping table in mysql database, need connect database , run reports.
i thinking of doing along line each of sql server instances
public datasource getdatasource(string url, string u, string p, string class) { return datasourcebuilder .create() .username(u) .password(p) .url(url) .driverclassname(class) .build(); } public jdbctemplate jdbctemplate(datasource datasource) { return new jdbctemplate(datasource); }
here builder generates datasource given url , create necessary jdbctemplate it. create 1 each of sql server configurations. concern creating 5000 datasources , 5000 jdbctemplate or perhaps more. doesn't sound right me. right way around here?
is there way remove datasource objects done or recycle them?
should cache these datasource objects in spring application, dont have create 1 each time , discard it. implies, need cache 5000 (or more in future).
spring docs says
the datasource should configured bean in spring ioc container. in first case bean given service directly; in second case given prepared template.
so makes things harder me.
thanks
you can define bean mybean scope prototype , use getbean(string name, object... args) method of beanfactory. args args sent constructor (in case these db connections). bean return jdbctemplate constructed datasource defined connection properties. template can further used in other classes.
since scope of bean prototype, created instances garbaged collected after current object used. can if have memory constraints, heavy lifting in terms of creating objects done when getting actual db connections. caching solution in case of heavy re-usage of connections.
see example of bean , method usage here: spring bean dynamic constructor value
Comments
Post a Comment