I am using spring with have multiple datasources (catering to multiple mysql dbs) and using tomcat dbcp. I am getting some weird exceptions like
- procedure not found - when the proc is definitely present in the db
- cannot borrow from pool - local dev setup, so definitely the pool is not full
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
.
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
You should certainly not use a shared a
JdbcTemplate
if you're going to keep changing itsDataSource
. Set theDataSource
once, and leave it alone.That either means multiple
JdbcTemplate
beans, one for eachDataSource
, or create newJdbcTemplate
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 theDataSource
manually. Might as well instantiateJdbcTemplate
usingnew
.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?