我的问题是如何在Spring批处理中启动作业之前检查数据库是否可用。
我尝试创建一个实现JobExecutionListener
的新类,并将方法调用到beforeJob
。问题是如何进行此测试,因为数据库配置在database-context.xml
?
数据库-context.xml中:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/myDB_3" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
</beans>
例如,当我放myDB_4
(并且myDB_4
不存在)时,我收到以下错误:
Error while extracting database product name - falling back to empty error codes
org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'myDB_4'
at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:293)
现在如何捕获此错误并检查beforeJob
中创建的方法中此数据库是否可用?