处理 LocalSessionFactoryBean 异常
Handling LocalSessionFactoryBean exception
这是第一次,当我使用 LocalSessionFactoryBean 而不是 SessionFactory 时我遇到了一个问题:如果我的服务器无法连接到数据库,@Bean 方法会抛出异常并且整个应用程序将关闭。但这里有一件奇怪的事情:异常是在 bean 方法 return object.
之后抛出
@Bean
LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(getDataSource());
sessionFactoryBean.setPackagesToScan(Properties.hibernate.packages);
sessionFactoryBean.setHibernateProperties(databaseSettings.getHibernateProperties());
return sessionFactoryBean;
}
所以 try-catch 在这里不起作用。当 Spring 初始化 bean 时,它会自行运行 sessionFactoryBean.afterPropertiesSet() 方法。所以在那个时候抛出异常。毕竟我有下一个日志:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sessionFactory' defined in class path resource [app/db/Hibernate.class]:
Invocation of init method failed; nested exception is
org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
在这种情况下处理数据库连接错误的最佳方法是什么?
观察到的行为对我来说完全符合逻辑。您问题中提供的代码仅在内部配置 SessionFactory
。一旦结果 LocalSessionFactoryBean
返回(并被其他代码片段使用),就会尝试 实际上 连接您以这种方式配置的数据库。
如果配置的 DataSource
- 通过 getDataSource()
获得 - 在建立(第一个)连接时遇到错误,您将按所述点击 org.hibernate.exception.GenericJDBCException
。
注意:有关详细信息,您必须提供更多的堆栈跟踪。
作为反制措施,您可以在返回前将以下 "connection check" 添加到上述代码片段中:
@Bean
LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
DataSource ds = getDataSource();
sessionFactoryBean.setDataSource(ds);
sessionFactoryBean.setPackagesToScan(Properties.hibernate.packages);
sessionFactoryBean.setHibernateProperties(databaseSettings.getHibernateProperties());
// conduct an early connection attempt which auto-closes the opened connection
try (Connection con = ds.getConnection(username, password)) {
// if you reach this line, connections to the DB can be established.
return sessionFactoryBean;
} catch (Exception ex {
// handle exceptions with one or more of these steps
// 1. log the problem to somewhere, console...
// 2. re-try connection attempt?
// 3. escalate by throwing a self-defined RuntimeException
// .. and shutdown the application in controlled manner?
}
}
另请参阅 Oracle 的 Connecting with DataSource Objects 指南。
提示:出于多种原因,您也可以考虑使用 "PooledDataSource"(请参阅上述数据源指南中的详细信息)。
希望对您有所帮助。
这是第一次,当我使用 LocalSessionFactoryBean 而不是 SessionFactory 时我遇到了一个问题:如果我的服务器无法连接到数据库,@Bean 方法会抛出异常并且整个应用程序将关闭。但这里有一件奇怪的事情:异常是在 bean 方法 return object.
之后抛出@Bean
LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(getDataSource());
sessionFactoryBean.setPackagesToScan(Properties.hibernate.packages);
sessionFactoryBean.setHibernateProperties(databaseSettings.getHibernateProperties());
return sessionFactoryBean;
}
所以 try-catch 在这里不起作用。当 Spring 初始化 bean 时,它会自行运行 sessionFactoryBean.afterPropertiesSet() 方法。所以在那个时候抛出异常。毕竟我有下一个日志:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sessionFactory' defined in class path resource [app/db/Hibernate.class]:
Invocation of init method failed; nested exception is
org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
在这种情况下处理数据库连接错误的最佳方法是什么?
观察到的行为对我来说完全符合逻辑。您问题中提供的代码仅在内部配置 SessionFactory
。一旦结果 LocalSessionFactoryBean
返回(并被其他代码片段使用),就会尝试 实际上 连接您以这种方式配置的数据库。
如果配置的 DataSource
- 通过 getDataSource()
获得 - 在建立(第一个)连接时遇到错误,您将按所述点击 org.hibernate.exception.GenericJDBCException
。
注意:有关详细信息,您必须提供更多的堆栈跟踪。
作为反制措施,您可以在返回前将以下 "connection check" 添加到上述代码片段中:
@Bean
LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
DataSource ds = getDataSource();
sessionFactoryBean.setDataSource(ds);
sessionFactoryBean.setPackagesToScan(Properties.hibernate.packages);
sessionFactoryBean.setHibernateProperties(databaseSettings.getHibernateProperties());
// conduct an early connection attempt which auto-closes the opened connection
try (Connection con = ds.getConnection(username, password)) {
// if you reach this line, connections to the DB can be established.
return sessionFactoryBean;
} catch (Exception ex {
// handle exceptions with one or more of these steps
// 1. log the problem to somewhere, console...
// 2. re-try connection attempt?
// 3. escalate by throwing a self-defined RuntimeException
// .. and shutdown the application in controlled manner?
}
}
另请参阅 Oracle 的 Connecting with DataSource Objects 指南。
提示:出于多种原因,您也可以考虑使用 "PooledDataSource"(请参阅上述数据源指南中的详细信息)。
希望对您有所帮助。