简体   繁体   English

Hibernate:崩溃后app不会关闭

[英]Hibernate: app doesn't close after crash

My app just creates an EntityManager and after that I throw a RuntimeException on purpose. 我的应用程序只是创建一个EntityManager,然后我故意抛出一个RuntimeException。 If I place the RuntimeException line before I create the entity manager, the exception is correctly caught by main and the app closes (as expected). 如果我在创建实体管理器之前放置RuntimeException行,则main会正确捕获异常并关闭应用程序(如预期的那样)。 But if any exception occurs after that, the exception is caught (I can see the stacktrace) but the app keeps running and I have to kill it. 但是如果在此之后发生任何异常,则会捕获异常(我可以看到堆栈跟踪)但应用程序仍在运行,我必须将其杀死。 In Netbeans, the thread's state is "zombie". 在Netbeans中,线程的状态是“僵尸”。

libs used 使用的库

  • Hibernate 4.3.1 Final: all jars from jpa folder and required folder ( link ) Hibernate 4.3.1 Final:来自jpa文件夹和所需文件夹的所有jar文件( 链接
  • Database connector: jtds 1.3.1 ( link ) 数据库连接器:jtds 1.3.1( 链接

My test class 我的考试班

package test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class Main {

    public static void main(String[] args) {

        // throw an exception here, app closes as expected

        EntityManagerFactory factory = Persistence.createEntityManagerFactory("pu");
        EntityManager em = factory.createEntityManager();


        // throw an exception here, it runs forever
        throw new RuntimeException();    
    }
}

persistence.xml persistence.xml中

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 
             xmlns="http://java.sun.com/xml/ns/persistence" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
            http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="pu" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
            <property name="javax.persistence.jdbc.driver" value="net.sourceforge.jtds.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.user" value="user"/>
            <property name="javax.persistence.jdbc.password" value="password"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:jtds:sqlserver://server:1433/database"/>
        </properties>
    </persistence-unit>
</persistence>

Any EntityManagerFactory could get hold of critical resources which need proper cleanup; 任何EntityManagerFactory都可以获取需要适当清理的关键资源; in your example it's probably a thread managing the connection. 在您的示例中,它可能是管理连接的线程。 You must make sure the factory is closed: normally frameworks take care of proper shutdown, but if you manage the resources yourself you have to enclose the next blocks in a finally block. 您必须确保工厂已关闭:通常框架负责正常关闭,但如果您自己管理资源,则必须将最后一个块中的下一个块包含在内。

EntityManagerFactory factory = Persistence.createEntityManagerFactory("pu");
try {
    // rest of your application here
}
finally {
    factory.close();
}

Are you trying to close your application on purpose? 您是否故意关闭申请? I'm not sure I follow what you're attempting to do here. 我不确定我是否遵循了你在这里尝试做的事情。

If all you want to do is stop the application, you can call, 如果你想做的就是停止申请,你可以打电话,

System.exit(0)

which signifies that the execution of the program was successful, or pass in other numbers to signify a failure. 表示程序执行成功,或传递其他数字表示失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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