Class DuckdbDataSource

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

    public class DuckdbDataSource
    extends BasicDataSource
    implements ManageableDataSource
    A specialized JDBC DataSource implementation for DuckDB connections.

    This class extends Apache DBCP's BasicDataSource to provide connection pooling specifically for DuckDB. It uses a custom DuckdbConnectionFactory to create connections that leverage DuckDB's connection duplication feature.

    The class accepts a list of SQL statements that are executed once to initialize the database (typically to load extensions like 'spatial' and 'parquet').

    • Field Summary

      • Fields inherited from class BasicDataSource

        closed, connectionInitSqls, connectionPool, connectionProperties, dataSource, defaultAutoCommit, defaultCatalog, defaultReadOnly, defaultTransactionIsolation, driverClassLoader, driverClassName, initialSize, logWriter, maxActive, maxIdle, maxOpenPreparedStatements, maxWait, minEvictableIdleTimeMillis, minIdle, numTestsPerEvictionRun, password, poolPreparedStatements, testOnBorrow, testOnReturn, testWhileIdle, timeBetweenEvictionRunsMillis, url, username, validationQuery, validationQueryTimeout
    • Constructor Summary

      Constructors 
      Constructor Description
      DuckdbDataSource​(List<String> databaseInitSqls)
      Constructs a new DuckdbDataSource.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes this data source, releasing all associated resources.
      protected DuckdbConnectionFactory createConnectionFactory()
      Creates a DuckDB-specific connection factory.
      Logger getParentLogger()  
      • Methods inherited from class BasicDataSource

        addConnectionProperty, createConnectionPool, createDataSource, createDataSourceInstance, createPoolableConnectionFactory, getConnection, getConnection, getConnectionInitSqls, getDefaultAutoCommit, getDefaultCatalog, getDefaultReadOnly, getDefaultTransactionIsolation, getDriverClassLoader, getDriverClassName, getInitialSize, getLogAbandoned, getLoginTimeout, getLogWriter, getMaxActive, getMaxIdle, getMaxOpenPreparedStatements, getMaxWait, getMinEvictableIdleTimeMillis, getMinIdle, getNumActive, getNumIdle, getNumTestsPerEvictionRun, getPassword, getRemoveAbandoned, getRemoveAbandonedTimeout, getTestOnBorrow, getTestOnReturn, getTestWhileIdle, getTimeBetweenEvictionRunsMillis, getUrl, getUsername, getValidationQuery, getValidationQueryTimeout, isAccessToUnderlyingConnectionAllowed, isClosed, isPoolPreparedStatements, isWrapperFor, log, removeConnectionProperty, setAccessToUnderlyingConnectionAllowed, setConnectionInitSqls, setConnectionProperties, setDefaultAutoCommit, setDefaultCatalog, setDefaultReadOnly, setDefaultTransactionIsolation, setDriverClassLoader, setDriverClassName, setInitialSize, setLogAbandoned, setLoginTimeout, setLogWriter, setMaxActive, setMaxIdle, setMaxOpenPreparedStatements, setMaxWait, setMinEvictableIdleTimeMillis, setMinIdle, setNumTestsPerEvictionRun, setPassword, setPoolPreparedStatements, setRemoveAbandoned, setRemoveAbandonedTimeout, setTestOnBorrow, setTestOnReturn, setTestWhileIdle, setTimeBetweenEvictionRunsMillis, setUrl, setUsername, setValidationQuery, setValidationQueryTimeout, unwrap, validateConnectionFactory
    • Constructor Detail

      • DuckdbDataSource

        public DuckdbDataSource​(List<String> databaseInitSqls)
        Constructs a new DuckdbDataSource.
        Parameters:
        databaseInitSqls - A list of SQL statements to execute once when initializing the database (typically used to load DuckDB extensions like 'spatial' and 'parquet')
        Throws:
        NullPointerException - if databaseInitSqls is null
    • Method Detail

      • createConnectionFactory

        protected DuckdbConnectionFactory createConnectionFactory()
                                                           throws SQLException
        Creates a DuckDB-specific connection factory.

        This method overrides the parent class implementation to create a DuckdbConnectionFactory instead of the default one. It configures connection properties and passes the database initialization SQL statements to the factory.

        Overrides:
        createConnectionFactory in class BasicDataSource
        Returns:
        The created DuckdbConnectionFactory
        Throws:
        SQLException - if there is an error creating the connection factory
      • close

        public void close()
                   throws SQLException
        Closes this data source, releasing all associated resources.

        This method ensures proper cleanup by first closing the underlying DuckdbConnectionFactory (which closes the sentinel connection) and then calling the parent class close method.

        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface ManageableDataSource
        Overrides:
        close in class BasicDataSource
        Throws:
        SQLException - if there is an error closing the data source