【问题标题】:SessionFactory loads wrong config file in karafSessionFactory 在 karaf 中加载错误的配置文件
【发布时间】:2014-07-18 15:56:48
【问题描述】:

我正在尝试创建 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 中的两个包中的休眠读取映射文件。

bundledA 的日志:

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

根据这个document,我通过使用由hibernate-osgi服务导出的SessionFactory服务在Dao类的init方法中获得了会话工厂。 在调用 getService 方法之前,我打印了 bundle name 来检查我是否得到了错误的 bundle,但是 bundle name 是我所期望的。

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]

我的环境是:

  • Apache Karaf 3.0.1
  • 休眠 4.2.12.Final

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

【问题讨论】:

  • 谁是导出SessionFactory Service的bundle?看来您在两个 daos 中都获得了相同的服务
  • 我从 hibernate-osgi 包中获得了 org.hibernate.SessionFactory 服务。根据hibernate's document
  • 对不起,从日志中看不到错误的配置文件。也许您可以提供一些信息,您可以从中得出结论。
  • @ArievanWijngaarden ,我编辑了帖子,在底部添加了我得出结论的原因。谢谢你:)
  • 我有一个问题,我是新手,但是你是如何让 mysql 驱动程序工作的呢?什么是verManagerConnectionProviderImp?

标签: hibernate osgi apache-karaf karaf


【解决方案1】:

查看了hibernate-osgi的代码后,我断定这是hibernate-osgi类org.hibernate.osgi.OsgiSessionFactoryService的一个bug。你应该在那里提交一个错误。

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

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

希望这会有所帮助。

【讨论】:

  • 感谢您的回答,@ArievanWijngaarden :) 我不认为这可能是一个错误...我会尝试更深入地查看源代码以查看是否存在。
  • 你真的应该尝试不同的位置绕过。
  • 我只是尝试将 cfg.xml 文件放在不同的位置,它看起来很有效!(日志显示 hibernate 在正确的位置读取配置文件,虽然我没有尝试访问道实际上......)。非常感谢您的帮助!
猜你喜欢
  • 2011-09-12
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
  • 2013-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多