Java 使用 com.zaxxer.hikari.HikariDataSource 时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31026545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error using com.zaxxer.hikari.HikariDataSource
提问by Amey Jadiye
I'm facing an error integrating HikariCP with Spring's JdbcTemplate. I'm using Spring 3.2.2, HikariCP 2.3.8 and sybase jconn4 version 7.0.0
我在将 HikariCP 与 Spring 的 JdbcTemplate 集成时遇到错误。我使用的是 Spring 3.2.2、HikariCP 2.3.8 和 sybase jconn4 版本 7.0.0
Spring configuration :
弹簧配置:
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="dataSourceClassName" value="com.sybase.jdbc4.jdbc.SybDataSource" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="maximumPoolSize" value="100" />
<property name="idleTimeout" value="60000" />
<property name="jdbcUrl"
value="jdbc:sybase:Tds:${hostname}:${port}/${dbname}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<bean id="ds" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="ds" />
</property>
</bean>
Every time the context is loading it throws this error and the datasource can't be created
每次加载上下文时都会抛出此错误并且无法创建数据源
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ds' defined in class path resource [commons/config/datasourceTest.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.zaxxer.hikari.HikariDataSource]: Constructor threw exception; nested exception is com.zaxxer.hikari.pool.PoolInitializationException: Exception during pool initialization
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1051)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:955)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:120)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
... 25 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.zaxxer.hikari.HikariDataSource]: Constructor threw exception; nested exception is com.zaxxer.hikari.pool.PoolInitializationException: Exception during pool initialization
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:121)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
... 42 more
Caused by: com.zaxxer.hikari.pool.PoolInitializationException: Exception during pool initialization
at com.zaxxer.hikari.pool.BaseHikariPool.initializeConnections(BaseHikariPool.java:544)
at com.zaxxer.hikari.pool.BaseHikariPool.<init>(BaseHikariPool.java:171)
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:60)
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:48)
at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:80)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
... 44 more
Caused by: java.sql.SQLException: JZ0PN: Specified port number of -1 was out of range. Port numbers must meet the following conditions: 0 <= portNumber <= 65535
at com.sybase.jdbc4.jdbc.SybConnection.getAllExceptions(Unknown Source)
at com.sybase.jdbc4.jdbc.SybConnection.handleSQLE(Unknown Source)
at com.sybase.jdbc4.jdbc.SybConnection.a(Unknown Source)
at com.sybase.jdbc4.jdbc.SybConnection.handleHAFailover(Unknown Source)
at com.sybase.jdbc4.jdbc.SybConnection.<init>(Unknown Source)
at com.sybase.jdbc4.jdbc.SybDriver.createConnection(Unknown Source)
at com.sybase.jdbc4.jdbc.SybDriver.connect(Unknown Source)
at com.sybase.jdbc4.jdbc.SybDriver.connect(Unknown Source)
at com.sybase.jdbc4.jdbc.SybDataSource.getConnection(Unknown Source)
at com.zaxxer.hikari.pool.BaseHikariPool.addConnection(BaseHikariPool.java:438)
at com.zaxxer.hikari.pool.BaseHikariPool.initializeConnections(BaseHikariPool.java:542)
... 53 more
Now the interesting part is this is working perfectly if i do it with simple code.
现在有趣的部分是,如果我用简单的代码来做的话,这是完美的。
public class JdbcTemplateFactoryTest {
@Autowired
HikariConfig hikariConfig;
//@Autowired
//HikariDataSource ds;
@Test
public void hikaritest(){
HikariDataSource ds = new HikariDataSource(hikariConfig);
JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
assertNotNull(jdbcTemplate);
}
}
Strange part is if i uncomment the dsto load by spring) it fails.
奇怪的部分是,如果我取消注释ds以通过弹簧加载)它会失败。
Spent the whole day on this. Any suggestion is appreciate
花了一整天的时间在这上面。任何建议表示赞赏
采纳答案by brettw
The dataSourceClassName
and jdbcUrl
properties are mutually exclusive (see the doc). You have two options...
的dataSourceClassName
和jdbcUrl
性质是相互排斥的(见该文档)。你有两个选择...
1) Using dataSourceClassName
:
1)使用dataSourceClassName
:
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="dataSourceClassName" value="com.sybase.jdbc4.jdbc.SybDataSource" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="maximumPoolSize" value="100" />
<property name="idleTimeout" value="60000" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="dataSourceProperties">
<props>
<property name="serverName" value="${hostname}" />
<property name="port" value="${port}" />
<property name="databaseName" value="${dbname}" />
</props>
</property>
</bean>
-or-
-或者-
2) Using jdbcUrl
:
2)使用jdbcUrl
:
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="jdbcUrl"
value="jdbc:sybase:Tds:${hostname}:${port}/${dbname}" />
<property name="connectionTestQuery" value="SELECT 1" />
<property name="maximumPoolSize" value="100" />
<property name="idleTimeout" value="60000" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
If your driver does not self-register, you may need to add the driverClassName
property to option #2.
如果您的驱动程序未自行注册,您可能需要将该driverClassName
属性添加到选项 #2。
UPDATE:
If you can read Czech you can read about the same error here http://blog.prodejna.biz/2013/03/glassfish-jms-broker-perzistujici.html. Basically, don't use SyDataSource
, use only the jdbcUrl
-based approach (and possibly driverClassName
if needed).
更新:如果您可以阅读捷克语,您可以在http://blog.prodejna.biz/2013/03/glassfish-jms-broker-perzistujici.html阅读有关相同错误的信息。基本上,不要使用SyDataSource
,只使用jdbcUrl
基于 - 的方法(driverClassName
如果需要的话)。
回答by LottaLava
In my case, I added a profile to my Configuration file and was starting spring without specifying it.
就我而言,我在配置文件中添加了一个配置文件,并且在没有指定的情况下启动了 spring。
@Bean
public DataSource dataSource() throws Exception {
JndiDataSourceLookup dataSourceLookup;
DataSource dataSource;
HikariDataSource hikariDataSource;
HikariConfig hikariConfig;
try {
LOGGER.info("BEGIN");
LOGGER.info("Creating DataSource");
dataSourceLookup = new JndiDataSourceLookup();
dataSourceLookup.setResourceRef(true);
dataSource = dataSourceLookup.getDataSource(CO_PARTICIPACAO_DS);
//hikariConfig = new HikariConfig(additionalProperties());
hikariConfig=new HikariConfig();
hikariConfig.setDataSource(dataSource);
hikariDataSource = new HikariDataSource(hikariConfig);
LOGGER.info("END");
return hikariDataSource;
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
throw new CoParticipacaoException(e);
}
}