java - 防止 Hibernate 初始化程序崩溃

标签 java hibernate exception

我有一个使用基本 Hibernate session 工厂的 Java 程序。我的 hibernate hbm.xml 映射文件出现问题,即使我在 try catch 中调用了 getSessionFactory() ,它也会使我的程序崩溃

                try
                {
                    session = SessionFactoryUtil.getSessionFactory().openStatelessSession();    
                    session.beginTransaction();
                    rh = getRunHistoryEntry(session);
                    if(rh == null)
                    {
                        throw new Exception("No run history information found in the database for run id " + runId_ + "!");
                    }
                }
                catch(Exception ex)
                {
                    logger.error("Error initializing hibernate");
                }

它仍然设法摆脱这个 try/catch 并使主线程崩溃。我怎样才能阻止它这样做?主要问题是我有一堆清理命令需要在主线程关闭之前运行,并且需要能够保证即使在失败之后它仍然会清理并稍微优雅地关闭。 session 工厂如下所示:

public class SessionFactoryUtil {
    private static final SessionFactory sessionFactory;  

    static {  
        try 
        {  
            // Create the SessionFactory from hibernate.cfg.xml  
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } 
        catch (Throwable ex) 
        {  
            // Make sure you log the exception, as it might be swallowed  
            System.err.println("Initial SessionFactory creation failed." + ex);  
            throw new ExceptionInInitializerError(ex);  
        }  
    }  

    public static SessionFactory getSessionFactory() 
    {   try
        {
            return sessionFactory;
        }
        catch(Exception ex)
        {
            return null;
        }
    }  
}

抛出的错误如下,我已修复它,但我想防止任何 hibernate 初始化错误停止主线程。

Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Could not parse mapping document from resource hibernate/TmdIndataLine.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.ryanco.db.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:19)
    at com.ryanco.rta.RtaMain.main(RtaMain.java:148)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource hibernate/TmdIndataLine.hbm.xml
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:616)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1635)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1603)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1582)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1556)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
    at com.ryanco.db.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:13)
    ... 1 more
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:555)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:613)
    ... 8 more
Caused by: org.dom4j.DocumentException: Error on line 114 of document  : The element type "class" must be terminated by the matching end-tag "</class>". Nested exception: The element type "class" must be terminated by the matching end-tag "</class>".
    at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:546)
    ... 9 more

最佳答案

SessionFactoryUtil 中的静态初始化程序对 Throwable 的所有实例都有一个 try/catch,但主 try/catch block 仅捕获异常。我会更改它以捕获 Throwables,看看是否可以解决问题。

关于java - 防止 Hibernate 初始化程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2814012/

相关文章:

java - Java EE 应用程序的理想错误页面

java - 如何在quartz上下文中执行db/jpa操作?

java - char 数组到字符串并在字符之间放置点

java - Java 中 JSONObject 的替代品

java - MultipleBagFetchException 在 Hibernate 4 中不会发生,但在 5 中会发生

java - 从构造函数抛出异常?

ruby - 如何捕获 ruby​​ 中的所有异常?

Java JTextField 不会显示

java - 将对象保存到数据库时 hibernate 未知实体异常

java - 在部署中保留实体