在两个 JBoss 应用程序中为同一数据源配置不同的物理数据库

发布于 2024-12-06 19:52:27 字数 236 浏览 3 评论 0原文

我想在 JBoss 6.0 服务器实例中部署相同的 Java EE 企业应用程序两次(使用两个不同的名称)。 这两个应用程序都使用服务器中配置的相同逻辑数据源。 到目前为止没有问题。

然而,我想通过这个唯一的逻辑数据源将两个应用程序中的每一个定位到不同的物理数据库。

JBoss 6.0 是否可以以某种方式实现这一点(无需使用两个不同的服务器实例或在部署之前更改应用程序或在每个应用程序中打包不同的 *ds.xml 文件?)

I want to deploy the same Java EE enterprise application twice (under two different names)in a JBoss 6.0 server instance.
Both of these applications use the same logical datasource as configured in the server.
No problem so far.

Yet, I'd like to target each of the two applications a different physical database through this one and only logical datasource.

Is that possible with JBoss 6.0 somehow (without using two different server instances or changing the application before deployment or packaging different *ds.xml files in each application?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦过后 2024-12-13 19:52:27

(不使用两个不同的服务器实例或更改
应用程序在部署或打包不同的 *ds.xml 文件之前
每个应用程序?)

同一个 *-ds.xml 文件下有两个不同的数据源:

<local-tx-datasource>
    <jndi-name>DS1</jndi-name>
    <connection-url>jdbc:mysql://db.server/db?</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>username</user-name>
    <password>pass</password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<metadata>
    <type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
<local-tx-datasource>
    <jndi-name>DS2</jndi-name>
    <connection-url>jdbc:mysql://com.db/db2?</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>username</user-name>
    <password>pass</password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<metadata>
   <type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>

唯一的缺点是,如果您使用 @Resource 注释,则必须自定义代码:

@Resource(mappedName="DS2")
private DataSource ds;

尚未尝试以下操作,但您也许可以通过InitialContext 进行显式JNDI 查找,并指定要在属性文件(DS1 / DS2) 中查找的资源?如果您使用 Maven(我现在做了多个假设:))您可以 过滤属性文件并在构建时通过配置文件提供正确的数据库数据源名称。

(without using two different server instances or changing the
application before deployment or packaging different *ds.xml files in
each application?)

Two different data sources under the same *-ds.xml file:

<local-tx-datasource>
    <jndi-name>DS1</jndi-name>
    <connection-url>jdbc:mysql://db.server/db?</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>username</user-name>
    <password>pass</password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<metadata>
    <type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
<local-tx-datasource>
    <jndi-name>DS2</jndi-name>
    <connection-url>jdbc:mysql://com.db/db2?</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>username</user-name>
    <password>pass</password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<metadata>
   <type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>

The only downside is that you have to customize your code if you are using the @Resource annotation:

@Resource(mappedName="DS2")
private DataSource ds;

Haven't tried the following, but you might be able to do a explicit JNDI lookup via InitialContext, and specify the resource to be looked up in a properties file (DS1 / DS2)? If you are using Maven (I am making multiple assumptions now :)) you could filter the properties file and provide the right DB datasource name via profiles during build time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文