我正在尝试使用
Spring Boot 1.2.6和HibernateJpaAutoConfiguration.它很棒;这就是它“神奇地”由entityManager创建,并使用我的spring.jpa.*属性为我开箱即用.
但是,在数据库错误(凭据无效,数据库无法访问等)的情况下,我找不到一种在启动时捕获Hibernate异常的简洁方法.
我想我可以捕获所有BeanCreationException并检查根本原因,但这似乎是一个非常黑客:
ConfigurableApplicationContext context; try { SpringApplication app = new SpringApplication(Application.class); context = app.run(args); } catch (Exception e) { // check if it is a DB error. if( ExceptionUtils.getRootCause(e) instanceof( HibernateException ) ){ System.out.println( "You are having trouble with the DB: " + ExceptionUtils.getRootCauseMessage(e) ); } } finally{ // finished so close the context if( context != null ) context.close(); }
在不失去使用自动配置的能力的情况下,是否有更干净/更简洁的方法?另外,如果没有解析错误消息,有没有办法确定Hibernate / DB错误是什么(即:无效的信用证,未经验证的数据库等).