java.net.UnknownHostException:Test:Test:unknown error无法获取VMID的本地InetAddress

时间:2014-07-07 09:35:14

标签: java spring spring-mvc jersey c3p0

我在spring-jersey1.8 restful web服务中使用c3p0进行连接池。在启动应用程序时,我收到以下错误:

[com.mchange.v2.c3p0.impl.C3P0ImplUtils] INFO - Failed to get local InetAddress for VMID. This is unlikely to matter. At all. We'll add some extra randomness
java.net.UnknownHostException: Test: Test: unknown error
    at java.net.InetAddress.getLocalHost(InetAddress.java:1484)
    at com.mchange.v2.c3p0.impl.C3P0ImplUtils.generateVmId(C3P0ImplUtils.java:120)
    at com.mchange.v2.c3p0.impl.C3P0ImplUtils.<clinit>(C3P0ImplUtils.java:98)
    at com.mchange.v2.c3p0.impl.PoolBackedDataSourceBase.<init>(PoolBackedDataSourceBase.java:227)
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.<init>(AbstractPoolBackedDataSource.java:62)
    at com.mchange.v2.c3p0.ComboPooledDataSource.<init>(ComboPooledDataSource.java:109)
    at com.mchange.v2.c3p0.ComboPooledDataSource.<init>(ComboPooledDataSource.java:105)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1069)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1021)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.UnknownHostException: Test: unknown error
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:907)
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1302)
    at java.net.InetAddress.getLocalHost(InetAddress.java:1479)
    ... 35 more

有没有人知道这个错误是什么?

我的spring-datasource.xml文件有:

<!-- <context:property-placeholder location="/WEB-INF/spring.properties"></context:property-placeholder> -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close"
    p:driverClass="com.mysql.jdbc.Driver"
    p:jdbcUrl="jdbc:mysql://localhost:3306/test2" 
    p:user="root" 
    p:password="root"
    p:acquireIncrement="5" 
    p:idleConnectionTestPeriod="60" 
    p:maxPoolSize="100"
    p:maxStatements="50" 
    p:minPoolSize="10" />

和pom.xml:

<dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.1.2</version>
</dependency>

2 个答案:

答案 0 :(得分:7)

正如another帖子中所述(在Steve Waldman的评论中链接),解决方案是添加缺少的条目

java.net.UnknownHostException: Test: Test: unknown error

在这种情况下,/etc/hosts

中的测试
127.0.0.1    Test

答案 1 :(得分:6)

因此,为了防止在不太可能发生的事件中将许多c3p0数据源序列化到分布式存储中的过度工程,c3p0尝试为每个DataSource提供全局唯一的#34; identityToken&#34;,这是在部分来自前缀一个&#34; VMID&#34;,意图(可能不足为奇)成为当前JVM的唯一标识符。

VMID部分由运行它的主机的Internet地址决定。所以c3p0调用java.net.InetAddress.getLocalHost()来查找。但是,对java.net.InetAddress.getLocalHost()的调用可能会异常失败,可能是出于安全原因或某些配置错误。

所有这些都是可怕的,可怕的矫枉过正。写完之后,我很害羞,但是我在理论上认为identityTokens可能会发生冲突,违反了一个identityToken应该映射到一个唯一池的不变量的可能性。那就是它。

因此,我们无法找到本地地址。哎呀。我们回过头来为我们将为您正在运行的VM生成的VMID添加一堆随机性,以减少在实践中已经无限的冲突可能性。然后我们继续前进。正如错误消息所示,&#34;这不太重要。完全没有。&#34;

如果您想让错误消失,您需要解决导致您的环境中getLocalHost()调用失败的问题。但你真的可以忽略这一点。

(看起来你的环境是IPv6,这可能与它有关。我应该研究一下这个代码是否适合在VMID中包含IPv6地址。但是你的代码还远远不够为此重要。)