Testcontainers JDBC driver that provides lightweight, throwaway database instances for testing
The ContainerDatabaseDriver is the core component that implements the JDBC Driver interface to intercept database connections and manage Docker containers automatically.
The driver automatically registers itself with the JDBC DriverManager when the class is loaded.
/**
* JDBC Driver implementation that handles jdbc:tc: URLs
*/
public class ContainerDatabaseDriver implements java.sql.Driver {
// Driver automatically registers itself via static block
}Determines whether the driver can handle a given JDBC URL.
/**
* Tests whether this driver can connect to the given URL
* @param url JDBC URL to test
* @return true if URL starts with "jdbc:tc:", false otherwise
* @throws SQLException if database access error occurs
*/
public boolean acceptsURL(String url) throws SQLException;Usage Example:
ContainerDatabaseDriver driver = new ContainerDatabaseDriver();
boolean canHandle = driver.acceptsURL("jdbc:tc:mysql:8.0://localhost/test"); // true
boolean cannotHandle = driver.acceptsURL("jdbc:mysql://localhost/test"); // falseCreates database connections by provisioning containers and delegating to the appropriate database driver.
/**
* Attempts to make a database connection to the given URL
* @param url JDBC URL in format jdbc:tc:type:tag://host:port/database?params
* @param info connection properties (user, password, etc.)
* @return Connection to the containerized database, or null if URL not supported
* @throws SQLException if database access error occurs
*/
public synchronized Connection connect(String url, Properties info) throws SQLException;Connection Process:
JdbcDatabaseContainerProviderUsage Example:
Properties props = new Properties();
props.setProperty("user", "test");
props.setProperty("password", "test");
String url = "jdbc:tc:postgresql:13.7://localhost/testdb?TC_INITSCRIPT=schema.sql";
Connection conn = DriverManager.getConnection(url, props);Provides information about supported connection properties.
/**
* Gets information about possible properties for connections
* @param url JDBC URL
* @param info proposed connection properties
* @return array of DriverPropertyInfo objects
* @throws SQLException if database access error occurs
*/
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException;Provides driver version information.
/**
* Gets the driver's major version number
* @return major version number (delegates to underlying driver or returns 1)
*/
public int getMajorVersion();
/**
* Gets the driver's minor version number
* @return minor version number (delegates to underlying driver or returns 0)
*/
public int getMinorVersion();Indicates whether the driver is JDBC compliant.
/**
* Reports whether this driver is a genuine JDBC Compliant driver
* @return true if underlying driver is JDBC compliant, false otherwise
*/
public boolean jdbcCompliant();Provides access to the driver's logger.
/**
* Return the parent Logger of all the Loggers used by this driver
* @return parent Logger for this driver
* @throws SQLFeatureNotSupportedException if not supported by underlying driver
*/
public Logger getParentLogger() throws SQLFeatureNotSupportedException;Static utility methods for managing database containers directly.
/**
* Utility method to kill ALL database containers
* Provided for cleanup in testing scenarios with resource constraints
*/
public static void killContainers();
/**
* Utility method to kill a specific database container
* @param jdbcUrl the JDBC URL of the container to kill
*/
public static void killContainer(String jdbcUrl);
/**
* Utility method to get container instance by JDBC URL
* @param jdbcUrl the JDBC URL of the container to retrieve
* @return container instance or null if not found
*/
static JdbcDatabaseContainer getContainer(String jdbcUrl);Usage Example:
// Clean up all containers after test suite
@AfterAll
public static void cleanup() {
ContainerDatabaseDriver.killContainers();
}
// Clean up specific container
ContainerDatabaseDriver.killContainer("jdbc:tc:mysql:8.0://localhost/test");
// Get container for inspection
JdbcDatabaseContainer container = ContainerDatabaseDriver.getContainer(
"jdbc:tc:postgresql:13://localhost/app"
);
if (container != null) {
System.out.println("Container is running: " + container.isRunning());
}The driver accepts URLs in the following format:
jdbc:tc:<database_type>[:<image_tag>]://[<host>:<port>]/<database_name>[?<parameters>]Components:
database_type - Database type (mysql, postgresql, oracle, etc.)image_tag - Optional Docker image tag (uses provider default if omitted)host:port - Optional host and port (ignored, container provides these)database_name - Database name to connect toparameters - Query parameters for both JDBC driver and TestcontainersExamples:
// MySQL with default tag
"jdbc:tc:mysql://localhost/testdb"
// PostgreSQL with specific version
"jdbc:tc:postgresql:13.7://localhost/myapp?user=app&password=secret"
// With initialization script
"jdbc:tc:mysql:8.0://localhost/test?TC_INITSCRIPT=schema.sql"
// With initialization function
"jdbc:tc:postgresql://localhost/test?TC_INITFUNCTION=com.example.DbInit::setupSchema"The driver manages container lifecycle based on connection usage:
TC_DAEMON=true persist across connection cyclesTC_REUSABLE=true may be reused across test runsThe driver implementation is thread-safe with synchronized access to container caches and connection management.
Install with Tessl CLI
npx tessl i tessl/maven-org-testcontainers--jdbc