All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.testcontainers.delegate.AbstractDatabaseDelegate Maven / Gradle / Ivy

There is a newer version: 1.20.4
Show newest version
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();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy