Interface DatabaseEngine


public interface DatabaseEngine
数据库引擎接口 - 一站式数据库系统管理

核心职责:

  • 启动和关闭数据库系统
  • 管理连接池生命周期
  • 提供持久化上下文
  • 创建 Repository 实例
  • 执行数据库迁移
  • 健康检查

使用示例:


 DatabaseEngine engine = DatabaseEngine.fromConfig(magstarConfig, plugin);
 engine.start();

 // 使用
 Repository<User, Long> userRepo = engine.getRepository(User.class);
 User user = userRepo.findById(1L).orElseThrow();

 // 关闭
 engine.shutdown();
 
Since:
1.0.0
Version:
1.0.0
Author:
Berry_so
  • Method Details

    • start

      启动数据库引擎

      执行步骤:

      1. 验证配置
      2. 初始化连接池
      3. 测试数据库连接
      4. 执行数据库迁移(如果启用)
      5. 初始化持久化上下文
      6. 注册 JVM 关闭钩子
      Returns:
      当前引擎实例(支持链式调用)
      Throws:
      DatabaseEngineException - 如果启动失败
    • shutdown

      void shutdown() throws DatabaseEngineException
      关闭数据库引擎

      执行步骤:

      1. 刷新所有未持久化的实体
      2. 关闭所有活跃连接
      3. 关闭连接池
      4. 清理资源
      Throws:
      DatabaseEngineException - 如果关闭失败
    • isRunning

      boolean isRunning()
      检查引擎是否正在运行
      Returns:
      true 如果引擎已启动且未关闭
    • healthCheck

      boolean healthCheck()
      检查数据库连接健康状态
      Returns:
      true 如果数据库连接正常
    • getDatabaseService

      DatabaseService getDatabaseService()
      获取数据库服务
      Returns:
      DatabaseService 实例
      Throws:
      IllegalStateException - 如果引擎未启动
    • getConnectionPool

      ConnectionPool getConnectionPool()
      获取连接池
      Returns:
      ConnectionPool 实例
      Throws:
      IllegalStateException - 如果引擎未启动
    • getPersistenceContext

      PersistenceContext getPersistenceContext()
      获取持久化上下文
      Returns:
      PersistenceContext 实例
      Throws:
      IllegalStateException - 如果引擎未启动
    • getConnection

      Connection getConnection() throws SQLException
      获取数据库连接(从连接池)
      Returns:
      Connection 实例
      Throws:
      SQLException - 如果获取连接失败
      IllegalStateException - 如果引擎未启动
    • getRepository

      <T, ID> Repository<T,ID> getRepository(Class<T> entityClass)
      创建 Repository 实例
      Type Parameters:
      T - 实体类型
      Parameters:
      entityClass - 实体类
      Returns:
      Repository 实例
      Throws:
      IllegalStateException - 如果引擎未启动
    • getCompositeRepository

      <T, K extends CompositeKey> CompositeKeyRepository<T,K> getCompositeRepository(Class<T> entityClass)
      创建 CompositeKeyRepository 实例
      Type Parameters:
      T - 实体类型
      Parameters:
      entityClass - 实体类
      Returns:
      CompositeKeyRepository 实例
      Throws:
      IllegalStateException - 如果引擎未启动
    • migrate

      void migrate(String migrationPath) throws DatabaseEngineException
      执行数据库迁移
      Parameters:
      migrationPath - 迁移脚本路径
      Throws:
      DatabaseEngineException - 如果迁移失败
    • autoCreateTables

      void autoCreateTables(Class<?>... entityClasses) throws DatabaseEngineException
      自动创建表结构(基于实体类)
      Parameters:
      entityClasses - 实体类列表
      Throws:
      DatabaseEngineException - 如果创建失败
    • getStatistics

      String getStatistics()
      获取引擎统计信息
      Returns:
      统计信息(JSON 格式)