Reputation: 11
I am using spring with have multiple datasources (catering to multiple mysql dbs) and using tomcat dbcp. I am getting some weird exceptions like
the Problem I feel might be this, need inputs from everyone:
I have one jdbcTemplate
object defined in my spring.xml
, on every query that I need to fire, I call jdbcTemplate.setDataSource()
to set the appropriate datasource and then use simplejdbccall(jdbctemplate)
to execute the proc.
Should I go in for also defining multiple jdbcTemplate
objects, i.e. one for each datasource defined. The bean from where I am setting the datasource on the jdbctemplate
and executing the stored proc is defined as prototype
.
Upvotes: 1
Views: 5133
Reputation: 80176
Spring has some level of native support for switching the data sources dynamically. Here is the article on how you do it.
Below also can help
Upvotes: 0
Reputation: 113
Thanks for the answer. I have got past those errors by creating a new instance of jdbctemplate everytime. I have also updated to the latest mysql jconnector jsr (5.1.14) The class design is now pretty simple. I have a base dao which uses a new instance of a custom written spring jdbc wrapper which in turn instantiates a jdbcTemplate object as an instance variable. This instance variable is used in a new instance of a SimpleJdbcCall for every request. all my dao classes inherit from this base dao.
However there are some intermittent errors like this :
org.springframework.jdbc.support.MetaDataAccessException: Error while extracting DatabaseMetadata .... . . . caused by : org.apache.tomcat.dbcp.dbcp.PoolingDataSource.checkConnection() : connection is closed. I don't see a pattern for this error. I have an initialSize of 10 for each of the datasources & a maxActive of 100. any tips regarding what could be the issue here?
Upvotes: 1
Reputation: 403471
You should certainly not use a shared a JdbcTemplate
if you're going to keep changing its DataSource
. Set the DataSource
once, and leave it alone.
That either means multiple JdbcTemplate
beans, one for each DataSource
, or create new JdbcTemplate
objects manually, on demand, and don't share them. There's no significant performance overhead in creating new ones, do that's not an issue.
You could define them as prototype
, sure, but there's not much point if you're going to inject the DataSource
manually. Might as well instantiate JdbcTemplate
using new
.
Upvotes: 5