简体   繁体   English

OSGI容器中的数据源

[英]DataSource in an OSGI container

I have a simple Spring App that connects to a DB via an EntityManager 我有一个简单的Spring App,它通过EntityManager连接到数据库

So I have to following configuration: 所以我必须进行以下配置:

<bean id="domainEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="TheManager" />
    <property name="dataSource" ref="domainDataSource" />
    <property name="packagesToScan" value="com.conztanz.persistence.stock.model" />

    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        </props>
    </property>
</bean>


<bean id="domainDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost:5433/dbName" />
    <property name="username" value="xxxx" />
    <property name="password" value="xxxx" />
</bean>

This works fine when lunched via a main class (loading the AppContext manually) 通过main类午餐时,此方法工作正常(手动加载AppContext)

But, once deployed into ServiceMix I get the following error : 但是,一旦部署到ServiceMix ,就会出现以下错误:

Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [org.postgresql.Driver]
  • I've read somewhere that OSGI and DriverManager don't mix well but I fail to understand why. 我读过某个地方的OSGIDriverManager混合不好,但我不明白为什么。
  • The solution that seems to be a good practice is to expose the dataSource as an OSGI bundle, do you agree ? 似乎一个好的做法是将dataSource公开为OSGI捆绑软件,您是否同意? and in that case how would you have access to it from a spring context to be able to have an EntityManager for example ? 在那种情况下,您将如何从spring context访问它以拥有例如EntityManager

DriverManager does not work well in OSGi. DriverManager在OSGi中无法正常运行。 The easiest way is to use a DataSource directly. 最简单的方法是直接使用数据源。 Most DB drivers have such a class. 大多数数据库驱动程序都具有此类。 If you instantiate it in your app context then it will work. 如果您在应用程序上下文中实例化它,那么它将起作用。 The downside is though that it binds your application to the DB driver as it then will import the packages for the DataSource impl. 缺点是,它将应用程序绑定到DB驱动程序,因为它将随后为DataSource impl导入软件包。

A more loosely coupled way is to use ops4j pax jdbc . 一种更宽松的耦合方式是使用ops4j pax jdbc It allows to create a DataSource as an OSGi service from a config in config admin. 它允许从config admin中的配置创建一个数据源作为OSGi服务。 So in your app context you just have to add a dependency to a DataSource service. 因此,在您的应用程序上下文中,您只需要向DataSource服务添加依赖项。 So your application is not bound to the specific DB driver. 因此,您的应用程序未绑定到特定的数据库驱动程序。 One typical use case is to use H2 in tests and oracle in production. 一种典型的用例是在测试中使用H2,在生产中使用oracle。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM