使用带有hibernate的属性文件连接到Mysql数据库

时间:2018-01-04 03:45:07

标签: java mysql hibernate

我已经查看了与此相似的各种问题但未找到直接命中需要帮助的问题。

所以以前我在hibernate xml配置文件中的所有数据库连接详细信息如下所示(此时所有内容都按预期工作,所以我只包含更改的行)

        <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>

我将这些行改为

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

我使用了这些属性并将它们放在属性文件dbConnection.properties

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

我的应用程序中有一个构建并返回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;

以及使用会话工厂的方法

    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 ****

在我做出这些更改之前,一切都按预期工作。但是,现在我收到以下错误。

你能帮助我理解为什么现在抛出这个错误吗?因为我不太清楚如何或为什么不再返回JDBC连接。

非常感谢

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)

1 个答案:

答案 0 :(得分:0)

感谢您的帮助,作为对我自己和其他用户的说明,不要假设之前有效的内容仍然有效。我的问题是用于数据库连接的URL。

当此URL位于XML文件中时,它如下jdbc:mysql://localhost:3306/gmail_to_facebook?useSSL=false&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC

但由于某些原因从属性文件中获取此信息时,处理放大器时遇到问题;在URL中,所以我删除它们以获得以下

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

由于