UnsupportedOperationException: The application must supply JDBC connections

后端 未结 3 651
走了就别回头了
走了就别回头了 2020-12-03 19:28

If I dont set anything programmatically and just call Configuration configuration = new Configuration().configure(); and use the hibernate.properties (as below)

相关标签:
3条回答
  • 2020-12-03 19:35

    Even if you are setting the connection parameters (URL, username, passwords) programmatically you should make sure other conditions which would force the usage of JDBC connections at application startup are not present. I identified at least two:

    1. Setting the hibernate.hbm2ddl.auto property to any other value than "none" forces a connection to the DB at startup; to validate, create or update the DB schema...

    2. If you are using C3P0 properties in your hibernate.cfg.xml or persistence.xml, this also forces the pooling manager to try too acquire connections from the DB at startup

    In all these cases, since you forced the connections to the DB without also configuring the connection parameters you will receive various error (like the one you faced)...

    In order to fix these error either disable the property (hibernate.hbm2ddl) or set the other properties also programmatically (hibernate.c3p0.*)

    0 讨论(0)
  • 2020-12-03 19:50

    An alternate way will be load all properties from hibernate.cfg.xml or hibernate.properties and overwrite only the one required programmatically.

    Configuration config = new Configuration().configure();
    config.setProperty("hibernate.connection.username", "xyz" );
    config.setProperty("hibernate.connection.password", "password" ); 
    
    0 讨论(0)
  • 2020-12-03 19:51

    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);    
    }  
    
    0 讨论(0)
提交回复
热议问题