Skip to content

Commit ee0e01c

Browse files
committed
added timeout to database config, refactorings
1 parent 5e31ac5 commit ee0e01c

25 files changed

+191
-109
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* added option for IngestionTool to update already existing entries
88
* changed database to use relative archive paths and added migration tool
99
* updated SLSTR reader to allow switching geo-coding source between tie-point/pixel
10+
* added database timeout to configuration
1011

1112
### Updates from version 1.5.4 to 1.5.5
1213
* updated to use SNAP version 8.0.9 / S3TBX version 8.0.6

MMS_Manual_v_1_5.pdf

127 KB
Binary file not shown.

core/src/main/java/com/bc/fiduceo/db/AbstractDriver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ abstract class AbstractDriver implements Driver {
4343
Connection connection;
4444

4545
@Override
46-
public void open(BasicDataSource dataSource) throws SQLException {
46+
public void open(DatabaseConfig databaseConfig) throws SQLException {
47+
final BasicDataSource dataSource = databaseConfig.getDataSource();
4748
try {
4849
final java.sql.Driver driverClass = (java.sql.Driver) Class.forName(dataSource.getDriverClassName()).newInstance();
4950
DriverManager.registerDriver(driverClass);

core/src/main/java/com/bc/fiduceo/db/DatabaseConfig.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
package com.bc.fiduceo.db;
2222

2323
import org.apache.commons.dbcp2.BasicDataSource;
24+
import org.esa.snap.core.util.StringUtils;
2425

2526
import java.io.File;
2627
import java.io.FileInputStream;
2728
import java.io.IOException;
29+
import java.sql.SQLException;
2830
import java.util.Properties;
2931

3032
public class DatabaseConfig {
@@ -60,4 +62,26 @@ public BasicDataSource getDataSource() {
6062

6163
return dataSource;
6264
}
65+
66+
public void setDataSource(BasicDataSource dataSource) {
67+
final String driverClassName = dataSource.getDriverClassName();
68+
final String url = dataSource.getUrl();
69+
final String username = dataSource.getUsername();
70+
final String password = dataSource.getPassword();
71+
if (StringUtils.isNullOrEmpty(driverClassName) |
72+
StringUtils.isNullOrEmpty(url) |
73+
StringUtils.isNullOrEmpty(username) |
74+
password == null) {
75+
throw new IllegalArgumentException("incomplete database configuration");
76+
}
77+
properties.setProperty("driverClassName", driverClassName);
78+
properties.setProperty("url", url);
79+
properties.setProperty("username", username);
80+
properties.setProperty("password", password);
81+
}
82+
83+
public int getTimeoutInSeconds() {
84+
final String timeout = properties.getProperty("timeout", "120");
85+
return Integer.parseInt(timeout);
86+
}
6387
}

core/src/main/java/com/bc/fiduceo/db/DbMaintenanceTool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ private void initialize(CommandLine commandLine) throws IOException, SQLExceptio
240240
final SystemConfig systemConfig = SystemConfig.loadFrom(confDirPath.toFile());
241241
final GeometryFactory geometryFactory = new GeometryFactory(systemConfig.getGeometryLibraryType());
242242

243-
storage = Storage.create(databaseConfig.getDataSource(), geometryFactory);
243+
storage = Storage.create(databaseConfig, geometryFactory);
244244
if (!storage.isInitialized()) {
245245
storage.initialize();
246246
}

core/src/main/java/com/bc/fiduceo/db/Driver.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.bc.fiduceo.core.SatelliteObservation;
2626
import com.bc.fiduceo.core.Sensor;
2727
import com.bc.fiduceo.geometry.GeometryFactory;
28-
import org.apache.commons.dbcp2.BasicDataSource;
2928

3029
import java.sql.SQLException;
3130
import java.util.List;
@@ -42,7 +41,7 @@ public interface Driver {
4241

4342
void setGeometryFactory(GeometryFactory geometryFactory);
4443

45-
void open(BasicDataSource dataSource) throws SQLException;
44+
void open(DatabaseConfig databaseConfig) throws SQLException;
4645

4746
boolean isInitialized() throws SQLException;
4847

@@ -55,7 +54,7 @@ public interface Driver {
5554
void insert(SatelliteObservation satelliteObservation) throws SQLException;
5655

5756
void update(SatelliteObservation satelliteObservation) throws SQLException;
58-
57+
5958
AbstractBatch updatePathBatch(SatelliteObservation satelliteObservation, String newPath, AbstractBatch batch) throws SQLException;
6059

6160
void commitBatch(AbstractBatch batch) throws SQLException;

core/src/main/java/com/bc/fiduceo/db/MongoDbDriver.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,18 @@ public String getUrlPattern() {
6666
}
6767

6868
@Override
69-
public void open(BasicDataSource dataSource) {
69+
public void open(DatabaseConfig databaseConfig) throws SQLException {
70+
final BasicDataSource dataSource = databaseConfig.getDataSource();
7071
final String address = parseAddress(dataSource.getUrl());
7172
final String port = parsePort(dataSource.getUrl());
7273
final ServerAddress serverAddress = new ServerAddress(address, Integer.parseInt(port));
7374

75+
final int timeoutInMillis = databaseConfig.getTimeoutInSeconds() * 1000;
76+
7477
final MongoClientOptions clientOptions = MongoClientOptions.builder().
75-
connectTimeout(120000).
76-
socketTimeout(120000).
77-
serverSelectionTimeout(120000).build();
78+
connectTimeout(timeoutInMillis).
79+
socketTimeout(timeoutInMillis).
80+
serverSelectionTimeout(timeoutInMillis).build();
7881

7982
final String username = dataSource.getUsername();
8083
final String password = dataSource.getPassword();

core/src/main/java/com/bc/fiduceo/db/PostGISDriver.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public void setGeometryFactory(GeometryFactory geometryFactory) {
5252
}
5353

5454
@Override
55-
public void open(BasicDataSource dataSource) throws SQLException {
55+
public void open(DatabaseConfig databaseConfig) throws SQLException {
56+
final BasicDataSource dataSource = databaseConfig.getDataSource();
5657
try {
5758
final java.sql.Driver driverClass = (java.sql.Driver) Class.forName(dataSource.getDriverClassName()).newInstance();
5859
DriverManager.registerDriver(driverClass);
@@ -64,8 +65,8 @@ public void open(BasicDataSource dataSource) throws SQLException {
6465
final Properties properties = new Properties();
6566
properties.put("user", dataSource.getUsername());
6667
properties.put("password", dataSource.getPassword());
67-
properties.put("loginTimeout", "120");
68-
properties.put("connectTimeout", "120");
68+
properties.put("loginTimeout", databaseConfig.getTimeoutInSeconds());
69+
properties.put("connectTimeout", databaseConfig.getTimeoutInSeconds());
6970

7071
connection = DriverManager.getConnection(url, properties);
7172
connection.setAutoCommit(false);

core/src/main/java/com/bc/fiduceo/db/Storage.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public class Storage {
3434

3535
private Driver driver;
3636

37-
public static Storage create(BasicDataSource dataSource, GeometryFactory geometryFactory) throws SQLException {
38-
return new Storage(dataSource, geometryFactory);
37+
public static Storage create(DatabaseConfig databaseConfig, GeometryFactory geometryFactory) throws SQLException {
38+
return new Storage(databaseConfig, geometryFactory);
3939
}
4040

4141
public void close() throws SQLException {
@@ -88,10 +88,10 @@ public int insert(Sensor sensor) throws SQLException {
8888
return driver.insert(sensor);
8989
}
9090

91-
private Storage(BasicDataSource dataSource, GeometryFactory geometryFactory) throws SQLException {
92-
driver = createDriver(dataSource);
91+
private Storage(DatabaseConfig databaseConfig, GeometryFactory geometryFactory) throws SQLException {
92+
driver = createDriver(databaseConfig.getDataSource());
9393
driver.setGeometryFactory(geometryFactory);
94-
driver.open(dataSource);
94+
driver.open(databaseConfig);
9595
}
9696

9797
public boolean isAlreadyRegistered(QueryParameter queryParameter) throws SQLException {

core/src/test/java/com/bc/fiduceo/db/DatabaseConfigTest.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@
3131
import java.io.File;
3232
import java.io.IOException;
3333
import java.io.PrintWriter;
34+
import java.sql.SQLException;
3435

35-
import static org.junit.Assert.assertEquals;
36-
import static org.junit.Assert.assertNotNull;
37-
import static org.junit.Assert.fail;
36+
import static org.junit.Assert.*;
3837

3938
@RunWith(IOTestRunner.class)
4039
public class DatabaseConfigTest {
@@ -54,7 +53,7 @@ public void tearDown() {
5453
}
5554

5655
@Test
57-
public void testLoadAndGetDataSource() throws IOException {
56+
public void testLoadAndGetDataSource() throws IOException, SQLException {
5857
final File databaseConfigFile = TestUtil.createFileInTestDir("database.properties");
5958

6059
final PrintWriter printWriter = new PrintWriter(databaseConfigFile);
@@ -74,6 +73,35 @@ public void testLoadAndGetDataSource() throws IOException {
7473
assertEquals("pass-word", dataSource.getPassword());
7574
}
7675

76+
@Test
77+
public void testLoadAndGetTimeout() throws IOException {
78+
final File databaseConfigFile = TestUtil.createFileInTestDir("database.properties");
79+
80+
final PrintWriter printWriter = new PrintWriter(databaseConfigFile);
81+
printWriter.write("timeout = 654");
82+
printWriter.close();
83+
84+
databaseConfig.loadFrom(testDirectory);
85+
86+
assertEquals(654, databaseConfig.getTimeoutInSeconds());
87+
}
88+
89+
@Test
90+
public void testLoadAndGetTimeout_default() throws IOException {
91+
final File databaseConfigFile = TestUtil.createFileInTestDir("database.properties");
92+
93+
final PrintWriter printWriter = new PrintWriter(databaseConfigFile);
94+
printWriter.write("driverClassName = driver-class\n");
95+
printWriter.write("url = database-url\n");
96+
printWriter.write("username = user-name\n");
97+
printWriter.write("password = pass-word");
98+
printWriter.close();
99+
100+
databaseConfig.loadFrom(testDirectory);
101+
102+
assertEquals(120, databaseConfig.getTimeoutInSeconds());
103+
}
104+
77105
@Test
78106
public void testLoad_throwsWhenFileNotPresent() throws IOException {
79107
try {
@@ -84,7 +112,7 @@ public void testLoad_throwsWhenFileNotPresent() throws IOException {
84112
}
85113

86114
@Test
87-
public void testGetDatasource_throwsWhenNotLoaded() {
115+
public void testGetDatasource_throwsWhenNotLoaded() throws SQLException {
88116
try {
89117
databaseConfig.getDataSource();
90118
fail("RuntimeException expected");

0 commit comments

Comments
 (0)