我也做过同样的事情,就像一些博客所说的那样。但我仍然无法解决问题。
环境:
spring - 4.2.5.RELEASE
spring boot - 1.3.3.RELEASE
hikariCP - 2.4.7
@Bean(name="HikariDataSource",destroyMethod = "shutdown")
public DataSource dataSource2() {
HikariConfig config = new HikariConfig();
config.setDriverClassName("oracle.jdbc.OracleDriver");
config.setJdbcUrl("");
config.setUsername("");
config.setPassword("");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
config.setPoolName("Hikaripool-1");
HikariDataSource ds = new HikariDataSource(config);
return ds;
}
@Bean
public JdbcTemplate getJdbcTemplate() {
return new JdbcTemplate(dataSource2());
}
例外:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.jmx.export.UnableToRegisterMBeanException: Unable to register MBean [HikariDataSource (null)] with key 'dataSource'; nested exception is javax.management.InstanceAlreadyExistsException: com.zaxxer.hikari:name=dataSource,type=HikariDataSource
Caused by: javax.management.InstanceAlreadyExistsException: com.zaxxer.hikari:name=dataSource,type=HikariDataSource
任何人都可以帮我解决这个问题。
答案 0 :(得分:1)
错误“InstanceAlreadyExistsException:com.zaxxer.hikari:name = dataSource”表示还有一个数据源实例。请检查您的配置。 以下是使用HikariCP,Spring Boot 1.4.0和hsqldb的jdbcTemplate的示例代码。请检查下面的HikariCPConnTest.java。希望这会有所帮助。
<强>的pom.xml 强>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Spring-HikariCP</groupId>
<artifactId>com.my</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<强> application.properties 强>
spring.datasource.driverClassName=org.hsqldb.jdbc.JDBCDriver
spring.datasource.url=jdbc:hsqldb:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
HikariCPConn.java
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@Component
@ConfigurationProperties
public class HikariCPConn {
@Value("${spring.datasource.driverClassName}")
private String driverClassName;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String user;
@Value("${spring.datasource.password}")
private String password;
@Bean
public DataSource getDataSource() {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setUsername(user);
hikariConfig.setPassword(password);
hikariConfig.setDriverClassName(driverClassName);
hikariConfig.setJdbcUrl(url);
return new HikariDataSource(hikariConfig);
}
@Bean
public JdbcTemplate getJdbcTemplate() {
return new JdbcTemplate(getDataSource());
}
}
<强> MySpringBootApp.java 强>
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringBootApp {
public static void main(String[] args) {
SpringApplication.run(new Object[] { MySpringBootApp.class }, args);
}
}
<强> HikariCPConnTest.java 强>
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MySpringBootApp.class)
public class HikariCPConnTest {
@Autowired
HikariCPConn hkConn;
@Test
public void testInsert() {
JdbcTemplate jt = hkConn.getJdbcTemplate();
jt.execute("create table employee (id int, name varchar(20))");
jt.execute("insert into employee (id, name) values (1, 'Emp1')");
jt.execute("insert into employee (id, name) values (2, 'Emp2')");
List<Map<String, Object>> data = jt.queryForList("select * from employee");
assertEquals(2,data.size());
for (Map map : data) {
System.out.println(map);
}
}
}