forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractDatabaseDelegate.java
More file actions
55 lines (46 loc) · 1.42 KB
/
AbstractDatabaseDelegate.java
File metadata and controls
55 lines (46 loc) · 1.42 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package org.testcontainers.delegate;
import java.util.Collection;
/**
* @param <CONNECTION> connection to the database
* @author Eugeny Karpov
*/
public abstract class AbstractDatabaseDelegate<CONNECTION> implements DatabaseDelegate {
/**
* Database connection
*/
private CONNECTION connection;
private boolean isConnectionStarted = false;
/**
* Get or create new connection to the database
*/
protected CONNECTION getConnection() {
if (!isConnectionStarted) {
connection = createNewConnection();
isConnectionStarted = true;
}
return connection;
}
@Override
public void execute(Collection<String> statements, String scriptPath, boolean continueOnError, boolean ignoreFailedDrops) {
int lineNumber = 0;
for (String statement : statements) {
lineNumber++;
execute(statement, scriptPath, lineNumber, continueOnError, ignoreFailedDrops);
}
}
@Override
public void close() {
if (isConnectionStarted) {
closeConnectionQuietly(connection);
isConnectionStarted = false;
}
}
/**
* Quietly close the connection
*/
protected abstract void closeConnectionQuietly(CONNECTION connection);
/**
* Template method for creating new connections to the database
*/
protected abstract CONNECTION createNewConnection();
}