0

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)
  • 2
    The problem is likely the configuration. I'm no expert with hibernate, but check this link out: https://stackoverflow.com/questions/23222632/java-lang-unsupportedoperationexception-the-application-must-supply-jdbc-connec – Chol Nhial Jan 04 '18 at 03:53
  • Thanks For that, I did look at that previously and tried changing the properties file based on what they suggest but I hit the same issue. – henrywright88404 Jan 04 '18 at 03:56
  • 1
    have you tried prefixing the properties with hibernate.? – Maciej Kowalski Jan 04 '18 at 08:27
  • yes i tried prefixing the properties in the properties file like `hibernate.connection.driver_class=com.mysql.jdbc.Driver` but then i get a host of other errors Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] followed by around 6 other errors – henrywright88404 Jan 05 '18 at 03:23
  • Never mind found the issue, in my database url property for some reason `jdbc:mysql://localhost:3306/gmail_to_facebook?useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC` worked in the xml file but not when retrieved from the properties file and i had to remove the `amp` and `;` from the url Thanks for your help – henrywright88404 Jan 05 '18 at 03:34

1 Answers1

0

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