简体   繁体   English

OSGi上的PostgreSQL连接器

[英]PostgreSQL connector on OSGi

I'm working on an OSGi application using Karaf. 我正在使用Karaf开发OSGi应用程序。

This application connects to a postgresql database using Hibernate 4.3. 此应用程序使用Hibernate 4.3连接到postgresql数据库。

My application works when I use the hibernate default connection pool but JBoss advices not to use it on a prod system. 当我使用hibernate默认连接池时,我的应用程序工作,但JBoss建议不要在prod系统上使用它。 So I'm trying to use a c3p0 connection pool. 所以我正在尝试使用c3p0连接池。

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: 为了使用c3p0,我还添加了必需的第三方库:

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

wrap option is set to "yes" as it's a simple jar file. wrap选项设置为"yes"因为它是一个简单的jar文件。

All those bundles are correctly loaded by karaf. 所有这些捆绑都由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). 现在我的问题是添加PostgreSQL驱动程序:我有postgresql-9.3-1102.jdbc41.jar ,它不是一个bundle(没有manifest.mf文件)。

I tried to: 我试过了:

  • add it to the classpath 将它添加到类路径中
  • wrap it using <bundle wrap="no">postgresql-9.3-1102.jdbc41.jar</bundle> in bundles.xml bundles.xml使用<bundle wrap="no">postgresql-9.3-1102.jdbc41.jar</bundle> bundles.xml
  • wrap it throught eclipse using this tutorial 使用本教程将其包装在eclipse中
  • make it into a fragment, hosted by my dao-layer bundle 将它变成一个由我的dao-layer包托管的片段

But nothing works. 但没有任何作用。 Each times c3p0 start the connexion pool and when it comes to find the right driver, it fails with this message: 每次c3p0启动连接池,当找到正确的驱动程序时,它会失败并显示以下消息:

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. hibernate.connection.url可能是错的。 But that's not my case. 但那不是我的情况。 I checked it multiple times. 我多次检查过它。
  • The postgresql driver is not loaded 未加载postgresql驱动程序

Is there any magical trick to use c3p0 with hibernate on a OSGi environment? 在OSGi环境中使用c3p0和hibernate有什么神奇的技巧吗?

Postgresql now delivers an OSGi compatible driver. Postgresql现在提供OSGi兼容的驱动程序。 It can be downloaded from maven central: mvn:org.postgresql/postgresql/9.4-1203-jdbc41 In Apache karaf it can be installed with 它可以从maven中心下载:mvn:org.postgresql / postgresql / 9.4-1203-jdbc41在Apache karaf中它可以安装

feature:install pax-jdbc-postgresql

The bundle offers a DataSourceFactory as an OSGi. 该bundle提供DataSourceFactory作为OSGi。 This can be used to create a DataSource programmatically. 这可用于以编程方式创建DataSource。

Use service:list to see the properties of this DataSourceFactory. 使用service:list查看此DataSourceFactory的属性。

Alternatively you can use pax-jdbc-config to create a DataSource from a config using pax-jdbc-config . 另外,您可以使用pax-JDBC-config来创建使用配置数据源PAX-JDBC-配置 Use osgi.jdbc.driver.class=org.postgresql.Driver to identify the Postgresql driver in the config. 使用osgi.jdbc.driver.class=org.postgresql.Driver在配置中标识Postgresql驱动程序。 If the config can be processed correctly there will be a service of type DataSource that you can refer to in your code. 如果配置可以正确处理,那么将有一个类型为DataSource的服务,您可以在代码中引用该服务。

I got a complete example using derby and hibernate that should be quite easy to adapt to postgresql. 我得到了一个使用derby和hibernate的完整示例,它应该很容易适应postgresql。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OSGi中的OpenJPA找不到MySQL JDBC连接器捆绑包 - OpenJPA in OSGi not finding MySQL JDBC Connector Bundle OSGi环境中的MySQL连接器(等级):NoClassDefFoundError - MySQL Connector in OSGi Environment (Gradle): NoClassDefFoundError postgresql驱动程序没有被OSGI容器加载? - postgresql driver not loaded by the OSGI container? 将Restlet配置为在OSGi中使用Jetty连接器(不是简单连接器) - Configure Restlet to use Jetty Connector (Not Simple Connector) in OSGi MySQL到PostgreSQL的迁移:mysql连接器 - MySQL to PostgreSQL migration: mysql connector 如何使用EclipseLink使PostgreSQL与OSGi一起工作 - How to make PostgreSQL work with OSGi using EclipseLink osgi-jax-rs-connector ClassNotFoundException javax.servlet.Filter - osgi-jax-rs-connector ClassNotFoundException javax.servlet.Filter OSGi与OSGi WAB捆绑在一起 - OSGi bundles with OSGi WAB OSGi,JPA Eclipselink,Postgresql:由以下原因引起:java.lang.ClassNotFoundException:找不到org.eclipse.persistence.jpa.PersistenceProvider - OSGi, JPA Eclipselink, Postgresql : Caused by: java.lang.ClassNotFoundException: org.eclipse.persistence.jpa.PersistenceProvider cannot be found 我们可以添加多个sql驱动程序,如OTD,MySQL Connector / J,PostgreSQL,SyBase JConnect等等吗? - Can we add multiple sql drivers like OTD, MySQL Connector/J, PostgreSQL, SyBase JConnect and more?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM