forked from Luke-Sikina/picsure-search-refinement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataSourceVerifier.java
40 lines (32 loc) · 1.29 KB
/
DataSourceVerifier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package edu.harvard.dbmi.avillach.dictionary.datasource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@Profile("!test")
@Configuration
public class DataSourceVerifier {
private static final Logger LOG = LoggerFactory.getLogger(DataSourceVerifier.class);
private final DataSource dataSource;
@Autowired
public DataSourceVerifier(DataSource dataSource) {
this.dataSource = dataSource;
}
@EventListener(ContextRefreshedEvent.class)
public void verifyDataSourceConnection() {
try (Connection connection = dataSource.getConnection()) {
if (connection != null) {
LOG.info("Datasource connection verified successfully.");
}
} catch (SQLException e) {
LOG.info("Failed to obtain a connection from the datasource.");
LOG.debug("Error verifying datasource connection: {}", e.getMessage());
}
}
}