Configuring database connection in Jboss FUSE

白昼怎懂夜的黑 提交于 2019-12-04 21:47:11

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.

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)

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