-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
CassandraDatabaseDelegate.java
58 lines (52 loc) · 1.99 KB
/
CassandraDatabaseDelegate.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package org.testcontainers.containers.delegate;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.DriverException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.testcontainers.containers.CassandraContainer;
import org.testcontainers.containers.ContainerState;
import org.testcontainers.delegate.AbstractDatabaseDelegate;
import org.testcontainers.exception.ConnectionCreationException;
import org.testcontainers.ext.ScriptUtils.ScriptStatementFailedException;
/**
* Cassandra database delegate
*
* @author Eugeny Karpov
*/
@Slf4j
@RequiredArgsConstructor
public class CassandraDatabaseDelegate extends AbstractDatabaseDelegate<Session> {
private final ContainerState container;
@Override
protected Session createNewConnection() {
try {
return CassandraContainer.getCluster(container)
.newSession();
} catch (DriverException e) {
log.error("Could not obtain cassandra connection");
throw new ConnectionCreationException("Could not obtain cassandra connection", e);
}
}
@Override
public void execute(String statement, String scriptPath, int lineNumber, boolean continueOnError, boolean ignoreFailedDrops) {
try {
ResultSet result = getConnection().execute(statement);
if (result.wasApplied()) {
log.debug("Statement {} was applied", statement);
} else {
throw new ScriptStatementFailedException(statement, lineNumber, scriptPath);
}
} catch (DriverException e) {
throw new ScriptStatementFailedException(statement, lineNumber, scriptPath, e);
}
}
@Override
protected void closeConnectionQuietly(Session session) {
try {
session.getCluster().close();
} catch (Exception e) {
log.error("Could not close cassandra connection", e);
}
}
}