Configuring database connection in Jboss FUSE

2019-04-17 05:23发布

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?

2条回答
Rolldiameter
2楼-- · 2019-04-17 05:39

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.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-04-17 05:40

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)

查看更多
登录 后发表回答