gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:04:55 26 4
gpt4 key购买 nike

我有一个使用基本 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com