我正在开发一个需要在大型机上调用DB2函数以获取id的应用程序。
在spring应用程序上下文中,我定义了jdbc模板来查询zOS上的DB2:
<bean id="jdbcTemplateDB2"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourceDB2"/>
</bean>
然后,我将数据源定义如下:
<bean id="dataSourceDB2"
class="com.ibm.db2.jcc.DB2DataSource">
<property name="serverName" value="hostname" />
<property name="portNumber" value="portno"/>
<property name="databaseName" value="dbname"/>
<property name="driverType" value="4"/>
<property name="user" value="userid"/>
<property name="password" value="password"/>
</bean>
以上工作。然而,看看ibm的db2jcc内部。jar文件中,我看到了一个用于连接池的datasource类-com。ibm。db2.jcc。DB2ConnectionPoolDataSource。所以我试着用它来代替上面的一个,就像这样:
<bean id="dataSourceDB2"
class="com.ibm.db2.jcc.DB2ConnectionPoolDataSource">
<property name="serverName" value="hostname" />
<property name="portNumber" value="portno"/>
<property name="databaseName" value="dbname"/>
<property name="driverType" value="4"/>
<property name="user" value="userid"/>
<property name="password" value="password"/>
</bean>
但是,使用DB2ConnectionPoolDataSource会给我带来以下错误。
Cannot convert value of type [com.ibm.db2.jcc.DB2ConnectionPoolDataSource] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
完整的stackTrace在下面
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'classUniqueIdDaoImpl_v2': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTemplateDB2' defined in class path resource [META-INF/spring/applicationContext-db2.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.ibm.db2.jcc.DB2ConnectionPoolDataSource' to required type 'javax.sql.DataSource' for property 'dataSource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.ibm.db2.jcc.DB2ConnectionPoolDataSource] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:307)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.slma.euclid.core.dao.MainDB2.main(MainDB2.java:18)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTemplateDB2' defined in class path resource [META-INF/spring/applicationContext-db2.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.ibm.db2.jcc.DB2ConnectionPoolDataSource' to required type 'javax.sql.DataSource' for property 'dataSource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.ibm.db2.jcc.DB2ConnectionPoolDataSource] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:876)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:439)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:417)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:559)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:304)
... 13 more
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.ibm.db2.jcc.DB2ConnectionPoolDataSource' to required type 'javax.sql.DataSource' for property 'dataSource'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.ibm.db2.jcc.DB2ConnectionPoolDataSource] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:485)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:516)
at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1406)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1365)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
... 27 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.ibm.db2.jcc.DB2ConnectionPoolDataSource] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:241)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:470)
... 33 more
我做错了什么?在实例化Spring JdbcTemplate时,使用连接池数据源的正确方法是什么?
非常感谢您的帮助。
-新加坡银行
使用Spring和DB2ConnectionPoolDataSource的基于注释的方法:
应用属性
spring.datasource.url=jdbc:db2://DBHOST:50000/DB:currentSchema=MYSCHEMA;currentFunctionPath=MYSCHEMA;\
clientProgramName=APPNAME-MYSCHEMA;\
progressiveStreaming=2;retrieveMessagesFromServerOnGetMessage=true;fullyMaterializeLobData=true;\
clientApplcompat=V12R1M500;jdbcCollection=NULLIDV12R1M500;currentPackageSet=NULLIDV12R1M500;
spring.datasource.username=USER
spring.datasource.password=PWD
应用程序配置。Java语言
@Configuration
@PropertySource("classpath:application.properties")
public class ApplicationConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationConfig.class);
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Bean
public DB2ConnectionPoolDataSource dataSource() {
LOGGER.info("DATASOURCE: " + url);
DB2ConnectionPoolDataSource ds = new DB2ConnectionPoolDataSource();
ds.setDriverType(4);
Properties hostPortDb = splitUrlHostPortDb(url);
ds.setServerName(hostPortDb.getProperty("server"));
ds.setPortNumber(Integer.parseInt(hostPortDb.getProperty("port")));
ds.setDatabaseName(hostPortDb.getProperty("db"));
Properties paramValuePairs = splitUrlParamValuePairs(url);
setParamValuePairs(ds, paramValuePairs);
ds.setUser(username);
ds.setPassword(password);
return ds;
}
private Properties splitUrlHostPortDb(String url) {
Properties properties = new Properties();
int hostIndex = url.indexOf("//");
int lastColonIndex = url.lastIndexOf(":");
String hostPortDb = url.substring(hostIndex, lastColonIndex);
URI uri = URI.create(hostPortDb);
properties.put("server", uri.getHost());
properties.put("port", uri.getPort() + "");
properties.put("db", uri.getPath().substring(1));
return properties;
}
private Properties splitUrlParamValuePairs(String url) {
Properties properties = new Properties();
int lastColonIndex = url.lastIndexOf(":");
String paramValues = url.substring(lastColonIndex + 1);
paramValues = paramValues.replaceAll(";", "\n");
try {
properties.load(new StringReader(paramValues));
} catch (IOException ex) {
String msg = "DATASOURCE: " + paramValues;
LOGGER.error(msg, ex);
throw new RuntimeException(msg, ex);
}
return properties;
}
private void setParamValuePairs(DB2ConnectionPoolDataSource ds, Properties properties) {
for (Object key : properties.keySet()) {
String value = (String) properties.get(key);
setter(ds, (String) key, value);
}
}
private void setter(DB2ConnectionPoolDataSource ds, String key, String value) {
boolean invoked = false;
String setterName = "set" + StringUtils.capitalize(key);
try {
Method[] methods = DB2ConnectionPoolDataSource.class.getMethods();
for (Method method : methods) {
if (method.getName().equals(setterName)) {
method.setAccessible(true);
if (method.getParameterTypes()[0] == String.class) {
method.invoke(ds, value);
invoked = true;
} else if (method.getParameterTypes()[0] == short.class) {
method.invoke(ds, Short.parseShort(value));
invoked = true;
} else if (method.getParameterTypes()[0] == int.class) {
method.invoke(ds, Integer.parseInt(value));
invoked = true;
} else if (method.getParameterTypes()[0] == long.class) {
method.invoke(ds, Long.parseLong(value));
invoked = true;
} else if (method.getParameterTypes()[0] == boolean.class) {
method.invoke(ds, Boolean.parseBoolean(value));
invoked = true;
}
LOGGER.info("DATASOURCE: " + method.getParameterTypes()[0] + " " + method + "=" + value);
}
}
} catch (Exception ex) {
String msg = "DATASOURCE: Assignment " + setterName + "=" + value + " failure - check type of argument.";
LOGGER.error(msg, ex);
throw new RuntimeException(msg, ex);
}
if (!invoked) {
String msg = "DATASOURCE: Method " + setterName + " not found - typo in DB URL.";
LOGGER.error(msg);
throw new RuntimeException(msg);
}
}
}
在Springbean中使用数据源:
@Autowired
private DB2ConnectionPoolDataSource dataSource;
以及:
connection = dataSource.getPooledConnection().getConnection();
你读过这个吗:http://forum.springsource.org/showthread.php?66142-How-to-use-javax-sql-datasource-with-a-ConnectionPoolDataSource
上面写着com。ibm。db2.jcc。如果使用正确,DB2DataSource已经是连接池。
我正在用vaadin和spring开发一个Web应用程序java,比如容器和eclipse链接,比如持久化框架。现在我想在我的应用程序中使用一个连接池。我在谷歌上读了一些我还没读到的东西。这是我的应用程序配置: 我的春日文脉 我的persistence.xml 但我得到以下异常:
我使用的是用Spring配置的mybatis。一切都很好,但是在这种配置下没有连接池。每次我执行一些SQL语句,它都会创建一个新的连接。有什么简单的方法可以用mybatis+Spring配置连接池吗?
我正在使用Node js和Postgresql,并试图在连接实现中达到最高效率。< br >我看到pg-promise构建在node-postgres之上,node-postgres使用pg-pool来管理池。< br >我还读到“一次超过100个客户端是一件非常糟糕的事情”(node-postgres)。 我正在使用pg-promise,想知道: 对于非常大的数据负载,推荐的poolSize是什
问题内容: 如何使用HttpClient创建连接池? 我必须经常连接同一台服务器。值得建立这样一个游泳池吗? 是否可以保持实时连接并将其用于各种请求,如果可以,我该如何做? 我正在使用Apache HTTP Client在Java中进行开发。 问题答案: [假设Java和Apache的HttpClient] 使用ThreadSafeClientConnManager。将单个全局实例传递给每个Htt
问题内容: 为什么在hibernate配置文件中为连接池设置10?size = 1是内置的size。为什么需要增加大小? 问题答案: 来自Hibernate API 文档。 但是,Hibernate自己的连接池算法非常初级。它旨在帮助您入门,不适合在生产系统中使用,甚至不用于性能测试。您应该使用第三方池来获得最佳性能和稳定性。只需将hibernate.connection.pool_size属性替
问题内容: 与 那两个工作正常。 使用连接池背后的想法是什么?什么时候使用? 问题答案: 从redis-py docs: 在后台,redis- py使用连接池来管理与Redis服务器的连接。默认情况下,您创建的每个Redis实例将依次创建自己的连接池。通过将已创建的连接池实例传递给Redis类的connection_pool参数,可以覆盖此行为并使用现有的连接池。您可以选择执行此操作,以实现客户端