Started working on an application development with Apache Karaf as run time 
recently. Have a problem with respect to accessing a JDBC data source deployed 
as OSGi service from another OSGi service, both using blueprint containers. I 
am following the tutorial 
https://github.com/cschneider/Karaf-Tutorial/tree/master/db/examplejdbc using 
apache karaf 3.0.0 and postgres 9.1 as database

Created the data source as below and deployed in deploy folder.

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0";>

   <bean id="dataSource" class="org.postgresql.ds.PGPoolingDataSource" 
destroy-method="close">
      <property name="serverName" value="localhost:5432/masterdb"/>
      <property name="user" value="userid"/>
      <property name="password" value="password"/>
      <property name="dataSourceName" value=" masterdb "/>
      <property name="initialConnections" value="2"/>
      <property name="maxConnections" value="4" />
  </bean>

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

When I checked the JNDI names using the JNDI enterprise services its showing as 
below.  I am able to connect and query the database using jdbc enterprise 
features using the data source created.

karaf@root()> jndi:names
JNDI Name                    | Class Name
-----------------------------------------------------------------------------
osgi:servicejdbc/masterDb | org.postgresql.ds.PGPoolingDataSource
osgi:service/jndi            | org.apache.karaf.jndi.internal.JndiServiceImpl

karaf@root()> jdbc:datasources
Name             | Product    | Version | URL                                   
                                                                      
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
jdbc/masterDb| PostgreSQL | 9.1.1   | 
jdbc:postgresql://localhost:5432/masterDb/null?loginTimeout=0&socketTimeout=0&prepareThreshold=5&unknownLength=2147483647&loglevel=0&tcpkeepalive=false

I am trying use this data source from another service which is invoked from an 
Apache Camel Route.  Code is as below

public class SvcMain implements ProcessingUnit {
        static Logger LOG = LoggerFactory.getLogger(SvcMain.class);
        private DataSource masterDb;

        public void setMasterDb(DataSource masterDb) {
                this.masterDb = masterDb;
                if(this.masterDb == null) {
                        LOG.info("masterDb is null.");
                }
        }

        public void process(DataSet dataset) throws ProcessingUnitException {
                LOG.info(dataset.getRecords().toString());
                try {
                        if(this.masterDb != null) {
                                Connection c = this.masterDb.getConnection();
                                DatabaseMetaData dbMeta = c.getMetaData();
                                LOG.info("Connected using datasource " + 
dbMeta.getDatabaseProductName() + ", URL " + dbMeta.getURL());
                        }
                        else {
                                LOG.info("Data source masterDb is null.");
                        }
                } catch (SQLException e) {
                        LOG.info("Error getting database connection, 
"+e.getMessage());
                }
        }
}

Blueprint descriptor for the service is as below.

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"; 
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0";>
        <reference id="masterDb" interface="javax.sql.DataSource" 
filter="(osgi.jndi.service.name=jdbc/masterDb)"/>
        <bean id="SvcImpl" class="com.foo.SvcMain">
                <property name="masterDb" ref="masterDb"/>
        </bean>
        <service id="Svc" interface="com.foo.ProcessingUnit" ref="SvcImpl">
                <service-properties>
                        <entry key="pu.svc.name" value="Svc" />
                        <entry key="pu.svc.version" value="1.0.0" />
                </service-properties>
        </service>
</blueprint>


In the service implementation, data source is getting injected properly. That 
is in the setMasterDb method masterDb is not null which is called when the 
service is getting deployed. But when it comes to process method which called 
from camel route,  masterDb is null. Anything else I need to do other than the 
above mentioned ?

regards
Vinu

The information contained in this electronic message and any attachments to 
this message are intended for the exclusive use of the addressee(s) and may 
contain proprietary, confidential or privileged information. If you are not the 
intended recipient, you should not disseminate, distribute or copy this e-mail. 
Please notify the sender immediately and destroy all copies of this message and 
any attachments.

WARNING: Computer viruses can be transmitted via email. The recipient should 
check this email and any attachments for the presence of viruses. The company 
accepts no liability for any damage caused by any virus transmitted by this 
email.

www.wipro.com

Reply via email to