当前位置: 首页 > 知识库问答 >
问题:

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

徐友樵
2023-03-14

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

  • BundleA:使用非托管本机hibernate访问数据库1。
  • BundleB:也可以使用非托管的本机hibernate,但要访问数据库2。
  • BundleC:导入BundleA和BundleB

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

有人能给我一些建议吗?

BundleA的蓝图.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的蓝图.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:(bundle a的区别是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,我尝试从实体/b1.mapping.xml读取映射文件。在bundleB中,我尝试从entity/b2.mapping.xml读取映射文件。但在日志中,hibernate从entity/b1.mapping读取映射文件。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

捆绑包 B 的日志:

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]

我的环境是:

  • Apache Karaf 3.0.1
  • Hibernate 4.2.12.Final

另外HibernateUtil类是Dao类,我在问这个问题的时候改了名字。抱歉,如果它使任何混乱。:(

共有1个答案

司寇研
2023-03-14

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

有关详细的解释:来自< code > OsgiSessionFactoryService 的方法< code>getService为您的包提供了< code>SessionFactory实例。然而,由于某些原因,该服务将所有请求会话工厂的包添加到随后搜索配置文件的包中。参见第85行的代码。因此,请求会话工厂的第二个包将总是获得以前的配置。

虽然我没有检查这一点,但我认为您可以通过将您的配置文件放在一个包的不同路径中来绕过这个错误,例如,对于包a,放在< code >/CFG/hibernate . CFG . XML 中。

希望这有帮助。

 类似资料:
  • 我试图使用嵌入Java应用程序中的Log4j2的XML配置文件,但该文件不起作用。 代码: 错误: 线程“main”java中出现异常。lang.ClassCastException:org。阿帕奇。登录中。log4j。果心配置。无法将XMLConfiguration转换为组织。阿帕奇。登录中。log4j。果心配置。中的配置工厂。枪战。发射前。服务器主要的main(main.java:62)

  • 我刚穿春靴。我想用mysql、hibernate、jpa和JSP配置spring boot。当我想启动它时,我出现了一个错误:“Error creating bean with name'session factory'等”,更多信息请参见StackTrace: pom.xml`4.0.0 `

  • 问题内容: 我有一个基于httpd官方映像的docker容器。从无处(新构建后),它开始失败并显示以下错误: 没有其他的。 我使用的是官方的httpd图片(),到目前为止一切正常。 该错误仅在用 问题答案: (编辑,谢谢delboy1978uk如果应用简单的最佳实践可以避免该错误:将docker映像固定到特定版本而不是。 在挖掘了官方httpd图像的提交之后,我找到了解决方案。(也许这个问题/答案

  • 问题内容: 我在github仓库中下载了spring batch admin应用程序,并将其导入了eclipse中。它完美地工作。 然后,我问自己如何将外部配置文件导入可以在我的职务定义类中使用的应用程序中。 我尝试了这个: 虚拟机参数 作业配置 XML配置 使用tomcat 7启动应用程序时的堆栈跟踪 pom.xml 我使用的文件和配置与spring batch admin github项目完全

  • 后台- 我有一个python程序,它调用API,将数据保存到数据帧,然后将数据写入

  • 我正在尝试用Babel设置网页包来编译React文件。webpack配置是在VSCode中自动生成的,具有webpack扩展名。我尝试在预设部分添加“@babel/preset react”,但它不起作用,出现以下错误: 模块解析失败:意外令牌(11:16)您可能需要一个适当的加载程序来处理此文件类型。 似乎thar webpack忽略了“@Babel/预设反应”?配置如下。 非常感谢。