How to add jdbc drivers to a plugin?

I try to create a plugin for bamboo, and I wound like to write to the database. The server is bamboo 5.15, using postGreSQL 9.6. I tried adding the .jar to different locations on the server, but it does not load it. (see my first question here )

How can I register the driver for my plugin ?

1 Like

So you’re introducing a backchannel to write to the database, correct? Bad! Bad! :wink: Beware of the caches – The db might contain stale information compared to, for example, ActiveObjects.

If you request to inject the PluginHibernateSessionFactory in the constructor arguments, do you get it? (don’t forget to add it as component-import in atlassian-plugin.xml). It works in Confluence and that’s the most canonical way.

Otherwise, use this algorithm which has never failed me yet:

  1. DriverManager.getDrivers();
  2. If null, load the driver’s class using Class.forName(driverClass), then try again to call DriverManager.getDrivers().
  3. If null, load the class from com.atlassian.plugin.util.ClassLoaderUtils#loadClass().
  4. If still null, instantiate the driver directly with driverClass.newInstance(). This fails with some badly-programmed drivers which require to be singleton (and that’s why we try using the DriverManager first), but sometimes there’s no other way.

Also, once you get this reference, cache it, because you don’t want to call newInstance() for each request. The resulting script, with all fallbacks, try/catch and conditions, should be around 50-70 lines long.

1 Like

I’ll re-iterate what @playsql said. Accessing the database directly is fraught with danger. That said - bamboo 5.15 is using sal 3.0. SAL 3.0 introduced a TransactionalExecutor:!TransactionalExecutor

Inject that into your code and then you can play around in the database directly without too much risk of doing bad things to the connection object:
Examples are at