【发布时间】:2011-10-15 03:20:17
【问题描述】:
我有一个应用程序在连接数据库时需要使用连接池。问题是,该应用程序旨在动态配置和更改连接设置。我编写了以下代码,允许我动态创建数据源,然后使用它打开与数据库的连接。但是,这段代码是为 DataDirect 驱动程序编写的,我用它来连接 Oracle 和 MS SQL。不幸的是,DataDirect Mysql 驱动程序只能连接到商业版的 mysql,而不是免费版。所以现在我正在尝试使用 mysql 中的 connect/j 完成相同的任务。我找不到如何通过像 DD 驱动程序这样的方法调用来设置在外部(静态)数据源定义中找到的连接属性。任何帮助将不胜感激。
BaseDataSource bds = (BaseDataSource)ds;
// Populate the DataSource
bds.setDescription("Driver Data Source");
bds.setServerName(connectUrl);
bds.setUser(userName);
bds.setPassword(password);
if ( spyAttr.length() > 0 ) bds.setSpyAttributes(spyAttr);
// Create the PooledConnection DataSource. Pass the data source created above
// to the PooledConnection DataSource
pds = new PooledConnectionDataSource();
pds.setDescription("Pooled Data Source");
pds.setDataSourceName("myDataSource", bds);
pds.setPoolName("myPool");
pds.setInitialPoolSize(nPoolSize);
pds.setMinPoolSize(minPoolSize);
pds.setMaxPoolSize(maxPoolSize);
pds.setPropertyCycle(propCycle);
pds.setMaxIdleTime(maxIdleTime);
pds.setTracing(tracing);
// Get connection
pds.getConnection();
【问题讨论】:
标签: java mysql jdbc connection-pooling