It's all about the answers!

Ask a question

Accessing to Java EE DataSource from an extension

Jose Miguel Ordax Cassa (2.4k4126100) | asked Jul 11 '13, 6:34 a.m.
edited Feb 23 '14, 8:55 a.m. by Sreerupa Sen (1.0k4)
Is it possible? How?
RTC seems to use Java EE DataSources defined in the Application Server but I haven't found any way to do it.

Thanks in advance,


3 answers

permanent link
Sandy Grewal (1.6k1223) | answered Jul 11 '13, 8:45 a.m.


By default RTC uses direct JDBC, it can use Java EE DS from the App Server if configured to do so, you can set it in the Database connection on the admin page.

Check the topic, Configuring Database Connection in the IC:


Jose Miguel Ordax Cassa commented Jul 11 '13, 10:25 a.m.

Hi Sandy, I understand that documentation is to setup RTC itself.
I was looking for a way to access a DS from custom code in a RTC Extension. In my case, for example, the lookups never work. Thanks.

Jose Miguel Ordax Cassa commented Jul 11 '13, 11:50 a.m.

I try to create a JNDI InitialContext and it is always null:

Context initialContext = new InitialContext();

I always get a javax.naming.NoInitialContextException so it seems to me I haven't access to Java EE environment from a RTC Extension. Should I add anything to dependencies?

permanent link
Jose Miguel Ordax Cassa (2.4k4126100) | answered Jan 30 '14, 8:59 a.m.
I think I found a way to do it, although I am sure it is not supported:

1.- Add following plug-in as a dependency in your manifest file:
2.- Make sure you have an Activator in your plug-in
3.- Use following code:

  BundleContext bundleContext = Activator.getContext();
  IJNDIService testService = (IJNDIService)bundleContext.getService(bundleContext.getServiceReference(IJNDIService.class.getName()));
  Context initialContext = testService.getInitialContext();
  DataSource datasource = (DataSource)initialContext.lookup("java:comp/env/jdbc/YourDB");
  Connection con = datasource.getConnection();

I have tested successfully with RTC 4.0.4 on Tomcat

Hope it helps,


permanent link
Amaury Quintero (901217) | answered Jan 30 '14, 1:47 p.m.
Hi, I found this very useful, just a few comments, in case of Team Concert, the examples recommend do not creat the Activator class cause these plugins are not pure osgi, in that case we can get the bundle like this 

try {
      BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
      ServiceReference jndiServiceReference = bundleContext.getServiceReference(IJNDIService.class.getName());
      IJNDIService jndiService = (IJNDIService) bundleContext.getService(jndiServiceReference);
      Context initialContext = jndiService.getInitialContext();
      DataSource datasource = (DataSource) initialContext.lookup("java:comp/env/jdbc/YourDB");
      Connection con = datasource.getConnection();

or if you set the prerequisites in your Team Concert service then you just need to do that inside the component

try {
     IJNDIService jndiService = (IJNDIService) getService(IJNDIService.class);
     Context initialContext = jndiService.getInitialContext();
     DataSource datasource = (DataSource) initialContext.lookup("java:comp/env/jdbc/YourDB");
     Connection con = datasource.getConnection();

also in the plugin.xml

            <requiredService interface=""/>
              more prerequisites

Hope this help

Your answer

Register or to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.