HikariCPをSpringのJdbcTemplateと統合するときにエラーが発生します。 Spring 3.2.2、HikariCP 2.3.8、およびsybasejconn4バージョン7.0.0を使用しています。
春の構成:
<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>
コンテキストが読み込まれるたびにこのエラーがスローされ、データソースを作成できません
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$1.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
興味深いのは、単純なコードで実行すると、これが完全に機能することです。
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);
}
}
奇妙な部分は、コメントを外すとdsスプリングでロードする)失敗することです。
これに一日を費やしました。どんな提案もありがたいです
dataSourceClassName
プロパティとjdbcUrl
プロパティは相互に排他的です( ドキュメントを参照 )。 2つのオプションがあります...
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>
-または-
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>
ドライバが自己登録しない場合は、オプション#2にdriverClassName
プロパティを追加する必要がある場合があります。
更新:チェコ語を読むことができる場合は、ここで同じエラーについて読むことができます http://blog.prodejna.biz/2013/03/glassfish-jms-broker-perzistujici.html 。基本的に、SyDataSource
を使用せず、jdbcUrl
ベースのアプローチのみを使用します(必要に応じてdriverClassName
も使用できます)。
私の場合、構成ファイルにプロファイルを追加し、それを指定せずに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);
}
}