user5479362
user5479362

Reputation:

NPE when configuring PostgreSQL DataSource in Equinox

I have the following DataSource configuration file for PAX:

osgi.jdbc.driver.class=org.postgresql.Driver
url=jdbc:postgresql://localhost:5432/demo
dataSourceName=demo-pg
user=demo
password=demo

It creates DataSource in Karaf, however, in Eclipse, using Equinox OSGi launcher, I get NPE:

java.lang.NullPointerException
    at java.util.Hashtable.put(Hashtable.java:459)
    at java.util.Properties.setProperty(Properties.java:166)
    at org.postgresql.ds.common.BaseDataSource.setProperty(BaseDataSource.java:1030)
    at org.postgresql.ds.common.BaseDataSource.setUrl(BaseDataSource.java:964)
    at org.postgresql.osgi.PGDataSourceFactory.configureBaseDataSource(PGDataSourceFactory.java:65)
    at org.postgresql.osgi.PGDataSourceFactory.createSimpleDataSource(PGDataSourceFactory.java:127)
    at org.postgresql.osgi.PGDataSourceFactory.createDataSource(PGDataSourceFactory.java:149)
    at org.ops4j.pax.jdbc.config.impl.DataSourceRegistration.createDs(DataSourceRegistration.java:108)
    at org.ops4j.pax.jdbc.config.impl.DataSourceRegistration.<init>(DataSourceRegistration.java:63)
    at org.ops4j.pax.jdbc.config.impl.DataSourceFactoryTracker.addingService(DataSourceFactoryTracker.java:42)
    at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:941)
    at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:1)
    at org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:256)
    at org.osgi.util.tracker.AbstractTracked.trackInitial(AbstractTracked.java:183)
    at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:318)
    at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:261)
    at org.ops4j.pax.jdbc.config.impl.DataSourceConfigManager.updated(DataSourceConfigManager.java:80)
    at org.apache.felix.cm.impl.helper.ManagedServiceFactoryTracker.updated(ManagedServiceFactoryTracker.java:159)
    at org.apache.felix.cm.impl.helper.ManagedServiceFactoryTracker.provideConfiguration(ManagedServiceFactoryTracker.java:93)
    at org.apache.felix.cm.impl.ConfigurationManager$UpdateConfiguration.run(ConfigurationManager.java:1772)
    at org.apache.felix.cm.impl.UpdateThread.run0(UpdateThread.java:141)
    at org.apache.felix.cm.impl.UpdateThread.run(UpdateThread.java:109)
    at java.lang.Thread.run(Thread.java:745)

After inspecting in Debugger, I've found out, the error is thrown because property PROTOCOL_VERSION is null. I've tried to set the variable in config file:

protocolVersion=3

but I still get the same error.

How to configure PostgreSQL DataSource in Equinox/Eclipse runtime?

I'm using PostgreSQL driver version 9.4-1200 :

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1200-jdbc41</version>
    </dependency>

Upvotes: 1

Views: 173

Answers (2)

Alessandro Da Rugna
Alessandro Da Rugna

Reputation: 4695

It seems resolved with new PostgreSQL 42.1.1 driver.

A note on PAXJDBC PostgreSQL Driver Adapter page states:

Current postgresql versions are valid bundles and also provide a DataSourceFactory. So if you use a recent version you will not need this adapter anymore.

Try this driver:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.1.1</version>
</dependency>

Upvotes: 1

Christian Schneider
Christian Schneider

Reputation: 19606

You might be hitting the issue PAXJDBC-107. Pax-jdbc-config uses reflection to write the properties. It currently works if the datasource has a setter with an Integer parameter but not with int. I plan to fix this in the next days and do a new release.

Upvotes: 0

Related Questions