开发者

Spring JDBC with Tomcat DBCP and multiple datasources

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开发者_JAVA百科 the stored proc is defined as prototype.


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.


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?


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

  1. Hibernate, Spring and multiple DataSources part 1
  2. Hibernate, Spring and multiple DataSources part 2
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜