It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Confluence plugin servlet and JDBC classpath issue

Paul Guthrie Feb 28, 2012

I'm having a problem getting a servet (part of a plugin) to connect to an external datasource. The datasource is a postgresql DB, and is defined in server.xml and can be accessed through the (wonderful) SQL plugin.

However, I cannot get to the DB through my servlet. I've tried three things:

1) By the standard Class.forName("org.postgresql.Driver"); syntax. I get a "no class found" error.

2) By directly instantiating a postgresql Driver object (Driver d = new org.postgresql.Driver()) and registering it. I get a java.lang.NoClassDefFoundError: org/postgresql/Driver error.

3) By getting an initial context within the servlet's init method ( Context ctx = InitialContext() ) and going after the datasource since clearly the SQL plugin can read the data source.

Nothing has worked to date. First, how is it that the postgresql libs appear to not be within the servlet's classpath? They are obviously accessible to Confluence as it is the database for the instance as well as for the SQL plugin calls.

Second, I'd rather use the datasource approach to raw JDBC connections. Does anyone have a code snippet that shows how that can work within the context of a servlet?

Thanks!

Paul


3 answers

1 accepted

1 vote
Answer accepted
Paul Guthrie Mar 13, 2012
To answer this there are three ways I've found. 1. Yes, it is possible to bundle the .jar file with my own plugin. I did get that working. 2. Not liking that solution, I wrote a custom classloader which loaded the postgresql drivers jar from the server. That too worked. 3. Not liking that solution, I grabbed the source for the SQL plugin, and it has a good source-code example of how to use the datasource through the InitialContext. There was a little magic in there I would never have figured out. That's what I'm going with.
Joe Clark Atlassian Team Mar 13, 2012

Nice work digging to find an answer to your own solution! I admit that it's an area I haven't messed around with much, so I'm glad you were able to find a good solution.

Sandro Hörler Aug 09, 2017

And how did you do that:

Yes, it is possible to bundle the .jar file with my own plugin.

Pls share, thx

0 votes
Joe Clark Atlassian Team Feb 28, 2012

The Atlassian plugin system uses OSGi to help keep plugins and the core app separate from each other - this means that each plugin can have a different 'effective' classpath and not everything from the host application is exposed to plugins by default.

The easiest solution is to bundle the database driver within your plugin.

Edit: That is to say, there is probably a way to configure your plugin to request the driver be provided to it by the host application, but I haven't dug into it to see exactly what would be required :)

Paul Guthrie Feb 28, 2012

Is there anyway to override the classpath within the manifest to include other server-hosted jars? Java doesn't handle jars within jars well and it doesn't really make sense to have to include a jar which is already part of the confluence installation.

Eric Koepfle Dec 10, 2012

I kind of agree with the above poster. Including multiple jars of the ~same~ classes is generally a very bad idea in Java.

Yet another reason for me to dislike OSGi. It is a framework for administrators, not developers. Unfortunately we are forced to comply.

0 votes
Chris Kent Jul 14, 2014

Hi,

Has anyone got the Class.forName("org.postgresql.Driver"); to work in a plugin.

I want to use this method for connecting to a database, I know the drivers jar file exists as I am using the same database as the confluence external database, so I know it's there.

Why does my plugin throw the exception " java.lang.ClassNotFoundException: org.postgresql.Driver"

Paul you said you found some magic, can you please share, although I'm not interested if I need to define the datasource in the server.xml file -> not very flexible solution when distributing a plugin. I know the SQL plugin can dynamically do this through its Configure menu, so I'm sure this can be achieved - please help.

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Confluence

6 Awesome Ways to Apply Trello, JIRA and Confluence to your Project

I attended  Atlassian Summit 2019  and learned a lot from the presenters, attendees and knowledgeable Atlassian product managers. The presentations I attended focused on applying Agile, pla...

1,301 views 8 19
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you