OSGi上的PostgreSQL连接器 [英] PostgreSQL connector on OSGi

查看:137
本文介绍了OSGi上的PostgreSQL连接器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Karaf开发OSGi应用程序。



此应用程序使用Hibernate 4.3连接到postgresql数据库。



我的应用程序在我使用hibernate default连接池,但JBoss建议不要在prod系统上使用它。所以我试图使用c3p0连接池。



我使用以下包:

 < bundle wrap =yes> hibernate-jpa-2.1-api-1.0.0.Final< / bundle> 
< bundle wrap =no> hibernate-core-4.3.7.Final< / bundle>
< bundle wrap =no> hibernate-entitymanager-4.3.7.Final< / bundle>
< bundle wrap =no> hibernate-c3p0-4.3.7.Final< / bundle>
< bundle wrap =no> hibernate-envers-4.3.7.Final< / bundle>
< bundle wrap =no> hibernate-osgi-4.3.7.Final< / bundle>

为了使用c3p0,我还添加了请求的第三方库:

 < bundle wrap =yes> c3p0-0.9.2.1< / bundle> 

wrap 选项设置为yes,因为它是一个简单的jar文件。



所有这些包都由karaf正确加载。



现在我的问题是添加PostgreSQL驱动程序:我有 postgresql-9.3-1102.jdbc41.jar ,这不是一个包没有manifest.mf文件)。



我试过:


  • 将它添加到classpath中

  • 在<$ c中使用< bundle wrap =no> postgresql-9.3-1102.jdbc41.jar< / bundle> $ c> bundles.xml

  • 使用本教程

  • 变成一个片段,由我的dao-layer包托管



但没有任何效果。每次c3p0都会启动连接池,当找到正确的驱动程序时,它会失败并显示以下消息:

  java.sql .SQLException:在java.sql.DriverManager.getDriver(DriverManager.java:278)[:1.7.0_67] 
处没有合适的驱动
。com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource。 java:240)[98:wrap_file_D_container_karaf_bundles_c3p0-0.9.2.1.jar:0]
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:146)[98:wrap_file_D_trunk_container_karaf_bundles_c3p0-0.9.2.1.jar :0]

寻找这样的问题,我发现有两个原因可能会增加:




  • hibernate.connection.url可能是错误的。但那不是我的情况。
  • postgresql驱动程序未加载



是否有任何魔法在OSGi环境中使用c3p0和hibernate?

解决方案

Postgresql现在提供了一个兼容OSGi的驱动程序。它可以从maven central下载:mvn:org.postgresql / postgresql / 9.4-1203-jdbc41
在Apache karaf中,它可以安装在

功能:安装pax-jdbc-postgresql



该包提供了一个DataSourceFactory作为OSGi。这可用于以编程方式创建数据源。



使用 service:list 来查看此DataSourceFactory的属性。

或者,您可以使用pax-jdbc-config从配置中使用 pax-jdbc-config 。使用 osgi.jdbc.driver.class = org.postgresql.Driver 来识别配置中的Postgresql驱动程序。如果可以正确处理配置,将会有一个DataSource类型的服务,您可以在您的代码中参考。



我有一个完整的使用derby和hibernate的例子应该很容易适应postgresql。


I'm working on an OSGi application using Karaf.

This application connects to a postgresql database using Hibernate 4.3.

My application works when I use the hibernate default connection pool but JBoss advices not to use it on a prod system. So I'm trying to use a c3p0 connection pool.

I use the following bundles:

<bundle wrap="yes">hibernate-jpa-2.1-api-1.0.0.Final</bundle>
<bundle wrap="no">hibernate-core-4.3.7.Final</bundle>       
<bundle wrap="no">hibernate-entitymanager-4.3.7.Final</bundle>
<bundle wrap="no">hibernate-c3p0-4.3.7.Final</bundle>
<bundle wrap="no">hibernate-envers-4.3.7.Final</bundle>
<bundle wrap="no">hibernate-osgi-4.3.7.Final</bundle>

In order to use c3p0, I also add the requiered third party library:

<bundle wrap="yes">c3p0-0.9.2.1</bundle>

wrapoption is set to "yes" as it's a simple jar file.

All those bundles are correctly loaded by karaf.

Now my problem is to add the PostgreSQL driver: I have postgresql-9.3-1102.jdbc41.jar, which is not a bundle (no manifest.mf file).

I tried to:

  • add it to the classpath
  • wrap it using <bundle wrap="no">postgresql-9.3-1102.jdbc41.jar</bundle> in bundles.xml
  • wrap it throught eclipse using this tutorial
  • make it into a fragment, hosted by my dao-layer bundle

But nothing works. Each times c3p0 start the connexion pool and when it comes to find the right driver, it fails with this message:

java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(DriverManager.java:278)[:1.7.0_67]
    at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:240)[98:wrap_file_D_container_karaf_bundles_c3p0-0.9.2.1.jar:0]
    at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:146)[98:wrap_file_D_trunk_container_karaf_bundles_c3p0-0.9.2.1.jar:0]

Looking for this kind of problem, I found there are 2 reasons this may append:

  • the hibernate.connection.url may be wrong. But that's not my case. I checked it multiple times.
  • The postgresql driver is not loaded

Is there any magical trick to use c3p0 with hibernate on a OSGi environment?

解决方案

Postgresql now delivers an OSGi compatible driver. It can be downloaded from maven central: mvn:org.postgresql/postgresql/9.4-1203-jdbc41 In Apache karaf it can be installed with

feature:install pax-jdbc-postgresql

The bundle offers a DataSourceFactory as an OSGi. This can be used to create a DataSource programmatically.

Use service:list to see the properties of this DataSourceFactory.

Alternatively you can use pax-jdbc-config to create a DataSource from a config using pax-jdbc-config. Use osgi.jdbc.driver.class=org.postgresql.Driver to identify the Postgresql driver in the config. If the config can be processed correctly there will be a service of type DataSource that you can refer to in your code.

I got a complete example using derby and hibernate that should be quite easy to adapt to postgresql.

这篇关于OSGi上的PostgreSQL连接器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆