Class SimpleSingleConnectionDataSource

  • All Implemented Interfaces:
    Closeable, AutoCloseable, Wrapper, CommonDataSource, DataSource

    public class SimpleSingleConnectionDataSource
    extends Object
    implements DataSource, Closeable
    A very simple implementation of DataSource intended to be used only during tests. As its name suggests, this implementation stores a single Connection which is always returned by the getConnection methods.

    Note specifically that the single connection ignores calls to close(), since we do not expect the code under test to close connections. To close the single connection, close this instance, which implements Closeable in addition to DataSource.

    The single Connection is eagerly initialized during construction since the expected usage pattern is to create an object before all tests run (e.g. using a JUnit @BeforeAll). Therefore, we keep things simple by eagerly initializing instead of waiting for the first getConnection call to occur.

    To be very clear, this is intended to be used only during tests, and specifically in tests that execute within a transaction that is rolled back once each test has executed. This ensures no data is actually stored in the database making for faster tests (due to no commit overhead) and also permits testing simultaneously against a shared database, for example a database setup for multiple developers or continuous integration servers to run tests against.

    This simple implementation does not support all of the DataSource methods. See the docs for each method.

    Implementation Note:
    This is heavily influenced from and some code copied from Spring's SingleConnectionDataSource but is much simpler since we do not need it to be as generic as Spring's version. We mostly used the code relating to the InvocationHandler that ignores calls to close a Connection, but are only handling one method close that we want to intercept.
    • Constructor Detail

      • SimpleSingleConnectionDataSource

        public SimpleSingleConnectionDataSource​(String url,
                                                String username)
        Create a new SingleConnectionDataSource with the given database URL and username. An empty password is supplied to DriverManager.

        The single Connection is eagerly initialized in this constructor.

        Parameters:
        url - the database URL
        username - the database username
      • SimpleSingleConnectionDataSource

        public SimpleSingleConnectionDataSource​(String url,
                                                String username,
                                                String password)
        Create a new SingleConnectionDataSource with the given database URL, username, and password to be supplied to DriverManager.

        The single Connection is eagerly initialized in this constructor.

        Parameters:
        url - the database URL
        username - the database username
        password - the database password