By Matteo Redaelli

http://www.redaelli.org/matteo/

HOWTO SHARING A DATABASE CONNECTION IN SMX4 OSGI CONTAINER

If you want to share in smx4/osgi a database connection you should

1) Check and eventually install the apache common-dbcp bundle in the servicemix console

osgi/install -s mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.commons-dbcp/1.2.2_3

You may probably need also

osgi/install -s wrap:mvn:org.springframework/spring-jdbc/2.5.6/
osgi/install -s wrap:mvn:mysql/mysql-connector-java/5.1.6

2) Expose a database connection in OSGI

Create a file "moodleDS.xml" and copy in "deploy" folder of servicemix/fuseesb4

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel-osgi="http://activemq.apache.org/camel/schema/osgi"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://activemq.apache.org/camel/schema/spring
http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
http://activemq.apache.org/camel/schema/osgi
http://activemq.apache.org/camel/schema/osgi/camel-osgi.xsd">

    <bean id="moodleDS"
                class="org.apache.commons.dbcp.BasicDataSource">
                <property name="driverClassName" value="com.mysql.jdbc.Driver" />
                <property name="url"
                        value="jdbc:mysql://localhost:3306/moodle" />
                <property name="username" value="root" />
                <property name="password" value="" />
    </bean>

<osgi:service id="moodleDSOsgiService" ref="moodleDS" interface="javax.sql.DataSource" />

</beans>


3) Use a database connection in OSGI

For instance in a CXF OSGI project the file beans.xml shoul be

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
        xmlns:osgi="http://www.springframework.org/schema/osgi"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
        http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-http.xml" />
    <import resource="classpath:META-INF/cxf/osgi/cxf-extension-osgi.xml" />

<osgi:reference id="moodleDS" interface="javax.sql.DataSource" bean-name="moodleDS" />

        <bean id="person" class="org.apache.servicemix.samples.wsdl_first.PersonImpl">
                <property name="dataSource" ref="moodleDS" />
         </bean>

    <jaxws:endpoint id="HTTPEndpoint"
        implementor="#person"
        address="/PersonService"
        wsdlLocation="wsdl/person.wsdl"
        endpointName="e:soap"
        serviceName="s:PersonService"
        xmlns:e="http://servicemix.apache.org/samples/wsdl-first"
            xmlns:s="http://servicemix.apache.org/samples/wsdl-first">
    </jaxws:endpoint>

</beans>