org.testcontainers.delegate.AbstractDatabaseDelegate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of database-commons Show documentation
Show all versions of database-commons Show documentation
Isolated container management for Java code testing
package org.testcontainers.delegate;
import java.util.Collection;
/**
* @param connection to the database
*/
public abstract class AbstractDatabaseDelegate 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 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();
}