【问题标题】:Need to dynamically create a data source for MySQL Connect/J connection pool需要为 MySQL Connect/J 连接池动态创建数据源
【发布时间】: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


    【解决方案1】:

    显然 MySQL Connector/J 驱动程序不允许将这些参数与方法调用一起传递,因此您必须将它们作为 URL 中的参数传递 (jdbc:mysql://localhost/test?user=foo&password =bar)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      • 2011-02-19
      • 2010-12-14
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多