H2數(shù)據(jù)庫(kù)配置為類(lèi)似jdbc:h2:tcp://localhost/~/test/database
的形式雁歌,就是Server Mode绎签,如果沒(méi)有單獨(dú)啟動(dòng)H2服務(wù)器壮韭,那么會(huì)導(dǎo)致如下錯(cuò)誤:
org.h2.jdbc.JdbcSQLException: Connection is broken: "java.net.ConnectException: Connection refused: connect: localhost" [90067-196]
Caused by: java.net.ConnectException: Connection refused: connect
org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.h2.jdbc.JdbcSQLException: Connection is broken: "java.net.ConnectException: Connection refused: connect: localhost" [90067-196]
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.h2.jdbc.JdbcSQLException: Connection is broken: "java.net.ConnectException: Connection refused: connect: localhost" [90067-196]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
大概就是連接失敗一類(lèi)的問(wèn)題产徊,網(wǎng)上說(shuō)是因?yàn)閟pringboot比h2服務(wù)器啟動(dòng)更早胞四,然后導(dǎo)致這個(gè)錯(cuò)誤的,具體原因還有待查證闸翅,解決方案是:
- 創(chuàng)建類(lèi)
Starters
(類(lèi)名無(wú)所謂再芋,后面引用就可以)
import org.h2.tools.Server;
public class Starters {
private static final Logger logger = LoggerFactory.getLogger(Starters.class);
public static void startH2Server() {
try {
Server h2Server = Server.createTcpServer().start(); // 關(guān)鍵代碼
if (h2Server.isRunning(true)) {
logger.info("H2 server was started and is running.");
} else {
throw new RuntimeException("Could not start H2 server.");
}
} catch (SQLException e) {
throw new RuntimeException("Failed to start H2 server: ", e);
}
}
}
注意:Springboot默認(rèn)設(shè)置h2的依賴范圍是runtime
,需要?jiǎng)h除那句代碼才能編譯時(shí)調(diào)用org.h2.tools.Server
坚冀。
- 分別在SpringBoot啟動(dòng)類(lèi)
ServletInitializer
和Application
中調(diào)用H2 server的啟動(dòng)代碼
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
Starters.startH2Server(); // 關(guān)鍵代碼
return application.sources(Application.class);
}
}
@SpringBootApplication
public class Application {
public static void main(String[] args) {
Starters.startH2Server(); // 關(guān)鍵代碼
SpringApplication.run(Application.class, args);
}
}
這樣修改之后,就可以正常啟動(dòng)了记某!
參考:How to start H2 TCP server on Spring Boot application startup?