[英]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: 我试过了:
<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
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: 寻找这种问题,我发现有两个原因可能会增加:
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.