Accessing to Java EE DataSource from an extension
3 answers
Jose,
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:
Sandy
Comments
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.
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?
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: com.ibm.team.repository.jndi
2.- Make sure you have an Activator in your plug-in
3.- Use following code:
try
{
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,
Chemi.
1.- Add following plug-in as a dependency in your manifest file: com.ibm.team.repository.jndi
2.- Make sure you have an Activator in your plug-in
3.- Use following code:
try
{
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,
Chemi.
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
<prerequisites>
<requiredService interface="com.ibm.team.repository.jndi.IJNDIService"/>
...
more prerequisites
...
</prerequisites>
Hope this help
Amaury