[prev in list] [next in list] [prev in thread] [next in thread] 

List:       esb-java-dev
Subject:    Re: [Dev] How to get a connection from the carbon datasources
From:       Kishanthan Thangarajah <kishanthan () wso2 ! com>
Date:       2016-09-28 17:17:47
Message-ID: CABK5Voqa5E3=66BGF4fSutA6UvfkuyrZW3+uvQERR94Y79X0rg () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


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 <rajithv@wso2.com> 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 <niranda@wso2.com> 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
>> Dev@wso2.org
>> 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>*

[Attachment #5 (text/html)]

<div dir="ltr">Hi Niranda,<div><br></div><div>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.</div><div><br></div><div>Thanks,</div><div>Kishanthan.  </div></div><div \
class="gmail_extra"><br><div class="gmail_quote">On Tue, Sep 27, 2016 at 9:29 AM, \
Rajith Vitharana <span dir="ltr">&lt;<a href="mailto:rajithv@wso2.com" \
target="_blank">rajithv@wso2.com</a>&gt;</span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><div dir="ltr">Hi Niranda,<div><br></div><div>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</div><div><br></div><div>CarbonDataSource cds =  \
dataSourceService.<wbr>getDataSource(&quot;datasourceName&quot;<wbr>);<br></div><div><br></div><div>You \
will need to get the datasourceService[2] as osgi \
service.</div><div><br></div><div>AFAIR you need to mention datasource name from \
&quot;name&quot; tag [1]  </div><div><br></div><div>[1] - \
&lt;datasource&gt;</div><div>                  \
&lt;name&gt;WSO2_CARBON_DB&lt;/name&gt;</div><div>[2] -  \
org.wso2.carbon.ndatasource.<wbr>core.DataSourceService</div><div><br></div><div>Thanks,</div><div><br></div></div><div \
class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On 27 September \
2016 at 08:03, Niranda Perera <span dir="ltr">&lt;<a href="mailto:niranda@wso2.com" \
target="_blank">niranda@wso2.com</a>&gt;</span> wrote:<br></div></div><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><div><div class="h5"><div dir="ltr"><div \
class="gmail_default" style="font-size:small">Hi all,  </div><div \
class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" \
style="font-size:small">I want to use a jdbc connection provided by a \
carbon-datasource.  </div><div class="gmail_default" \
style="font-size:small"><br></div><div class="gmail_default" \
style="font-size:small">I found the following blog from Kishanthan [1], which was \
done in 2013.  </div><div class="gmail_default" \
style="font-size:small"><br></div><div class="gmail_default" \
style="font-size:small">it uses the  \
org.wso2.carbon.tomcat.jnd<wbr>i.CarbonJavaURLContextFactory as follows</div><div \
class="gmail_default" style="font-size:small"><div \
class="gmail_default"><br></div><div class="gmail_default"><font face="monospace, \
monospace">Hashtable environment = new Hashtable();</font></div><div \
class="gmail_default"><font face="monospace, monospace">  \
environment.put(&quot;java.naming.<wbr>factory.initialoorg.wso2.carbo<wbr>n.tomcat.jndi.CarbonJavaURLCon<wbr>textFactory&quot;);</font></div><div \
class="gmail_default"><font face="monospace, monospace">      Context initContext = \
new InitialContext(environment);</font></div><div class="gmail_default"><font \
face="monospace, monospace">      Object result = \
initContext.lookup(&quot;jdbc/MyCar<wbr>bonDataSource&quot;);</font></div><div \
class="gmail_default"><font face="monospace, monospace">      if (result != null) \
{</font></div><div class="gmail_default"><font face="monospace, monospace">           \
// Do your work here</font></div><div class="gmail_default"><font face="monospace, \
monospace">      } else {</font></div><div class="gmail_default"><font \
face="monospace, monospace">            System.out.println("Cannot find \
MyCarbonDataSource");</font></div><div class="gmail_default"><font face="monospace, \
monospace">      }</font></div></div><div class="gmail_default" \
style="font-size:small"><br></div><div class="gmail_default" \
style="font-size:small">My question is, is there a better way of doing this now (a \
util method may be?) or is this method still applicable?  </div><div \
class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" \
style="font-size:small">Best  </div><div class="gmail_default" \
style="font-size:small"><br></div><div class="gmail_default" \
style="font-size:small">[1]  <a \
href="https://kishanthan.wordpress.com/2013/02/11/access-cabon-data-sources-within-webapps-in-wso2-application-server/" \
target="_blank">https://kishanthan.wordpre<wbr>ss.com/2013/02/11/access-<wbr>cabon-dat \
a-sources-within-<wbr>webapps-in-wso2-application-<wbr>server/</a></div><span><font \
color="#888888"><div><br></div>-- <br><div><div dir="ltr"><div><div \
dir="ltr"><div><div><div><span style="background-color:rgb(255,255,255)"><font \
color="#000000"><b>Niranda Perera</b><br></font></span></div><span \
style="background-color:rgb(255,255,255)"><font color="#000000">Software Engineer, \
WSO2 Inc.<br></font></span></div><span \
style="background-color:rgb(255,255,255)"><font color="#000000">Mobile:  <a \
href="tel:%2B94-71-554-8430" value="+94715548430" \
target="_blank">+94-71-554-8430</a><br></font></span></div><font \
style="background-color:rgb(255,255,255)" color="#000000">Twitter: <a \
href="https://twitter.com/N1R44" target="_blank">@n1r44</a></font></div><div \
dir="ltr"><span style="background-color:rgb(255,255,255)"><font color="#000000"><a \
href="https://pythagoreanscript.wordpress.com/" \
target="_blank">https://pythagoreanscript.word<wbr>press.com/</a></font></span><br></div></div></div></div>
 </font></span></div>
<br></div></div>______________________________<wbr>_________________<br>
Dev mailing list<br>
<a href="mailto:Dev@wso2.org" target="_blank">Dev@wso2.org</a><br>
<a href="http://wso2.org/cgi-bin/mailman/listinfo/dev" rel="noreferrer" \
target="_blank">http://wso2.org/cgi-bin/mailma<wbr>n/listinfo/dev</a><br> \
<br></blockquote></div><span class="HOEnZb"><font color="#888888"><br><br \
clear="all"><div><br></div>-- <br><div data-smartmail="gmail_signature"><div \
dir="ltr"><div><div dir="ltr"><div><span style="color:rgb(34,34,34)">Rajith \
Vitharana</span><br style="color:rgb(34,34,34)"><br style="color:rgb(34,34,34)"><span \
style="color:rgb(34,34,34)">Senior Software Engineer,</span><br \
style="color:rgb(34,34,34)"><span style="color:rgb(34,34,34)">WSO2 Inc. :  </span><a \
href="http://wso2.com/" style="color:rgb(136,136,136)" \
target="_blank">wso2.com</a><br style="color:rgb(34,34,34)"><span \
style="color:rgb(34,34,34)">Mobile :  </span><a title="Call Now" \
style="color:rgb(136,136,136)">+94715883223</a><br></div><div \
style="color:rgb(136,136,136)"><span style="color:rgb(34,34,34)">Blog :  </span><a \
href="http://lankavitharana.blogspot.com/" \
target="_blank">http://lankavitharana.<wbr>blogspot.com/</a></div><div \
style="color:rgb(136,136,136)"><a href="http://wso2.com/signature" \
target="_blank"><img \
src="http://c.content.wso2.com/signatures/wso2-signature-general.png" \
alt=""></a><br></div></div></div></div></div> </font></span></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div \
class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div \
dir="ltr"><div><div dir="ltr"><b>Kishanthan Thangarajah</b><div>Technical \
Lead,</div><div>Platform Technologies Team,</div><div>WSO2, \
Inc.</div><div><span>lean.enterprise.middleware</span></div><div><span><br></span></div><div><span>Mobile \
- +94773426635</span></div><div>Blog -  <i><a href="http://kishanthan.wordpress.com" \
target="_blank">http://kishanthan.wordpress.com</a></i></div><div>Twitter -  <i><a \
href="http://twitter.com/kishanthan" \
target="_blank">http://twitter.com/kishanthan</a></i></div></div></div></div></div></div></div>
 </div>



_______________________________________________
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic