【问题标题】:UnsupportedOperationException: The application must supply JDBC connectionsUnsupportedOperationException:应用程序必须提供 JDBC 连接
【发布时间】:2013-05-05 01:19:08
【问题描述】:

如果我没有以编程方式设置任何内容,只需调用 Configuration configuration = new Configuration().configure(); 并使用 hibernate.properties(如下所示),一切都会很好。一旦我尝试以编程方式提供用户名、密码和连接 url,我就会得到一个奇怪的异常,提示 hbm 文件。我错过了什么?

这个工程

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://myEC2/mCruiseOnServerDB?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
hsqldb.write_delay_millis=0
shutdown=true
hibernate.connection.username=root
hibernate.connection.password=mypwd
hibernate.connection.pool_size=2
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.c3p0.idle_test_period=300
hibernate.c3p0.timeout=120

根据@Kshitij 的建议。做混合模式。

hibernate.properties现在是

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hsqldb.write_delay_millis=0
shutdown=true
hibernate.connection.pool_size=2
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect

守则

String connection = "jdbc:mysql://"
            + Globals.DBSERVER
            + "/mCruiseOnServerDB?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
        Configuration configuration = new Configuration()   
            .setProperty("hibernate.connection.url", connection)                                
            .setProperty("hibernate.connection.username", Globals.DB_USER_NAME)     
            .setProperty("hibernate.connection.password", Globals.DB_PASSWORD);
        configuration.configure();

        sessionFactory = configuration
                .buildSessionFactory(new ServiceRegistryBuilder()
            .buildServiceRegistry());

例外

我现在得到了这个异常,我的 hbm 文件中的每个 mapping resource 条目都有一个。

11 May 2013 08:46:31,969 1300 [main] FATAL ReadOnlyOperations  - Have chosen to ignore this runtime exception java.lang.UnsupportedOperationException: The application must supply JDBC connections, may be fatal, examine this carefully
11 May 2013 08:46:31,969 1300 [main] FATAL ReadOnlyOperations  - java.lang.UnsupportedOperationException: The application must supply JDBC connections

总结

如果我使用所有 hibernate.properties 并且没有代码(代码中没有 .setProperty),一切都会很好。如果我使用部分 hibernate.properties 和部分代码(服务器、用户名、密码),我会在每个映射属性的 hbm 中得到错误。

我需要有人帮我弄清楚我缺少什么。它应该是非常基本的东西。

【问题讨论】:

  • 我已经试过了,还是不行。

标签: hibernate hibernate-mapping


【解决方案1】:

哇,刚刚解决了问题。

sessionFactory = configuration.buildSessionFactory(new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry());

我错过了

.applySettings(configuration.getProperties())

学习

  1. configure() 应该被调用 AFTER setProperty
  2. 使用hibernate.connection.url 和不 connection.url 如果你使用hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
  3. 将休眠日志的 log4j 属性设置为 ALL,以便您查看更详细的问题
  4. 要删除WARN Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!,您需要替换cfg.xml 和所有hbm 文件中的http://www.hibernate.org/dtd/。不要忘记 hbm 文件,它们也使用相同的 DTD。

最后,提到这个,修复this。 Bill Gorder 的最后一条建议非常棒。

private static SessionFactory configureSessionFactory()    
        throws HibernateException {    
    Configuration configuration = new Configuration();    
    configuration.configure();    
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()    
            .applySettings(configuration.getProperties())    
            .buildServiceRegistry();    
    return configuration.buildSessionFactory(serviceRegistry);    
}  

【讨论】:

  • 那么你是在混合属性还是在 java 文件中添加所有属性?
  • 混合,有的在属性文件,有的在java
  • 我也有同样的问题.. 我找不到课程ServiceRegistryBuilder() 哪个版本的 HIbernate 可以工作。我正在使用 Hibernate 3.x
【解决方案2】:

另一种方法是从hibernate.cfg.xml 或hibernate.properties 加载所有属性,并仅以编程方式覆盖所需的属性。

Configuration config = new Configuration().configure();
config.setProperty("hibernate.connection.username", "xyz" );
config.setProperty("hibernate.connection.password", "password" ); 

【讨论】:

  • 我现在为每个 hbm 文件获得 10 May 2013 14:20:34,238 1196 [main] FATAL ReadOnlyOperations - Have chosen to ignore this runtime exception java.lang.UnsupportedOperationException: The application must supply JDBC connections, may be fatal, examine this carefully 1。
  • 在我进行更改后,基本上没有加载 hbm 文件。所以它还没有完成。
  • 你设置hibernate.connection.url了吗?从您修改后的代码来看,您似乎没有以编程方式放置它。
  • 是的,我有,只是忘了提。
  • 我也看到你在设置属性后完成了configuration.configure();。你需要在 setProperty 之前完成它。在上面显示的示例中查看我是如何做到的。
【解决方案3】:

即使您以编程方式设置连接参数(URL、用户名、密码),您也应确保不存在会在应用程序启动时强制使用 JDBC 连接的其他条件。我确定了至少两个:

  1. 将 hibernate.hbm2ddl.auto 属性设置为“none”以外的任何其他值都会在启动时强制连接到 DB;验证、创建或更新数据库架构...

  2. 如果您在 hibernate.cfg.xml 或 persistence.xml 中使用 C3P0 属性,这也会强制池管理器在启动时尝试从数据库获取连接

在所有这些情况下,由于您在没有配置连接参数的情况下强制连接到数据库,您将收到各种错误(就像您遇到的错误一样)...

为了修复这些错误,请禁用该属性 (hibernate.hbm2ddl) 或以编程方式设置其他属性 (hibernate.c3p0.*)

【讨论】:

    猜你喜欢
    • 2014-07-26
    • 2017-07-02
    • 2014-06-07
    • 2016-12-04
    • 2015-12-04
    • 2014-09-25
    • 2016-04-17
    • 2021-08-05
    相关资源
    最近更新 更多