Class AbstractJdbcDialect

java.lang.Object
io.agentscope.extensions.jdbc.dialect.AbstractJdbcDialect
All Implemented Interfaces:
SandboxLockStrategy, SessionStateDialect, SnapshotDialect, StoreDialect
Direct Known Subclasses:
H2Dialect, MysqlDialect, PostgresDialect, SqliteDialect

public abstract class AbstractJdbcDialect extends Object implements StoreDialect, SessionStateDialect, SnapshotDialect, SandboxLockStrategy
Aggregate dialect abstract class — implements all table-domain interfaces and the SandboxLockStrategy contract.

Holds a unified table prefix and per-table name overrides. Vendor classes extend this class and override only the methods where their SQL diverges from ANSI defaults. Lock behavior is also overridable: the default tryEnter(java.lang.String, int) uses a portable table-based lock; vendor dialects with native advisory locks (e.g. MySQL GET_LOCK, PostgreSQL pg_try_advisory_lock) override tryEnter.

Table-name resolution priority: per-table override > prefix + base. The final resolution methods lock the logic in the base class; vendor dialects inherit them and read the assembled names.

Fields are mutable at assembly time (set by AbstractJdbcDialectBuilder) and effectively read-only after build() returns.

Auto-detection

from(DataSource) returns a AbstractJdbcDialectBuilder that uses JDK SPI (ServiceLoader) to discover all dialect implementations on the classpath. Dialects are sorted by getOrder() (ascending) then by inheritance depth (child before parent). The first dialect whose supports(DatabaseMetaData) returns true wins.

Author:
shanhongyu
  • Constructor Details

    • AbstractJdbcDialect

      public AbstractJdbcDialect()
  • Method Details

    • storeTableName

      public final String storeTableName()
      Description copied from interface: StoreDialect
      Base table name (without prefix).
      Specified by:
      storeTableName in interface StoreDialect
    • sessionStateTableName

      public final String sessionStateTableName()
      Description copied from interface: SessionStateDialect
      Base table name (without prefix).
      Specified by:
      sessionStateTableName in interface SessionStateDialect
    • snapshotTableName

      public final String snapshotTableName()
      Description copied from interface: SnapshotDialect
      Base table name (without prefix).
      Specified by:
      snapshotTableName in interface SnapshotDialect
    • createTableDdls

      protected List<String> createTableDdls()
      Collects create-schema DDL for all table-domain interfaces, executed by the builder in a single connection during build(). Each table-domain method may return one or more statements (e.g. CREATE TABLE plus a secondary CREATE INDEX), so no vendor is constrained to a single SQL statement per table.
      Returns:
      all DDL statements, in store → sessions → snapshots order
    • bindDataSource

      public void bindDataSource(DataSource dataSource)
      Binds the DataSource used for lock acquisition. Called by the builder and JdbcDistributedStore; not intended for direct application use.

      If a different DataSource was already bound (e.g. the one the dialect was built with), the replacement is logged so callers notice a mismatched JdbcDistributedStore.create(dataSource, dialect).

    • getDataSource

      protected DataSource getDataSource()
      Returns the bound DataSource (for vendor lock implementations).
    • closeConnection

      public static void closeConnection(Connection con)
      Closes a JDBC connection quietly, returning it to the pool when pooled. Null-safe; a close failure is logged at debug level rather than propagated. Intended for vendor dialects that acquire a dedicated lock connection in their tryEnter implementation.
    • closeStatement

      public static void closeStatement(Statement stmt)
      Closes a JDBC statement quietly. Null-safe; failures are logged, not propagated.
    • closeResultSet

      public static void closeResultSet(ResultSet rs)
      Closes a JDBC result set quietly. Null-safe; failures are logged, not propagated.
    • supports

      public abstract boolean supports(DatabaseMetaData metaData) throws SQLException
      Whether this dialect supports the target database.
      Parameters:
      metaData - JDBC metadata (connection stays open)
      Returns:
      true if this dialect handles the database
      Throws:
      SQLException - if metadata reading fails
    • getOrder

      public int getOrder()
      Detection priority (lower wins). Default 100.
    • lockTableName

      protected String lockTableName()
      Lock table name, derived from the table prefix.
    • tryEnter

      public io.agentscope.harness.agent.sandbox.SandboxLease tryEnter(String lockName, int timeoutSeconds) throws InterruptedException
      Description copied from interface: SandboxLockStrategy
      Acquires the named lock, blocking until available or timeout expires.
      Specified by:
      tryEnter in interface SandboxLockStrategy
      Parameters:
      lockName - a unique, validated lock name
      timeoutSeconds - maximum wait in seconds
      Returns:
      a lease that releases the lock when closed
      Throws:
      InterruptedException - if the calling thread is interrupted
    • from

      public static AbstractJdbcDialectBuilder from(DataSource dataSource)
      Entry point for auto-detection. Returns a builder for chainable configuration.