0

我正在尝试创建 3 个捆绑包:

  • BundleA:使用非托管本机休眠访问数据库 1。
  • BundleB:也使用非托管本机休眠但访问数据库 2。
  • BundleC:导入 BundleA 和 BundleB。

我希望 bundleA 和 bundleB 将使用他们的配置文件获得会话工厂。但是,Hibernate 的日志显示BundleB 使用 BundleA 的配置文件获取会话工厂

有人可以给我任何建议吗?


BundleA 的 blueprint.xml:

<bean id="dao" class="idv.peayton.osgi.core.bundle1.Dao" init-method="init" />  
<bean id="serviceImpl" class="idv.peayton.osgi.core.bundle1.impl.B1ServiceImpl">
    <property name="dao" ref="dao" />
</bean>
<service id="service" ref="serviceImpl" interface="idv.peayton.osgi.core.bundle1.B1Service" />

BundleA 的 hibernate.cfg.xml:

<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/se00001?autoReconnect=true</property>
    <property name="hibernate.connection.username">username</property>
    <property name="hibernate.connection.password">password</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <mapping resource="entity/b1.mapping.xml"/>
</session-factory>

BundleB的blueprint.xml:

<bean id="dao" class="idv.peayton.osgi.core.bundle2.Dao" init-method="init" />  
<bean id="serviceImpl" class="idv.peayton.osgi.core.bundle2.impl.B2ServiceImpl">
    <property name="dao" ref="dao" />
</bean>
<service id="service" ref="serviceImpl" interface="idv.peayton.osgi.core.bundle2.B2Service" />

BundleB的hibernate.cfg.xml:(BundleA的区别在于url和映射资源)

<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3307/m00001?autoReconnect=true</property>
    <property name="hibernate.connection.username">username</property>
    <property name="hibernate.connection.password">password</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <mapping resource="entity/b2.mapping.xml"/>
</session-factory>

Dao 类如下所示:

public void init() {
    logger.info("Initializing session factory...");
    if (sf == null) {
        Bundle bundle = FrameworkUtil.getBundle(Dao.class);
        logger.info("Using bundle: " + bundle);

        BundleContext context = bundle.getBundleContext();
        logger.info("Using context: " + context);

        ServiceReference sr = context.getServiceReference(SessionFactory.class.getName());
        logger.info("Using servRef: " + sr);

        sf = (SessionFactory) context.getService(sr);
        logger.info("SessionFactory is: " + sf);
    }
}

日志如下所示:

2014-05-29 15:33:32,582 | INFO  | Local user karaf | HibernateUtil                    | 206 - peayton-blueprint-core-bundleA - 0.0.1.SNAPSHOT | Initializing session factory...
2014-05-29 15:33:32,582 | INFO  | Local user karaf | HibernateUtil                    | 206 - peayton-blueprint-core-bundleA - 0.0.1.SNAPSHOT | Using bundle: peayton-blueprint-core-bundleA [206]
2014-05-29 15:33:32,582 | INFO  | Local user karaf | HibernateUtil                    | 206 - peayton-blueprint-core-bundleA - 0.0.1.SNAPSHOT | Using context: org.apache.felix.framework.BundleContextImpl@c6e491
2014-05-29 15:33:32,582 | INFO  | Local user karaf | HibernateUtil                    | 206 - peayton-blueprint-core-bundleA - 0.0.1.SNAPSHOT | Using servRef: [org.hibernate.SessionFactory]
2014-05-29 15:33:32,598 | INFO  | Local user karaf | Configuration                    | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000043: Configuring from resource: /hibernate.cfg.xml
2014-05-29 15:33:32,598 | INFO  | Local user karaf | Configuration                    | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000040: Configuration resource: /hibernate.cfg.xml
2014-05-29 15:33:33,707 | INFO  | Local user karaf | Configuration                    | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000221: Reading mappings from resource: entity/b1.mapping.xml
2014-05-29 15:33:35,176 | INFO  | Local user karaf | Configuration                    | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000041: Configured SessionFactory: null
2014-05-29 15:33:35,192 | WARN  | Local user karaf | verManagerConnectionProviderImpl | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000402: Using Hibernate built-in connection pool (not for production use!)
2014-05-29 15:33:35,207 | INFO  | Local user karaf | verManagerConnectionProviderImpl | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000115: Hibernate connection pool size: 20
2014-05-29 15:33:35,207 | INFO  | Local user karaf | verManagerConnectionProviderImpl | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000006: Autocommit mode: false
2014-05-29 15:33:35,207 | INFO  | Local user karaf | verManagerConnectionProviderImpl | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://127.0.0.1:3306/se00001?autoReconnect=true]
2014-05-29 15:33:35,207 | INFO  | Local user karaf | verManagerConnectionProviderImpl | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000046: Connection properties: {user=username, password=****}
2014-05-29 15:33:35,379 | INFO  | Local user karaf | Dialect                          | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2014-05-29 15:33:35,426 | INFO  | Local user karaf | TransactionFactoryInitiator      | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000399: Using default transaction strategy (direct JDBC transactions)
2014-05-29 15:33:35,426 | INFO  | Local user karaf | ASTQueryTranslatorFactory        | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000397: Using ASTQueryTranslatorFactory
2014-05-29 15:33:35,598 | INFO  | Local user karaf | HibernateUtil                    | 206 - peayton-blueprint-core-bundleA - 0.0.1.SNAPSHOT | SessionFactory is: org.hibernate.internal.SessionFactoryImpl@1a85af4
2014-05-29 15:33:35,598 | INFO  | Local user karaf | Dao                              | 207 - peayton-blueprint-core-bundleB - 1.0.0.SNAPSHOT | Initializing session factory...
2014-05-29 15:33:35,598 | INFO  | Local user karaf | Dao                              | 207 - peayton-blueprint-core-bundleB - 1.0.0.SNAPSHOT | Using bundle: peayton-blueprint-core-bundleB [207]
2014-05-29 15:33:35,598 | INFO  | Local user karaf | Dao                              | 207 - peayton-blueprint-core-bundleB - 1.0.0.SNAPSHOT | Using context: org.apache.felix.framework.BundleContextImpl@866459
2014-05-29 15:33:35,598 | INFO  | Local user karaf | Dao                              | 207 - peayton-blueprint-core-bundleB - 1.0.0.SNAPSHOT | Using servRef: [org.hibernate.SessionFactory]
2014-05-29 15:33:35,629 | INFO  | Local user karaf | Configuration                    | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000043: Configuring from resource: /hibernate.cfg.xml
2014-05-29 15:33:35,629 | INFO  | Local user karaf | Configuration                    | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000040: Configuration resource: /hibernate.cfg.xml
2014-05-29 15:33:35,863 | INFO  | Local user karaf | Configuration                    | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000221: Reading mappings from resource: entity/b1.mapping.xml
2014-05-29 15:33:37,348 | INFO  | Local user karaf | Configuration                    | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000041: Configured SessionFactory: null
2014-05-29 15:33:37,348 | WARN  | Local user karaf | verManagerConnectionProviderImpl | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000402: Using Hibernate built-in connection pool (not for production use!)
2014-05-29 15:33:37,348 | INFO  | Local user karaf | verManagerConnectionProviderImpl | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000115: Hibernate connection pool size: 20
2014-05-29 15:33:37,348 | INFO  | Local user karaf | verManagerConnectionProviderImpl | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000006: Autocommit mode: false
2014-05-29 15:33:37,348 | INFO  | Local user karaf | verManagerConnectionProviderImpl | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://127.0.0.1:3306/se00001?autoReconnect=true]
2014-05-29 15:33:37,348 | INFO  | Local user karaf | verManagerConnectionProviderImpl | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000046: Connection properties: {user=username, password=****}
2014-05-29 15:33:37,363 | INFO  | Local user karaf | Dialect                          | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
2014-05-29 15:33:37,363 | INFO  | Local user karaf | TransactionFactoryInitiator      | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000399: Using default transaction strategy (direct JDBC transactions)
2014-05-29 15:33:37,363 | INFO  | Local user karaf | ASTQueryTranslatorFactory        | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000397: Using ASTQueryTranslatorFactory
2014-05-29 15:33:37,363 | INFO  | Local user karaf | Dao                              | 207 - peayton-blueprint-core-bundleB - 1.0.0.SNAPSHOT | SessionFactory is: org.hibernate.internal.SessionFactoryImpl@88dcd7

编辑:为什么我得到这个结论

在 bundleA 的 hibernate.cfg.xml 中,我尝试从entity/b1.mapping.xml读取映射文件。在 bundleB 中,我尝试从entity/b2.mapping.xml读取映射文件。但在日志中,它看起来像是从entity/b1.mapping.xml 中的休眠读取映射文件,在两个 bundle中。

bundleldA的日志:

2014-05-29 15:33:33,707 | INFO  | Local user karaf | Configuration                    | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000221: Reading mappings from resource: entity/b1.mapping.xml

bundleB的日志:

2014-05-29 15:33:35,863 | INFO  | Local user karaf | Configuration                    | 110 - org.jboss.logging.jboss-logging - 3.1.0.GA | HHH000221: Reading mappings from resource: entity/b1.mapping.xml

根据这个文档,我通过使用由 hibernate-osgi 服务导出的 SessionFactory 服务在 Dao 类的 init 方法中获取会话工厂。在调用getService方法之前,我打印包名称以检查是否得到错误的包,但包名称与我的预期一致。

2014-05-29 15:33:32,582 | INFO  | Local user karaf | HibernateUtil                    | 206 - peayton-blueprint-core-bundleA - 0.0.1.SNAPSHOT | Using bundle: peayton-blueprint-core-bundleA [206]

2014-05-29 15:33:35,598 | INFO  | Local user karaf | Dao                              | 207 - peayton-blueprint-core-bundleB - 1.0.0.SNAPSHOT | Using bundle: peayton-blueprint-core-bundleB [207]

我的环境是:

  • 阿帕奇卡拉夫 3.0.1
  • 休眠 4.2.12.Final

ps HibernateUtil 类是 Dao 类,我在问这个问题时更改了它的名称。对不起,如果它造成任何混乱。:(

4

1 回答 1

0

在查看了 hibernate-osgi 的代码后,我得出结论,这是 hibernate-osgi 类中的一个错误org.hibernate.osgi.OsgiSessionFactoryService。你应该在那里提交一个错误。

有关详细说明:方法getServicefromOsgiSessionFactoryService为您的捆绑包提供SessionFactory实例。但是,由于某些原因,此服务会将请求会话工厂的所有捆绑包添加到随后搜索配置文件的捆绑包中。请参见此处的代码第 85 行。因此,请求会话工厂的第二个捆绑包将始终获得以前的配置。

虽然我没有检查这一点,但我认为您可以通过将配置文件放在仅一个捆绑包中的不同路径中来绕过此错误,例如在/cfg/hibernate.cfg.xml捆绑包 A 中。由于捆绑包之间的路径不同,我认为它会由正确的装载机找到。

希望这可以帮助。

于 2014-06-04T07:56:50.800 回答