简体   繁体   中英

connecting to Mysql Database using a properties file with hibernate

I have looked at various questions that are similar to this but haven't found one that directly hits at what need to ask for help with.

So previously I had all of my database connection details in a hibernate xml config file as below (everything at this point worked as expected so I have only included the changed lines)

        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">URL</property>
    <property name="connection.username">USERNAME</property>
    <property name="connection.password">PASSWORD</property>

I changed these lines to

<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

I took these properties and have placed them in a properties file dbConnection.properties

connection.driver_class=com.mysql.jdbc.Driver
connection.username=USERNAME
connection.password=PASSWORD
connection.url=URL

I have in my application a method that builds and returns a SessionFactory

private SessionFactory getSessionFactory(){
         Properties dbConnectionProperties = new Properties();

         try {
            dbConnectionProperties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("dbConnection.properties"));

        } catch (IOException e) {
            e.printStackTrace();
        }

         SessionFactory factory = new Configuration()
                    .mergeProperties(dbConnectionProperties)
                    .configure("hibernate.cfg.xml")
                    .addAnnotatedClass(EmailMessage.class)
                    .buildSessionFactory();

        return factory;

and the method that uses the session factory

    public void PostUnpostedMessageToFacebook(){


        SessionFactory factory = getSessionFacto

ry();
    System.out.println(factory.getProperties());
    // create a session 
    Session session = factory.getCurrentSession();
    session.beginTransaction();
    **** some more code let me know if you want to see it but I didn't think it was relevant as this is where the error is thrown ****

before I made these changes, everything worked as expected. however, now I receive the error below.

would you be able to help my understanding as to why this error is now being thrown? as I don't quite understand how or why there is no longer a JDBC connection being returned.

Many thanks

Exception in thread "main" java.lang.UnsupportedOperationException: The application must supply JDBC connections
    at org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:44)
    at org.hibernate.internal.NonContextualJdbcConnectionAccess.obtainConnection(NonContextualJdbcConnectionAccess.java:35)
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:115)
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:145)
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getConnectionForTransactionManagement(LogicalConnectionManagedImpl.java:263)
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.begin(LogicalConnectionManagedImpl.java:271)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.begin(JdbcResourceLocalTransactionCoordinatorImpl.java:214)
    at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:56)
    at org.hibernate.internal.AbstractSharedSessionContract.beginTransaction(AbstractSharedSessionContract.java:409)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:355)
    at com.sun.proxy.$Proxy23.beginTransaction(Unknown Source)
    at com.post2facebook.postToFacebookApp.GmailToFacebook.PostUnpostedMessageToFacebook(GmailToFacebook.java:80)
    at com.post2facebook.postToFacebookApp.MainApp.gmailToFacebookOnlyDemo(MainApp.java:16)
    at com.post2facebook.postToFacebookApp.MainApp.main(MainApp.java:11)

Thanks for your help, as a note to myself and other users don't assume that something that worked before still works. my issue was with the URL that was being used for the database connection.

when this URL was in the XML file it was as follows jdbc:mysql://localhost:3306/gmail_to_facebook?useSSL=false&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC

but for some reason when taking this from the properties file there was an issue processing the amp and ; in the URL so I removed them to get the following

hibernate.connection.url=jdbc:mysql://localhost:3306/gmail_to_facebook?useSSL=false&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM