Apache Felix not able to access Postgres JDBC

匿名 (未验证) 提交于 2019-12-03 01:36:02

问题:

I downloaded Postgresql-9.2-1003.jdbc3.jar and put it in felix\bundle directory.

My program accesses Postgres table EMP and prints it. I am trying to do it in Felix OSGi server. There are two parts of my program:

  1. Part-1 program which simply connects to Postgres JDBC driver and opens the database:

    package com.myprogram.myemp;

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import org.postgresql.Driver;

    public class ConnectPostgres {

    static final String DB_URL = "jdbc:postgresql://localhost:5432/scott"; static final String UNAME = "postgres"; static final String PWORD = "password";   public void myMain() {     Connection conn = null;     ResultSet rs = null;     Statement st = null;     String JDBC_DRIVER = Driver.class.getName();      try {         Class.forName(JDBC_DRIVER);         conn = DriverManager.getConnection(DB_URL, UNAME, PWORD);         st = conn.createStatement();         rs = st.executeQuery("select * from EMP");         while (rs.next()) {             System.out.println ("EMP Name:" + rs.getLong("EMPNO") + " " + rs.getString("ENAME") );         }     } catch (Exception e) {         e.printStackTrace();     } finally {         try {             rs.close();             st.close();             conn.close();         } catch (Exception e) {             e.printStackTrace();         }     } } 

    }

  2. Part-2 program is more like launches a bundle as a service provider:

    package com.myprogram.myemp;

    import org.osgi.framework.BundleContext; import org.osgi.framework.BundleActivator;

    public class Activator implements BundleActivator {

    @Override public void start(BundleContext arg0) throws Exception {     ConnectPostgres app = new ConnectPostgres();     app.myMain(); }  @Override public void stop(BundleContext arg0) throws Exception { } 

    }

The requirement is: Using database connection of a popular database like Postgres or SQLite, I should be able to publish EMP table as a Service on a OSGi compliant server Felix, Equinox.

**The error which I am getting in Felix 3.0 is:

Unsatisfied requirement(s):

(&(package=org.postgresql))**

The driver is there, I placed it under bundle directory.

The problem as it seems to me:

  1. Database connections using JDBC are not possible in OSGi. Can OSGi connect to Databases? The specification, wikis, examples all seem to be silent about. Without which all examples look like Celsius to Fahrenheit temperature conversion programs, of no real value to business. Please correct me if my understanding about OSGi is wrong.

  2. What could be I am doing wrong? What is the other way I should try to connect to database.

Thanks in advance

回答1:

The Postgresql-9.2-1003.jdbc3.jar JAR is probably not an OSGI bundle, so you can't just install it the way you have.

Check here:

https://ops4j1.jira.com/wiki/display/PAXJDBC/PostgreSQL+Driver+Adapter

The official Maven artifact postgresql:postgresql is a plain old JAR without OSGi manifest headers. You will have to wrap this on the fly using the Pax URL wrap: handler, or build your own bundle, adding an OSGi manifest. This gap is to be filled by the Pax Tipi project.



回答2:

The newest version of postgresql (9.4-1201-jdbc41) already is an OSGi bundle.

Servicemix bundles now contains a bundled version of the postgres driver. So ou can find the postgres jdbc driver in the maven central repo.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!