SessionFactory在karaf中加载错误的配置文件

时间:2014-05-29 07:46:44

标签: hibernate osgi apache-karaf karaf

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

  • BundleA:使用非托管原生hibernate访问数据库1。
  • BundleB:也使用非托管本机hibernate但访问数据库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 中读取映射文件。 但是在日志中,看起来hibernate从 entity / b1.mapping.xml中读取映射文件,在两个包中

bundldA的日志:

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 方法之前,我打印包名称以检查我是否收到了错误的包,但是包名是我期望的。

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
  • Hibernate 4.2.12.Final

P.S。 HibernateUtil类是Dao类,我在问这个问题时改了名字。对不起,如果它有任何混乱。 :(

1 个答案:

答案 0 :(得分:0)

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

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

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

希望这有帮助。