Class DuckdbDataSource

Object
BasicDataSource
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

    Modifier and Type
    Method
    Description
    void
    Closes this data source, releasing all associated resources.
    protected DuckdbConnectionFactory
    Creates a DuckDB-specific connection factory.
     

    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

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface CommonDataSource

    createShardingKeyBuilder

    Methods inherited from interface Wrapper

    isWrapperFor, unwrap
  • Constructor Details

    • 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 Details

    • 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
    • getParentLogger

      public Logger getParentLogger() throws SQLFeatureNotSupportedException
      Specified by:
      getParentLogger in interface CommonDataSource
      Throws:
      SQLFeatureNotSupportedException