forked from Luke-Sikina/picsure-search-refinement
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DataSourceVerifier to check DB connection on startup
New class `DataSourceVerifier` created to ensure that the database connection is successfully established when the application context is refreshed. It logs messages indicating the success or failure of this verification.
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...weights/src/main/java/edu/harvard/dbmi/avillach/dictionaryweights/DataSourceVerifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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.event.ContextRefreshedEvent; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
|
||
@Service | ||
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."); | ||
} else { | ||
LOG.info("Failed to obtain a connection from the datasource."); | ||
} | ||
} catch (SQLException e) { | ||
LOG.info("Error verifying datasource connection: {}", e.getMessage()); | ||
} | ||
} | ||
|
||
} |