Configuring database connection in Jboss FUSE

只谈情不闲聊 提交于 2019-12-10 00:05:15

问题


One way I know to configure DB in JBOSS FUSE is to use blueprint.xml. Below configuration in blueprint.xml works

<bean id="gemsDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="${gems_url}" />
    <property name="username" value="${gems_username}" />
    <property name="password" value="${gems_password}" />
    <property name="maxIdle" value="5" />
    <property name="minIdle" value="1" />
    <property name="initialSize" value="1" />
</bean>

However, Is there any way to configure it in JBOSS container specific configuration file. For example - In JBOSS EAP we can configure it in standalone.xml. On similar lines can we configure it in JBOSS FUSE?


回答1:


Jboss Fuse provides the integration with various data sources. You need to configure them bundle wise like you have used. But no such configuration is there on container level.




回答2:


You can define a Datasource in a bundle and export it. In other bundles you import and use it like a service.

Prerequisites

Install these features

features:install jdbc
features:install jndi

Datasource bundle

Drop an XML file inside deploy folder with following content:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">

    <bean id="dataSource" class="org.postgresql.jdbc3.Jdbc3SimpleDataSource">
        <property name="url" value="jdbc:postgresql://localhost:5432/databasename"/>
        <property name="user" value="username"/>
        <property name="password" value="xxx"/>
    </bean>

    <service interface="javax.sql.DataSource" ref="dataSource">
        <service-properties>
            <entry key="osgi.jndi.service.name" value="jdbc/yourdatabasename_ds"/>
        </service-properties>
    </service>
</blueprint>

This will export a service with javax.sql.DataSource interface and a JNDI name

Use Datasource service

When a bundle needs the datasource, ask OSGi to inject it.

<blueprint>
    <reference id="yourDatabaseDs"
           interface="javax.sql.DataSource"
           availability="mandatory"
           filter="(osgi.jndi.service.name=jdbc/yourdatabasename_ds)"/>
</blueprint>

The right Datasource is retrieved using the JNDI name you provided.
Using availability="mandatory" you can force your bundle to wait for the Datasource to become available. The bundle won't start without this reference.

You need the correct JDBC drivers for the database you are using.

Other goodies

You have a lot of commands in JBoss Fuse console to interact with the database now, like jdbc:datasources that will list all available datasources. With jdbc:query you can run any SQL against your DB (good for debugging issues and testing the connection)



来源:https://stackoverflow.com/questions/43465689/configuring-database-connection-in-jboss-fuse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!