Hi Niranda,

What you have given is mostly used when accessing datasources within
webapps. This is where using JNDI lookup will be helpful. But if this is
within an OSGi component that you want to access datasources, then best way
is to use relevant OSGi services as mentioned by Rajith above.

Thanks,
Kishanthan.

On Tue, Sep 27, 2016 at 9:29 AM, Rajith Vitharana <[email protected]> wrote:

> Hi Niranda,
>
> Since we use jndi, IMO this approach is also ok. Other than that, you can
> also get carbon datasource using Ndatasource component as well. Sample will
> be as below
>
> CarbonDataSource cds = dataSourceService.getDataSource("datasourceName");
>
> You will need to get the datasourceService[2] as osgi service.
>
> AFAIR you need to mention datasource name from "name" tag [1]
>
> [1] - <datasource>
>             <name>WSO2_CARBON_DB</name>
> [2] - org.wso2.carbon.ndatasource.core.DataSourceService
>
> Thanks,
>
>
> On 27 September 2016 at 08:03, Niranda Perera <[email protected]> wrote:
>
>> Hi all,
>>
>> I want to use a jdbc connection provided by a carbon-datasource.
>>
>> I found the following blog from Kishanthan [1], which was done in 2013.
>>
>> it uses the org.wso2.carbon.tomcat.jndi.CarbonJavaURLContextFactory as
>> follows
>>
>> Hashtable environment = new Hashtable();
>>  environment.put("java.naming.factory.initialoorg.wso2.carbo
>> n.tomcat.jndi.CarbonJavaURLContextFactory");
>>     Context initContext = new InitialContext(environment);
>>     Object result = initContext.lookup("jdbc/MyCarbonDataSource");
>>     if (result != null) {
>>         // Do your work here
>>     } else {
>>         System.out.println(“Cannot find MyCarbonDataSource”);
>>     }
>>
>> My question is, is there a better way of doing this now (a util method
>> may be?) or is this method still applicable?
>>
>> Best
>>
>> [1] https://kishanthan.wordpress.com/2013/02/11/access-
>> cabon-data-sources-within-webapps-in-wso2-application-server/
>>
>> --
>> *Niranda Perera*
>> Software Engineer, WSO2 Inc.
>> Mobile: +94-71-554-8430
>> Twitter: @n1r44 <https://twitter.com/N1R44>
>> https://pythagoreanscript.wordpress.com/
>>
>> _______________________________________________
>> Dev mailing list
>> [email protected]
>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>
>>
>
>
> --
> Rajith Vitharana
>
> Senior Software Engineer,
> WSO2 Inc. : wso2.com
> Mobile : +94715883223
> Blog : http://lankavitharana.blogspot.com/
> <http://wso2.com/signature>
>



-- 
*Kishanthan Thangarajah*
Technical Lead,
Platform Technologies Team,
WSO2, Inc.
lean.enterprise.middleware

Mobile - +94773426635
Blog - *http://kishanthan.wordpress.com <http://kishanthan.wordpress.com>*
Twitter - *http://twitter.com/kishanthan <http://twitter.com/kishanthan>*
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to