如何使用HikariCP在jboss配置文件中配置JNDI数据源我在Hikari的帮助内容中找不到任何东西,只有Tomcat配置.
我有一个Spring webb应用程序,我在应用程序中定义了一个数据源,我想将其移动到JNDI数据源.
我的数据源定义是:
<datasource jndi-name="java:jboss/datasources/mydatasource" pool-name="mydatasource" enabled="true" use-java-context="true">
<connection-url>jdbc:postgresql://localhost:5432/database</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<datasource-class>com.zaxxer.hikari.HikariDataSource</datasource-class>
<driver>postgresql</driver>
<pool>
<min-pool-size>5</min-pool-size>
<max-pool-size>10</max-pool-size>
</pool>
<security>
<user-name>user</user-name>
<password>password</password>
</security>
</datasource>
和驱动程序定义:
<driver name="postgresql" module="org.postgresql.jdbc">
<xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
</driver>
我在其他方面遇到这个错误:
ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation (“add”) failed – address: ([
(“subsystem” => “datasources”),
(“data-source” => “mydatasource”)
]) – failure description: {“JBAS014771: Services with missing/unavailable dependencies” => [
“jboss.driver-demander.java:jboss/datasources/mydatasource is missing [jboss.jdbc-driver.postgresql]”,
“jboss.data-source.java:jboss/datasources/mydatasource is missing [jboss.jdbc-driver.postgresql]”
]}
那么配置这个的正确方法是什么?
编辑:
按照创建Tomcat资源的指南并使用此question中提供的信息,我已经了解了这个DataSource定义:
<datasource jta="false" jndi-name="java:jboss/mydatasource" pool-name="mydatasource" enabled="true" use-ccm="false">
<connection-url>jdbc:postgresql://localhost:5432/databasename</connection-url>
<driver-class>org.postgresql.Driver</driver-class>
<driver>postgresql</driver>
<pool>
<min-pool-size>5</min-pool-size>
<max-pool-size>10</max-pool-size>
<flush-strategy>FailingConnectionOnly</flush-strategy>
</pool>
<security>
<user-name>username</user-name>
<password>password</password>
</security>
<validation>
<validate-on-match>false</validate-on-match>
<background-validation>false</background-validation>
</validation>
<statement>
<share-prepared-statements>false</share-prepared-statements>
</statement>
</datasource>
我在Jboss中安装了postgresql驱动程序并声明了它.
并在Spring配置中
...
@Bean
public DataSource dataSource() {
final JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
dataSourceLookup.setResourceRef(true);
DataSource dataSourceTemp = null;
try {
dataSourceTemp = dataSourceLookup.getDataSource("jdbc/mydatasource");
} catch (DataSourceLookupFailureException e) {
log.error("DataSource not found.");
}
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDataSource(dataSourceTemp);
return new HikariDataSource(hikariConfig);
}
...
这段代码基于HikariJNDIFactory代码,一切似乎都有效,但我想我必须用连接属性创建一个属性对象,我必须在对象中包含哪些属性?
解决方法:
以下是“纯”Tomcat JNDI DataSource的配置:
https://github.com/brettwooldridge/HikariCP/wiki/JNDI-DataSource-Factory-(Tomcat,-etc.)
你可能也会找到这个答案Re:Spring Tomcat JNDI资讯:
How to use JNDI DataSource provided by Tomcat in Spring?
还推荐最新的HikariCP 2.0.1.