Vaadin和Hibernate - 正确关闭与数据库的连接

Way*_*der 6 java database hibernate reload vaadin

我目前遇到了一个有趣的问题.

我的情况:

  • 我目前正在开发一个Web服务(我正在使用VAADIN在eclipse中使用JAVA进行编程)
  • 我的数据库背后是java derby
  • 我正在使用hibernate作为我的数据库
  • 我目前正在Tomcat v7.0上部署它

我的问题:

  • 当我在代码中更改某些内容(无关紧要)时,服务器应该重新加载它而不需要重新启动 - 我猜这是整体预期的行为
  • 服务器重新加载应用程序成功,但如果我尝试点击某些东西(所以重新加载后),例如登录按钮,我收到一个错误

错误信息:

原因:org.hibernate.exception.GenericJDBCException:无法打开连接]与根本原因错误XSDB6:另一个Derby实例可能已经启动了数据库C:\ HTML-Ausgabe\database\DocumentDB.at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)...

我的想法

似乎在重新加载过程中,hibernate的连接/上下文不会被破坏/关闭,因此当服务器尝试重新连接到数据库时会发生错误

我的守则

我有一个名为Hibernate Listener的类:

public class HibernateListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        HibernateUtil.getSessionFactory(); // Just call the static initializer of that class    
    }

    public void contextDestroyed(ServletContextEvent event) {
        HibernateUtil.getSessionFactory().close(); // Free all resources
    }
}
Run Code Online (Sandbox Code Playgroud)

我的hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
        <property name="hibernate.connection.url">jdbc:derby:C:\HTML-Ausgabe\database\DocumentDB;create=true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.hbm2ddl.auto">create-drop</property>
        <property name="hibernate.show_sql">true</property>

        <mapping class="view.model.database.User"/>
        <mapping class="view.model.database.Document"/>
        <mapping class="view.model.database.Version"/>
        <mapping class="view.model.database.VersionData"/>
    </session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)

我的(VAADIN)web.xml,其中我为上面显示的HibernateListener添加了一个"监听器"(检查监听器上的文本):

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Bachelorprojekt</display-name>
    <context-param>
        <description>Vaadin production mode</description>
        <param-name>productionMode</param-name>
        <param-value>false</param-value>
    </context-param>
    <servlet>
        <servlet-name>Bachelorprojekt Application</servlet-name>
        <servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
        <init-param>
            <description>Vaadin application class to start</description>
            <param-name>application</param-name>
            <param-value>view.view.WebsiteFrame</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>Bachelorprojekt Application</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>view.model.database.HibernateListener</listener-class>
    </listener>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>
Run Code Online (Sandbox Code Playgroud)

我做了研究,也发布在hibernate论坛上(仍然没有一个答案:(),现在在这个网站上找不到匹配的主题.所以我希望我没有做错什么.

如果你们中的任何人能以某种方式帮助我,我会非常高兴.目前我不知道要改变什么来阻止这种错误的发生.当然,如果我更改了一行代码,那么当我的应用程序在互联网上时,我总是无法重新启动整个服务器.

非常感谢每一个答案,并认为你与我分享.

Way*_*der 4

我找到了解决问题的方法。

首先,这是一个 apache derby 问题,因为我强迫它关闭

public void contextDestroyed(ServletContextEvent event) {
    HibernateUtil.getSessionFactory().close(); // Free all resources
    try {
          DriverManager.getConnection("jdbc:derby:;shutdown=true");
    } catch (SQLException e) {}
    }
}
Run Code Online (Sandbox Code Playgroud)

它对我来说效果很好。

现在,我将数据库 vom apache derby 切换到 mysql,并注意到上面问题中描述的问题不再发生。

所以,吸取的教训是:不要使用 apache derby。我希望有一天这能对其他人有所帮助。非常感谢你的帮助。