【问题标题】:Updating URL of DataSource object for JDBC connection更新 JDBC 连接的 DataSource 对象的 URL
【发布时间】:2012-03-27 09:54:21
【问题描述】:

我有一种情况,我在服务器上使用有效的 DataSource 文件(如下所示)连接到本地数据库,它工作正常。我还有许多其他机器具有完全相同的凭据,只是它们的 IP 地址不同(我也有)。我想读入我的 localhost DataSource 对象并使用不同的 ipaddress 更新连接 url。我该怎么做呢?我正在使用 JBoss 4.2.3 进行部署(我无法更改)。

这是我想创建的方法

/**
 * This gets a DataSource for a specified IP address using pacsDS as a template.
 * @param ipaddress
 * @return 
 */
public static DataSource getDataSource(Context context, String ipaddress) throws NamingException {
    DataSource ds = (DataSource)context.lookup("java:/pacsDS");
    // Update ds to make use of supplied ipaddress
    // ...
    return ds;
}

这里是数据源xml文件

<?xml version="1.0" encoding="UTF-8"?>

<!-- ===================================================================== -->
<!--                                                                       -->
<!--  JBoss Server Configuration                                           -->
<!--                                                                       -->
<!-- ===================================================================== -->

<!-- $Id: pacs-postgres-ds.xml 5174 2007-09-26 21:05:41Z gunterze $ -->
<!-- ==================================================================== -->
<!--  Datasource config for Postgres                                      -->
<!-- ==================================================================== -->


<datasources>
    <local-tx-datasource>
        <jndi-name>pacsDS</jndi-name>
        <connection-url>jdbc:postgresql://localhost/pacsdb</connection-url>
        <driver-class>org.postgresql.Driver</driver-class>
        <user-name>postgres</user-name>
        <password></password>
        <!-- sql to call when connection is created.  Can be anything, select 1 is valid for PostgreSQL
        <new-connection-sql>select 1</new-connection-sql>
        -->

        <!-- sql to call on an existing pooled connection when it is obtained from pool.  Can be anything, select 1 is valid for PostgreSQL
        <check-valid-connection-sql>select 1</check-valid-connection-sql>
        -->

        <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
        <metadata>
            <type-mapping>PostgreSQL 7.2</type-mapping>
        </metadata>
    </local-tx-datasource>
</datasources>

【问题讨论】:

    标签: java jdbc jboss datasource


    【解决方案1】:

    您可以参数化您的 JBoss 配置文件。

    https://community.jboss.org/wiki/SystemPropertiesInConfigFiles?_sscc=t

    【讨论】:

    • 您能举个例子说明如何在代码中更新这些变量之一吗?我知道如何在磁盘上对其进行硬编码,但我必须在运行时更改它。
    • 我尝试更改 xml 文件,使其显示为 jdbc:postgresql://${connection.url}/pacsdb 然后在代码中添加该行System.setProperty("connection.url", "localhost");在我查找数据源之前,但这似乎不起作用。
    • 数据源在启动时加载。您需要在命令行上设置该属性。例如-Dconnection.url=
    • 好的,这对我不起作用,因为我需要在运行时修改连接 url。你知道怎么做吗?
    • 我建议您直接使用 JDBC 获取连接。 docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html
    猜你喜欢
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 2017-09-03
    • 2012-06-13
    • 2011-02-24
    • 1970-01-01
    • 2020-02-18
    • 2014-09-30
    相关资源
    最近更新 更多