Added connection test when creating any data source now.

This commit is contained in:
Andrew Lalis 2024-02-05 11:38:10 -05:00
parent 54f6612048
commit 64c46e6be9
2 changed files with 16 additions and 6 deletions

View File

@ -59,7 +59,11 @@ public class JdbcDataSourceFactory implements DataSourceFactory {
throw new ProfileLoadException("Profile " + profileName + " has a database with an unsupported schema version."); throw new ProfileLoadException("Profile " + profileName + " has a database with an unsupported schema version.");
} }
} }
return new JdbcDataSource(getJdbcUrl(profileName), Profile.getContentDir(profileName)); var dataSource = new JdbcDataSource(getJdbcUrl(profileName), Profile.getContentDir(profileName));
if (!testConnection(dataSource)) {
throw new ProfileLoadException("Unabled to connect to the profile's database.");
}
return dataSource;
} }
public SchemaStatus getSchemaStatus(String profileName) throws IOException { public SchemaStatus getSchemaStatus(String profileName) throws IOException {

View File

@ -14,6 +14,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
@ -50,6 +51,8 @@ public class ProfileLoader {
} catch (IOException e) { } catch (IOException e) {
throw new ProfileLoadException("Failed to load profile settings.", e); throw new ProfileLoadException("Failed to load profile settings.", e);
} }
// Try to check the profile's schema version and migrate if needed.
try { try {
DataSourceFactory.SchemaStatus status = dataSourceFactory.getSchemaStatus(name); DataSourceFactory.SchemaStatus status = dataSourceFactory.getSchemaStatus(name);
if (status == DataSourceFactory.SchemaStatus.NEEDS_MIGRATION) { if (status == DataSourceFactory.SchemaStatus.NEEDS_MIGRATION) {
@ -72,12 +75,15 @@ public class ProfileLoader {
} }
// Check for a recent backup and make one if not present. // Check for a recent backup and make one if not present.
LocalDateTime lastBackup = ProfileBackups.getLastBackupTimestamp(name);
if (lastBackup == null || lastBackup.isBefore(LocalDateTime.now().minusDays(1))) {
try { try {
ProfileBackups.makeBackup(name); ProfileBackups.makeBackup(name);
ProfileBackups.cleanOldBackups(name); ProfileBackups.cleanOldBackups(name);
} catch (IOException e) { } catch (IOException e) {
log.error("Failed to create backup for profile " + name + ".", e); log.error("Failed to create backup for profile " + name + ".", e);
} }
}
DataSource dataSource = dataSourceFactory.getDataSource(name); DataSource dataSource = dataSourceFactory.getDataSource(name);
return new Profile(name, settings, dataSource); return new Profile(name, settings, dataSource);