Linux下h2数据库java.net.UnknownHostException异常处理

Linux下h2数据库java.net.UnknownHostException异常处理

最近再用spring boot开发的时候,数据库在使用h2数据库的管理界面出现了异常,具体如下

org.h2.message.DbException: IO Exception: "java.net.UnknownHostException: gentoo: gentoo: 未知的名称或服务" [90028-199]
 at org.h2.message.DbException.get(DbException.java:194) ~[h2-1.4.199.jar:1.4.199]
 at org.h2.message.DbException.convert(DbException.java:339) ~[h2-1.4.199.jar:1.4.199]
 at org.h2.util.NetUtils.getLocalAddress(NetUtils.java:254) ~[h2-1.4.199.jar:1.4.199]
 at org.h2.server.web.WebServer.updateURL(WebServer.java:374) ~[h2-1.4.199.jar:1.4.199]
 at org.h2.server.web.WebServer.init(WebServer.java:363) ~[h2-1.4.199.jar:1.4.199]
 at org.h2.server.web.WebServlet.init(WebServlet.java:50) ~[h2-1.4.199.jar:1.4.199]
 at javax.servlet.GenericServlet.init(GenericServlet.java:158) ~[tomcat-embed-core-9.0.26.jar:9.0.26]
 at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1134) [tomcat-embed-core-9.0.26.jar:9.0.26]
 at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:777) [tomcat-embed-core-9.0.26.jar:9.0.26]
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135) [tomcat-embed-core-9.0.26.jar:9.0.26]
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.26.jar:9.0.26]
 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526) [tomcat-embed-core-9.0.26.jar:9.0.26]
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.26.jar:9.0.26]
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.26.jar:9.0.26]
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.26.jar:9.0.26]
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.26.jar:9.0.26]
 at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.26.jar:9.0.26]
 at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.26.jar:9.0.26]
 at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860) [tomcat-embed-core-9.0.26.jar:9.0.26]
 at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589) [tomcat-embed-core-9.0.26.jar:9.0.26]
 at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.26.jar:9.0.26]
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_222]
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_222]
 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.26.jar:9.0.26]
 at java.lang.Thread.run(Thread.java:748) [na:1.8.0_222]
Caused by: org.h2.jdbc.JdbcSQLNonTransientException: IO Exception: "java.net.UnknownHostException: gentoo: gentoo: 未知的名称或服务" [90028-199]
 at org.h2.message.DbException.getJdbcSQLException(DbException.java:502) ~[h2-1.4.199.jar:1.4.199]
 at org.h2.message.DbException.getJdbcSQLException(DbException.java:427) ~[h2-1.4.199.jar:1.4.199]
 ... 25 common frames omitted

从字面意思上来看是没有识别到gentoo这个host,因为我这是linux所以我检查了一下/etc/hosts文件

chengjian@gentoo  ~  cat /etc/hosts 
# /etc/hosts: Local Host Database
#
# This file describes a number of aliases-to-address mappings for the for 
# local hosts that share this file.
#
# The format of lines in this file is:
#
# IP_ADDRESS canonical_hostname [aliases...]
#
#The fields can be separated by any number of spaces or tabs.
#
# In the presence of the domain name service or NIS, this file may not be 
# consulted at all; see /etc/host.conf for the resolution order.
#
# IPv4 and IPv6 localhost aliases
127.0.0.1 localhost
::1 localhost

果然host里面127.0.0.1的是localhost,很简单的处理将gentoo加入到hosts文件

# /etc/hosts: Local Host Database
#
# This file describes a number of aliases-to-address mappings for the for 
# local hosts that share this file.
#
# The format of lines in this file is:
#
# IP_ADDRESS canonical_hostname [aliases...]
#
#The fields can be separated by any number of spaces or tabs.
#
# In the presence of the domain name service or NIS, this file may not be 
# consulted at all; see /etc/host.conf for the resolution order.
#
# IPv4 and IPv6 localhost aliases
127.0.0.1 localhost gentoo
::1 localhost

修改好后再ping一下gentoo有反馈了

chengjian@gentoo  ~  ping gentoo
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.035 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.034 ms

再重新启动应用就能正常使用h2的管理界面了