Tomcat: HikariCP issue when deploying two applications with DB connection

南笙酒味 提交于 2019-12-19 06:46:20

问题


I am trying to deploye two WAR files (app1.war and app2.war) on the same tomcat7 instance. I am getting this error :

Unable to register MBean [HikariDataSource (HikariPool-0)] with key
  'dataSource'; nested exception is javax.management.InstanceAlreadyExistsException:
  com.zaxxer.hikari:name=dataSource,type=HikariDataSource

I don't have this error if I have only one application deployed on tomcat. Is there a way to solve this issue?


回答1:


in spring boot, jmx bean is loaded at run time and it scans your application. If two data sources are found, its going to throw javax.management.InstanceAlreadyExistsException. This can be resolved by defining the default jmx default domain name in your application.properties file as follows

spring.jmx.default-domain=app_name

I hope this helps.




回答2:


In Spring Boot you can change the name of the Hikari data source pool via application.properties:

spring.datasource.hikari.poolName=MyDataPoolName

or application.yml respectivly:

spring:
  datasource:
    hikari:
      pool-name: MyDataPoolName

Then Tomcat can load both applications and the name conflict is gone.




回答3:


Give your data sources unique names. For example, if you have separate schemas for storing user and product data, you could name your data sources userDS and productDS, respectively.

Programmatic configuration


HikariDataSource userDS = new HikariDataSource();
userDS.setPoolName("userDS");
// Set other data source properties.

HikariDataSource productDS = new HikariDataSource();
productDS.setPoolName("productDS");
// Set other data source properties.

Spring configuration


<bean id="userDS" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
  <property name="poolName" value="userDS"/>
  ...
</bean>

<bean id="productDS" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
  <property name="poolName" value="productDS"/>
  ...
</bean>


来源:https://stackoverflow.com/questions/34284984/tomcat-hikaricp-issue-when-deploying-two-applications-with-db-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!