UnsupportedOperationException:应用程序必须提供JDBC连接 [英] UnsupportedOperationException: The application must supply JDBC connections

查看:573
本文介绍了UnsupportedOperationException:应用程序必须提供JDBC连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我没有以编程方式设置任何东西,只需调用 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的=真安培; failOverReadOnly =假安培; maxReconnects为= 10\" ;
配置配置=新配置()
.setProperty(hibernate.connection.url,连接)
.setProperty(hibernate.connection.username,Globals.DB_USER_NAME)
.setProperty(hibernate.connection.password,Globals.DB_PASSWORD);
configuration.configure();

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

例外



我现在得到这个异常,对于我的hbm文件中的每一个映射资源条目都有一个异常。

  2013年5月11日星期三08:46:31,969 1300 [main]致命ReadOnlyOperations  - 选择忽略此运行时异常java.lang.UnsupportedOperationException:应用程序必须提供JDBC连接,可能会致命,仔细检查
2013年5月11日08:46:31,969 1300 [main] FATAL ReadOnlyOperations - java.lang.UnsupportedOperationException:应用程序必须提供JDBC连接



小结



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



我需要有人帮助我找出我缺少的东西。它应该是一个非常基本的东西。

解决方案

哇,只是解决了问题。

'pre> 的sessionFactory = configuration.buildSessionFactory(新ServiceRegistryBuilder()applySettings(configuration.getProperties())buildServiceRegistry());

我错过了



.applySettings(configuration.getProperties())





学习


  1. configure()应该被称为 AFTER setProperty

  2. 使用 hibernate.connection.url NOT connection.url c $ c> hibernate.dialect = org.hibernate.dialect.MySQLInnoDBDialect

  3. 将hibernate日志的log4j属性设置为ALL,以便查看更详细的问题

  4. 删除 WARN认可的过时hibernate命名空间http://hibernate.sourceforge.net/。改为使用命名空间http://www.hibernate.org/dtd/。请参阅Hibernate 3.6迁移指南!,您需要在 cfg中替换 http://www.hibernate.org/dtd/ .xml 和所有 hbm 文件。不要忘记 hbm 文件,它们也使用相同的DTD。

最后,修复 Bill Gorder 的最后一条建议非常好。

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


If I dont set anything programmatically and just call Configuration configuration = new Configuration().configure(); and use the hibernate.properties (as below) everything works just great. As soon as I try to provide username, password and connection url programmatically, I get a weird Exceptions, hinting at the hbm file. What am I missing ?

This Works

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

As per @Kshitij recommendation. Doing a mixed mode.

The hibernate.properties now is

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

The Code

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());

The Exception

I now get this exception, one for every mapping resource entry in my hbm file.

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

Summary

If I use all hibernate.properties and no code (no .setProperty in code) everything works great. If I use part hibernate.properties and part code (server, username, password) I get errors for in the hbm for every mapping property.

I need someone to help me figure out what I am missing. It should be something really basic.

解决方案

Wow, just fixed the problem.

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

I was missing the

.applySettings(configuration.getProperties())

Learnings

  1. configure() should be called AFTER setProperty
  2. Use hibernate.connection.url and NOT connection.url if you use hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
  3. Set log4j property for hibernate logs to ALL, so that you can see more detailed issues
  4. To get rid of the WARN Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!, you need to replace http://www.hibernate.org/dtd/ in the cfg.xml and all hbm files too. Dont forget teh hbm files, they too use the same DTD.

Lastly, referred to this, to fix this. The last advice by Bill Gorder is superb.

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

这篇关于UnsupportedOperationException:应用程序必须提供JDBC连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆